Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/const-generics/mgca/type_const-incemental-compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ check-pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a multi-crate test such as:

// crate a
#[type_const]
pub const FOO: usize = 1;

// crate b
fn main() {
    println!("{}", a::FOO);
}

Copy link
Member

@BoxyUwU BoxyUwU Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though that might ICE until #151260 is merged? unsure what the overlap here is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not really see any overlap, you might want to ask @reddevilmidzy about that, but yea such a test would currently ICE. Might be better to make that part of #151260?

//@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() {}
Loading