Always evaluate free lifetime-generic constants

Co-authored-by: Michael Goulet <michael@errs.io>
This commit is contained in:
León Orell Valerian Liehr 2025-05-02 20:22:13 +02:00
parent 642e49bfed
commit a83f8d02ea
No known key found for this signature in database
GPG key ID: D17A07215F68E713
4 changed files with 9 additions and 8 deletions

View file

@ -203,7 +203,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
tcx.ensure_ok().eval_static_initializer(item_def_id);
check::maybe_check_static_with_link_section(tcx, item_def_id);
}
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
DefKind::Const if !tcx.generics_of(item_def_id).own_requires_monomorphization() => {
// FIXME(generic_const_items): Passing empty instead of identity args is fishy but
// seems to be fine for now. Revisit this!
let instance = ty::Instance::new_raw(item_def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None };
let typing_env = ty::TypingEnv::fully_monomorphized();

View file

@ -1483,7 +1483,7 @@ impl<'v> RootCollector<'_, 'v> {
// But even just declaring them must collect the items they refer to
// unless their generics require monomorphization.
if !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
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);

View file

@ -1,5 +1,5 @@
error[E0080]: evaluation of `_::<'_>` failed
--> $DIR/def-site-eval.rs:14:20
error[E0080]: evaluation of constant value failed
--> $DIR/def-site-eval.rs:13:20
|
LL | const _<'_a>: () = panic!();
| ^^^^^^^^ evaluation panicked: explicit panic

View file

@ -1,16 +1,15 @@
//! Test that we only evaluate free const items (their def site to be clear)
//! whose generics don't require monomorphization.
#![feature(generic_const_items)]
#![allow(incomplete_features)]
#![expect(incomplete_features)]
//@ revisions: fail pass
//@[fail] build-fail (we require monomorphization)
//@[pass] build-pass (we require monomorphization)
//@[pass] check-pass
const _<_T>: () = panic!();
const _<const _N: usize>: () = panic!();
#[cfg(fail)]
const _<'_a>: () = panic!(); //[fail]~ ERROR evaluation of `_::<'_>` failed
const _<'_a>: () = panic!(); //[fail]~ ERROR evaluation of constant value failed
fn main() {}