Skip to content

Commit 0a9ee02

Browse files
committed
GCI: Don't try to collect mono items inside overly generic free const items
1 parent 01a26c0 commit 0a9ee02

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

compiler/rustc_monomorphize/src/collector.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1454,11 +1454,14 @@ impl<'v> RootCollector<'_, 'v> {
14541454
self.output.push(dummy_spanned(MonoItem::Static(def_id)));
14551455
}
14561456
DefKind::Const => {
1457-
// const items only generate mono items if they are
1458-
// actually used somewhere. Just declaring them is insufficient.
1457+
// Const items only generate mono items if they are actually used somewhere.
1458+
// Just declaring them is insufficient.
14591459

1460-
// but even just declaring them must collect the items they refer to
1461-
if let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id()) {
1460+
// But even just declaring them must collect the items they refer to
1461+
// unless their generics require monomorphization.
1462+
if !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
1463+
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
1464+
{
14621465
collect_const_value(self.tcx, val, self.output);
14631466
}
14641467
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: evaluation of `_::<'_>` failed
2+
--> $DIR/def-site-eval.rs:14:20
3+
|
4+
LL | const _<'_a>: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/def-site-eval.rs:14:20
6+
|
7+
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Test that we only evaluate free const items (their def site to be clear)
2+
//! whose generics don't require monomorphization.
3+
#![feature(generic_const_items)]
4+
#![allow(incomplete_features)]
5+
6+
//@ revisions: fail pass
7+
//@[fail] build-fail (we require monomorphization)
8+
//@[pass] build-pass (we require monomorphization)
9+
10+
const _<_T>: () = panic!();
11+
const _<const _N: usize>: () = panic!();
12+
13+
#[cfg(fail)]
14+
const _<'_a>: () = panic!(); //[fail]~ ERROR evaluation of `_::<'_>` failed
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Ensure that we don't try to collect monomorphizeable items inside free const
2+
//! items (their def site to be clear) whose generics require monomorphization.
3+
//!
4+
//! Such items are to be collected at instantiation sites of free consts.
5+
6+
#![feature(generic_const_items)]
7+
#![allow(incomplete_features)]
8+
9+
//@ build-pass (we require monomorphization)
10+
11+
const _IDENTITY<T>: fn(T) -> T = |x| x;
12+
13+
fn main() {}

0 commit comments

Comments
 (0)