Skip to content

ICE during validation of ill-typed static: assertion left == right failed at rustc_const_eval/src/interpret/validity.rs #124164

Closed

Description

@cushionbadak

Code

(minimized)

static S_COUNT: = std::sync::atomic::AtomicUsize::new(0);

fn main() {}

EDIT: static S_COUNT = std::sync::atomic::AtomicUsize::new(0); also causes ICE.

Original Code

// Reject mixing cyclic structure and Drop when using trait
// objects to hide the cross-references.
//
// (Compare against ui/span/dropck_vec_cycle_checked.rs)

use std::cell::Cell;
use id::Id;

mod s {
    use std::sync::atomic::{AtomicUsize, Ordering};

    static S_COUNT:  = AtomicUsize::new(0);

    pub fn next_count() -> usize {
        S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
    }
}

mod id {
    use s;
    #[derive(Debug)]
    pub struct Id {
        orig_count: usize,
        count: usize,
    }

    impl Id {
        pub fn new() -> Id {
            let c = s::next_count();
            println!("building Id {}", c);
            Id { orig_count: c, count: c }
        }
        pub fn count(&self) -> usize {
            println!("Id::count on {} returns {}", self.orig_count, self.count);
            self.count
        }
    }

    impl Drop for Id {
        fn drop(&mut self) {
            println!("dropping Id {}", self.count);
            self.count = 0;
        }
    }
}

trait HasId {
    fn count(&self) -> usize;
}

#[derive(Debug)]
struct CheckId<T:HasId> {
    v: T
}

#[allow(non_snake_case)]
fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }

impl<T:HasId> Drop for CheckId<T> {
    fn drop(&mut self) {
        assert!(self.v.count() > 0);
    }
}

trait Obj<'a> : HasId {
    fn set0(&self, b: &'a Box<dyn Obj<'a>>);
    fn set1(&self, b: &'a Box<dyn Obj<'a>>);
}

struct O<'a> {
    id: Id,
    obj0: CheckId<Cell<Option<&'a Box<dyn Obj<'a>>>>>,
    obj1: CheckId<Cell<Option<&'a Box<dyn Obj<'a>>>>>,
}

impl<'a> HasId for O<'a> {
    fn count(&self) -> usize { self.id.count() }
}

impl<'a> O<'a> {
    fn new() -> Box<O<'a>> {
        Box::new(O {
            id: Id::new(),
            obj0: CheckId(Cell::new(None)),
            obj1: CheckId(Cell::new(None)),
        })
    }
}

impl<'a> HasId for Cell<Option<&'a Box<dyn Obj<'a>>>> {
    fn count(&self) -> usize {
        match self.get() {
            None => 1,
            Some(c) => c.count(),
        }
    }
}

impl<'a> Obj<'a> for O<'a> {
    fn set0(&self, b: &'a Box<dyn Obj<'a>>) {
        self.obj0.v.set(Some(b))
    }
    fn set1(&self, b: &'a Box<dyn Obj<'a>>) {
        self.obj1.v.set(Some(b))
    }
}


fn f() {
    let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
    o1.set0(&o2); //~ ERROR `o2` does not live long enough
    o1.set1(&o3); //~ ERROR `o3` does not live long enough
    o2.set0(&o2); //~ ERROR `o2` does not live long enough
    o2.set1(&o3); //~ ERROR `o3` does not live long enough
    o3.set0(&o1); //~ ERROR `o1` does not live long enough
    o3.set1(&o2); //~ ERROR `o2` does not live long enough
}

fn main() {
    f();
}

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (e3181b091 2024-04-18)
binary: rustc
commit-hash: e3181b091e88321f5ea149afed6db0edf0a4f37b
commit-date: 2024-04-18
host: aarch64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.4

Command

rustc

Error output

error: missing type for `static` item
 --> m_crushed_713540.rs:1:16
  |
1 | static S_COUNT: = std::sync::atomic::AtomicUsize::new(0);
  |                ^ help: provide a type for the static variable: `AtomicUsize`
Backtrace


thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/validity.rs:727:21:
assertion `left == right` failed
  left: Mut
 right: Not
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_failed::<rustc_ast_ir::Mutability, rustc_ast_ir::Mutability>
   4: rustc_const_eval::interpret::validity::mutability::<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>
   5: <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
   6: <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>>::walk_value
   7: <rustc_const_eval::interpret::eval_context::InterpCx<rustc_const_eval::const_eval::machine::CompileTimeInterpreter>>::validate_operand_internal
   8: rustc_const_eval::const_eval::eval_queries::eval_static_initializer_provider
      [... omitted 2 frames ...]
   9: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
  10: <rustc_data_structures::sync::parallel::ParallelGuard>::run::<(), rustc_data_structures::sync::parallel::enabled::par_for_each_in<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], <rustc_middle::hir::map::Map>::par_body_owners<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}>::{closure#0}::{closure#0}::{closure#0}>
  11: rustc_hir_analysis::check_crate
  12: rustc_interface::passes::analysis
      [... omitted 2 frames ...]
  13: <rustc_middle::ty::context::GlobalCtxt>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#3}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
  14: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#1}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/Users/jisukbyun/workspace/placeholder/240420_rustexec/rustc-ice-2024-04-19T15_57_00-86400.txt` to your bug report

query stack during panic:
#0 [eval_static_initializer] evaluating initializer of static `S_COUNT`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

Note

Activity

  1. added
    C-bugCategory: This is a bug.
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 19, 2024
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 19, 2024
  3. cushionbadak commented on Apr 19, 2024

    @cushionbadak
    Author

    searched nightlies: from nightly-2024-04-01 to nightly-2024-04-19
    regressed nightly: nightly-2024-04-19
    searched commit range: becebb3...e3181b0
    regressed commit: 5260893

    bisected with cargo-bisect-rustc v0.6.8

    Host triple: aarch64-apple-darwin
    Reproduce with:

    cargo bisect-rustc --start=2024-04-01 --end=2024-04-19 --preserve --regress=ice -vv

    It points to #122684

  4. added
    S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.
    on Apr 20, 2024
  5. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 28, 2024
  6. RalfJung commented on May 6, 2024

    @RalfJung
    Member

    It points to #122684

    Cc @oli-obk

  7. RalfJung commented on May 6, 2024

    @RalfJung
    Member

    Oh, this is the usual -- there's an error that should make us not evaluate the static. It doesn't even have a type, how on earth are we trying to evaluate that?!?

  8. oli-obk commented on May 23, 2024

    @oli-obk
    Contributor

    Probably because of fallback typeck figuring out that the static's type is AtomicUsize and providing that from then on (through HIR and MIR for the body), but failing to account for it from the static's type via type_of.

  9. moved this from Todo to In Progress in error taintingon May 25, 2024
  10. landaire commented on Jun 23, 2024

    @landaire
    Contributor

    I apologize for maybe just contributing noise since I don't have a reproducer, but I just hit this myself with a slightly different stack trace. I didn't even realize the compiler ICE'd, but the code that triggered this was when I was trying to accomplish the following:

    use core::cell::LazyCell;
    
    #[repr(transparent)]
    struct CachedPtr<T, F = fn() -> T>(LazyCell<T, F>);
    unsafe impl<T, F> Sync for CachedPtr<T, F> {}
    
    impl<T, F: FnOnce() -> T> CachedPtr<T, F> {
        #[inline]
        pub const fn new(f: F) -> CachedPtr<T, F> {
            CachedPtr(LazyCell::new(f))
        }
    }
    
    pub fn get_kernel32() -> PVOID {
        static KERNEL32: CachedPtr<PVOID> = CachedPtr::new(|| {
            let KERNEL32_STR: [u16; 13] = [75, 69, 82, 78, 69, 76, 51, 50, 46, 68, 76, 76, 0];
            crate::get_module_by_name(KERNEL32_STR.as_ptr())
        });
    
        *KERNEL32.0
    }
    Backtrace
    thread 'rustc' panicked at compiler\rustc_const_eval\src\interpret\validity.rs:740:21:
    assertion `left == right` failed
      left: Mut
     right: Not
    stack backtrace:
       0:     0x7ffa007ed5c3 - std::backtrace_rs::backtrace::dbghelp64::trace
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
       1:     0x7ffa007ed5c3 - std::backtrace_rs::backtrace::trace_unsynchronized
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
       2:     0x7ffa007ed5c3 - std::backtrace::Backtrace::create
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\backtrace.rs:331
       3:     0x7ffa007ed50a - std::backtrace::Backtrace::force_capture
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\backtrace.rs:312
       4:     0x7ffa05319dd0 - memchr
       5:     0x7ffa00807127 - alloc::boxed::impl$50::call
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\alloc\src\boxed.rs:2077
       6:     0x7ffa00807127 - std::panicking::rust_panic_with_hook
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\panicking.rs:799
       7:     0x7ffa00806fb7 - std::panicking::begin_panic_handler::closure$0
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\panicking.rs:664
       8:     0x7ffa008044af - std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\sys_common\backtrace.rs:171
       9:     0x7ffa00806c68 - std::panicking::begin_panic_handler
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\panicking.rs:652
      10:     0x7ffa0085d034 - core::panicking::panic_fmt
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\core\src\panicking.rs:72
      11:     0x7ffa0085d4c1 - core::panicking::assert_failed_inner
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\core\src\panicking.rs:408
      12:     0x7ffa05f04977 - <unicode_script[b464a1086fe56cc4]::ScriptIterator as core[51bac21d59838a73]::iter::traits::iterator::Iterator>::next
      13:     0x7ffa04ac8f6f - <rustc_const_eval[38ef215cd26bfcb]::interpret::eval_context::InterpCx<_>>::storage_live_dyn::is_very_trivially_sized
      14:     0x7ffa04ac95b6 - <rustc_const_eval[38ef215cd26bfcb]::interpret::eval_context::InterpCx<_>>::storage_live_dyn::is_very_trivially_sized
      15:     0x7ffa04ac948b - <rustc_const_eval[38ef215cd26bfcb]::interpret::eval_context::InterpCx<_>>::storage_live_dyn::is_very_trivially_sized
      16:     0x7ffa03b5ae8f - rustc_const_eval[38ef215cd26bfcb]::interpret::eval_context::mir_assign_valid_types
      17:     0x7ffa04b1541b - <rustc_const_eval[38ef215cd26bfcb]::check_consts::check::Checker as rustc_middle[8bd6919c1f4aa33a]::mir::visit::Visitor>::visit_terminator
      18:     0x7ffa03b5df9e - rustc_const_eval[38ef215cd26bfcb]::const_eval::eval_queries::eval_static_initializer_provider
      19:     0x7ffa03ab38e8 - <dyn std[9da2099332a7be2b]::io::Write as nu_ansi_term[11c658f38e3a19d9]::write::AnyWrite>::write_str
      20:     0x7ffa03a9aa2d - <dyn std[9da2099332a7be2b]::io::Write as nu_ansi_term[11c658f38e3a19d9]::write::AnyWrite>::write_str
      21:     0x7ffa0482b254 - rustc_ty_utils[57b86fd349b0e915]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
      22:     0x7ffa03ac5a5c - rustc_query_impl[961bb5465c83ca44]::query_system
      23:     0x7ffa0410f47b - <rustc_hir_typeck[7aa6ee09b860c109]::upvar::InferBorrowKind as rustc_hir_typeck[7aa6ee09b860c109]::expr_use_visitor::Delegate>::borrow
      24:     0x7ffa0418cbfc - rustc_hir_analysis[e57844e54f02f090]::check_crate
      25:     0x7ffa03ed62b2 - rustc_interface[11a50036ec87fade]::passes::resolver_for_lowering_raw
      26:     0x7ffa01107e17 - rustc_interface[11a50036ec87fade]::passes::analysis
      27:     0x7ffa03ab45ea - <dyn std[9da2099332a7be2b]::io::Write as nu_ansi_term[11c658f38e3a19d9]::write::AnyWrite>::write_str
      28:     0x7ffa039d4739 - rustc_ty_utils[57b86fd349b0e915]::ty::adt_sized_constraint
      29:     0x7ffa03ab9b73 - rustc_query_impl[961bb5465c83ca44]::query_system
      30:     0x7ffa010c514d - __ImageBase
      31:     0x7ffa010c250f - __ImageBase
      32:     0x7ffa010ca209 - __ImageBase
      33:     0x7ffa0081825d - alloc::boxed::impl$48::call_once
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\alloc\src\boxed.rs:2063
      34:     0x7ffa0081825d - alloc::boxed::impl$48::call_once
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\alloc\src\boxed.rs:2063
      35:     0x7ffa0081825d - std::sys::pal::windows::thread::impl$0::new::thread_start
                                   at /rustc/d0227c6a19c2d6e8dceb87c7a2776dc2b10d2a04/library\std\src\sys\pal\windows\thread.rs:52
      36:     0x7ffb19be257d - BaseThreadInitThunk
      37:     0x7ffb1afeaf28 - RtlUserThreadStart
    
    
    rustc version: 1.81.0-nightly (d0227c6a1 2024-06-11)
    platform: x86_64-pc-windows-msvc
    
    query stack during panic:
    #0 [eval_static_initializer] evaluating initializer of static `functions::get_kernel_32::KERNEL32`
    #1 [analysis] running analysis passes on this crate
    end of query stack
    
  11. changed the title [-]ICE: assertion `left == right` failed at `rustc_const_eval/src/interpret/validity.rs`[/-] [+]ICE during validation of ill-typed static: assertion `left == right` failed at `rustc_const_eval/src/interpret/validity.rs`[/+] on Jun 23, 2024
  12. theemathas commented on Jul 2, 2024

    @theemathas
    Contributor

    @landaire This issue occurs when a static containing interior mutability doesn't have a type annotation. Your issue appears to be different. Could you produce a reproducer and open a new issue?

  13. added 2 commits that reference this issue on Sep 5, 2024
    39d9903
    3daa015
  14. added 2 commits that reference this issue on Sep 6, 2024
    760d14d
    d72feff
  15. moved this from In Progress to Done in error taintingon Sep 6, 2024
  16. added a commit that references this issue on Sep 6, 2024
    9367a1f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions