Rollup merge of #151441 - Keith-Cancel:mgca3, r=BoxyUwU

Fix ICE: Don't try to evaluate type_consts when eagerly collecting items

This fixes https://github.com/rust-lang/rust/issues/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:
fe98ddcfcf/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 https://github.com/rust-lang/rust/issues/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
This commit is contained in:
Jonathan Brouwer
2026-01-22 13:35:42 +01:00
committed by GitHub
2 changed files with 26 additions and 4 deletions

View File

@@ -1563,11 +1563,22 @@ impl<'v> RootCollector<'_, 'v> {
// If we're collecting items eagerly, then recurse into all constants.
// Otherwise the value is only collected when explicitly mentioned in other items.
if self.strategy == MonoItemCollectionStrategy::Eager {
if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization()
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
{
collect_const_value(self.tcx, val, self.output);
let def_id = id.owner_id.to_def_id();
// Type Consts don't have bodies to evaluate
// nor do they make sense as a static.
if self.tcx.is_type_const(def_id) {
// FIXME(mgca): Is this actually what we want? We may want to
// normalize to a ValTree then convert to a const allocation and
// collect that?
return;
}
if self.tcx.generics_of(id.owner_id).own_requires_monomorphization() {
return;
}
let Ok(val) = self.tcx.const_eval_poly(def_id) else {
return;
};
collect_const_value(self.tcx, val, self.output);
}
}
DefKind::Impl { of_trait: true } => {

View File

@@ -0,0 +1,11 @@
//@ check-pass
//@compile-flags: -Clink-dead-code=true
// link-dead-code tries to eagerly monomorphize and collect items
// This lead to collector.rs to try and evaluate a type_const
// which will fail since they do not have bodies.
#![expect(incomplete_features)]
#![feature(min_generic_const_args)]
#[type_const]
const TYPE_CONST: usize = 0;
fn main() {}