Skip to content

clippy::uninit_vec raises ICE #12032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
usamoi opened this issue Dec 28, 2023 · 6 comments
Closed

clippy::uninit_vec raises ICE #12032

usamoi opened this issue Dec 28, 2023 · 6 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@usamoi
Copy link

usamoi commented Dec 28, 2023

Summary

use std::cell::UnsafeCell;

// #[allow(clippy::uninit_vec)]
unsafe fn _check<S>() {
    let mut vec = Vec::<UnsafeCell<*mut S>>::with_capacity(1);
    vec.set_len(1);
}

fn main() {}

This code raises ICE. If clippy::uninit_vec is skipped, it does not raise ICE any more.

Version

rustc 1.77.0-nightly (89e2160c4 2023-12-27)
binary: rustc
commit-hash: 89e2160c4ca5808657ed55392620ed1dbbce78d1
commit-date: 2023-12-27
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Error output

Backtrace

  Checking play_rust v0.0.0 (/usamoi/repos/play_rust)
error: internal compiler error: /rustc/89e2160c4ca5808657ed55392620ed1dbbce78d1/compiler/rustc_middle/src/ty/layout.rs:971:21: failed to get layout for `*mut S`: Unknown(Alias(Projection, AliasTy { args: [S/#0], def_id: DefId(2:1880 ~ core[6d34]::ptr::metadata::Pointee::Metadata) })),
                              despite it being a field (#0) of an existing layout: TyAndLayout {
                                  ty: std::cell::UnsafeCell<*mut S>,
                                  layout: Layout {
                                      size: Size(8 bytes),
                                      align: AbiAndPrefAlign {
                                          abi: Align(8 bytes),
                                          pref: Align(8 bytes),
                                      },
                                      abi: Scalar(
                                          Initialized {
                                              value: Pointer(
                                                  AddressSpace(
                                                      0,
                                                  ),
                                              ),
                                              valid_range: 0..=18446744073709551615,
                                          },
                                      ),
                                      fields: Arbitrary {
                                          offsets: [
                                              Size(0 bytes),
                                          ],
                                          memory_index: [
                                              0,
                                          ],
                                      },
                                      largest_niche: None,
                                      variants: Single {
                                          index: 0,
                                      },
                                      max_repr_align: None,
                                      unadjusted_abi_align: Align(8 bytes),
                                  },
                              }

thread 'rustc' panicked at /rustc/89e2160c4ca5808657ed55392620ed1dbbce78d1/compiler/rustc_errors/src/lib.rs:1068:30:
Box<dyn Any>
stack backtrace:
 0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
 1: <rustc_errors::diagnostic_builder::BugAbort as rustc_errors::diagnostic_builder::EmissionGuarantee>::emit_producing_guarantee
 2: <rustc_errors::DiagCtxt>::bug::<alloc::string::String>
 3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
 4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
 5: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
 6: rustc_middle::util::bug::bug_fmt
 7: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::project_field::<rustc_const_eval::interpret::operand::OpTy>
 8: <rustc_const_eval::interpret::validity::ValidityVisitor<rustc_const_eval::const_eval::machine::CompileTimeInterpreter> as rustc_const_eval::interpret::visitor::ValueVisitor<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::visit_value
 9: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::validate_operand_internal
10: rustc_const_eval::util::check_validity_requirement::check_validity_requirement
11: <rustc_const_eval::provide::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, (rustc_middle::ty::layout::ValidityRequirement, rustc_middle::ty::ParamEnvAnd<rustc_middle::ty::Ty>))>>::call_once
    [... omitted 3 frames ...]
12: clippy_utils::ty::is_uninit_value_valid_for_ty
13: clippy_lints::uninit_vec::handle_uninit_vec_pair
14: <clippy_lints::uninit_vec::UninitVec as rustc_lint::passes::LateLintPass>::check_block
15: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_block
16: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_expr::{closure#0}
17: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_expr
18: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_nested_body
19: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_fn
20: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_item
21: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_mod
22: rustc_lint::late::check_crate::{closure#0}
23: rustc_lint::late::check_crate
24: rustc_interface::passes::analysis
    [... omitted 1 frame ...]
25: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml

note: please attach the file at `/usamoi/repos/play_rust/rustc-ice-2023-12-28T12_01_19-95315.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [check_validity_requirement] checking validity requirement for `core::cell::UnsafeCell<*mut S>`: allows being left uninitialized
#1 [analysis] running analysis passes on this crate
end of query stack
note: Clippy version: clippy 0.1.76 (89e2160 2023-12-27)

error: could not compile `play_rust` (bin "play_rust")

@usamoi usamoi added C-bug Category: Clippy is not doing the correct thing I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ labels Dec 28, 2023
@PartiallyUntyped
Copy link
Contributor

@rustbot claim

@PartiallyUntyped
Copy link
Contributor

PartiallyUntyped commented Jan 3, 2024

So I have "triaged" the issue to:

https://github.com/rust-lang/rust-clippy/blob/master/clippy_utils/src/ty.rs#L612-L616

pub fn is_uninit_value_valid_for_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
    cx.tcx
        .check_validity_requirement((ValidityRequirement::Uninit, cx.param_env.and(ty)))
        .unwrap_or_else(|_| is_uninit_value_valid_for_ty_fallback(cx, ty))
}

More specifically

    cx.tcx
        .check_validity_requirement(_)

when given:

ParamEnvAnd {
    param_env: ParamEnv {
        caller_bounds: [
            Binder { value: TraitPredicate(<S as std::marker::Sized>, polarity:Positive), bound_vars: [] },
        ],
        reveal: UserFacing,
    },
    value: std::cell::UnsafeCell<*mut S/#0>,
}

but I am not sure how to move from here.

@GuillaumeGomez
Copy link
Member

Maybe ask the compiler team directly?

@dswij
Copy link
Member

dswij commented Feb 11, 2024

rust-lang/rust#119620

@dswij dswij closed this as completed Feb 11, 2024
@usamoi
Copy link
Author

usamoi commented Feb 21, 2024

The code still raises ICE in the latest nightly clippy. Why close it?

@PartiallyUntyped
Copy link
Contributor

This is not a clippy issue, but a rustc issue. The corresponding issue has been created in rustc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

4 participants