Repository navigation
ICE during validation of ill-typed static: assertion left == right failed at rustc_const_eval/src/interpret/validity.rs #124164
Copy link
Copy link
Closed
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Issue: 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.Status: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
-
ICE-loc:
compiler/rustc_const_eval/src/interpret/validity.rs:727:21
rust/compiler/rustc_const_eval/src/interpret/validity.rs
Lines 726 to 728 in e3181b0
if let Some((_, alloc)) = ecx.memory.alloc_map.get(alloc_id) { assert_eq!(alloc.mutability, mutability); } -
Original ICE-triggering code is mutated from test-file
dropck_trait_cycle_checked.rs
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.Category: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Issue: 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.Status: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
- StatusShow more project fieldsDone
Activity
cushionbadak commentedon Apr 19, 2024
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:
It points to #122684
RalfJung commentedon May 6, 2024
Cc @oli-obk
RalfJung commentedon May 6, 2024
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 commentedon May 23, 2024
Probably because of fallback typeck figuring out that the static's type is
AtomicUsizeand providing that from then on (through HIR and MIR for the body), but failing to account for it from the static's type viatype_of.landaire commentedon Jun 23, 2024
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:
Backtrace
[-]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`[/+]theemathas commentedon Jul 2, 2024
@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?
Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…
Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…
Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…
Rollup merge of rust-lang#129472 - folkertdev:const-refs-to-static-as…
Unrolled build for rust-lang#129472
missing type for 'static' item#134542 as a duplicate of this issue