Skip to content

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

@cushionbadak

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

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
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Apr 19, 2024
cushionbadak

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

added
S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.
on Apr 20, 2024
added
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Apr 28, 2024
RalfJung

RalfJung commented on May 6, 2024

@RalfJung
Member

It points to #122684

Cc @oli-obk

RalfJung

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?!?

oli-obk

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.

7 remaining items

added 2 commits that reference this issue on Sep 5, 2024

Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…

39d9903

Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…

3daa015
added 2 commits that reference this issue on Sep 6, 2024

Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…

760d14d

Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…

d72feff
moved this from In Progress to Done in error taintingon Sep 6, 2024
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

    Status

    Done

    Milestone

    No milestone

    Participants

    @RalfJung@oli-obk@matthiaskrgr@landaire@theemathas

    Issue actions

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