Skip to content

Commit 5cccc7c

Browse files
Rollup merge of rust-lang#151441 - Keith-Cancel:mgca3, r=BoxyUwU
Fix ICE: Don't try to evaluate type_consts when eagerly collecting items This fixes rust-lang#151246 The change is pretty straightforward if the Monomorphization strategy is eager which `-Clink-dead-code=true` sets. This then would lead to the existing code to try and evaluate a `type const` which does not have a body to evaluate leading to an ICE. The change is pretty straight forward just skip over type consts. This also seems like a sensible choice to me since a MonoItem can only be a Fn, Static, or Asm. A type const is none of the aforementioned. And even if it was added to the MonoItems list it would then later fail this check: https://github.com/rust-lang/rust/blob/fe98ddcfcfb6f185dbf4adeaf439d8a756da0273/compiler/rustc_monomorphize/src/collector.rs#L438-L440 Since that explicitly checks that the MonoItem's `DefKind` is static and not anything else. One more change is the addition of a simple test of the example code from rust-lang#151246 that checks that code compiles successfully with `-Clink-dead-code=true`. The only other change was to make the guard checks a little easier to read by making the logic more linear instead of one big if statement. r? @BoxyUwU @rustbot label +F-associated_const_equality +F-min_generic_const_args
2 parents f03c1a2 + 7635702 commit 5cccc7c

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,11 +1563,22 @@ impl<'v> RootCollector<'_, 'v> {
15631563
// If we're collecting items eagerly, then recurse into all constants.
15641564
// Otherwise the value is only collected when explicitly mentioned in other items.
15651565
if self.strategy == MonoItemCollectionStrategy::Eager {
1566-
if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization()
1567-
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
1568-
{
1569-
collect_const_value(self.tcx, val, self.output);
1566+
let def_id = id.owner_id.to_def_id();
1567+
// Type Consts don't have bodies to evaluate
1568+
// nor do they make sense as a static.
1569+
if self.tcx.is_type_const(def_id) {
1570+
// FIXME(mgca): Is this actually what we want? We may want to
1571+
// normalize to a ValTree then convert to a const allocation and
1572+
// collect that?
1573+
return;
1574+
}
1575+
if self.tcx.generics_of(id.owner_id).own_requires_monomorphization() {
1576+
return;
15701577
}
1578+
let Ok(val) = self.tcx.const_eval_poly(def_id) else {
1579+
return;
1580+
};
1581+
collect_const_value(self.tcx, val, self.output);
15711582
}
15721583
}
15731584
DefKind::Impl { of_trait: true } => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ check-pass
2+
//@compile-flags: -Clink-dead-code=true
3+
// link-dead-code tries to eagerly monomorphize and collect items
4+
// This lead to collector.rs to try and evaluate a type_const
5+
// which will fail since they do not have bodies.
6+
#![expect(incomplete_features)]
7+
#![feature(min_generic_const_args)]
8+
9+
#[type_const]
10+
const TYPE_CONST: usize = 0;
11+
fn main() {}

0 commit comments

Comments
 (0)