Skip to content

Commit 35fcd9d

Browse files
Always evaluate free lifetime-generic constants
Co-authored-by: Michael Goulet <[email protected]>
1 parent f97b3c6 commit 35fcd9d

File tree

5 files changed

+5
-26
lines changed

5 files changed

+5
-26
lines changed

compiler/rustc_hir_analysis/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
215215
tcx.ensure_ok().eval_static_initializer(item_def_id);
216216
check::maybe_check_static_with_link_section(tcx, item_def_id);
217217
}
218-
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
218+
DefKind::Const if !tcx.generics_of(item_def_id).own_requires_monomorphization() => {
219+
// FIXME(generic_const_items): Passing empty instead of identity args is fishy but
220+
// seems to be fine for now. Revisit this!
219221
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
220222
let cid = GlobalId { instance, promoted: None };
221223
let typing_env = ty::TypingEnv::fully_monomorphized();

compiler/rustc_middle/src/mir/interpret/queries.rs

-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use super::{
99
ReportedErrorInfo,
1010
};
1111
use crate::mir;
12-
use crate::query::TyCtxtEnsureOk;
1312
use crate::ty::{self, GenericArgs, TyCtxt, TypeVisitableExt};
1413

1514
impl<'tcx> TyCtxt<'tcx> {
@@ -197,24 +196,3 @@ impl<'tcx> TyCtxt<'tcx> {
197196
}
198197
}
199198
}
200-
201-
impl<'tcx> TyCtxtEnsureOk<'tcx> {
202-
/// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
203-
/// that can't take any generic arguments like const items or enum discriminants. If a
204-
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
205-
#[instrument(skip(self), level = "debug")]
206-
pub fn const_eval_poly(self, def_id: DefId) {
207-
// In some situations def_id will have generic parameters within scope, but they aren't allowed
208-
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
209-
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
210-
// encountered.
211-
let args = GenericArgs::identity_for_item(self.tcx, def_id);
212-
let instance = ty::Instance::new(def_id, self.tcx.erase_regions(args));
213-
let cid = GlobalId { instance, promoted: None };
214-
let typing_env = ty::TypingEnv::post_analysis(self.tcx, def_id);
215-
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
216-
// improve caching of queries.
217-
let inputs = self.tcx.erase_regions(typing_env.as_query_input(cid));
218-
self.eval_to_const_value_raw(inputs)
219-
}
220-
}

compiler/rustc_monomorphize/src/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ impl<'v> RootCollector<'_, 'v> {
14651465

14661466
// But even just declaring them must collect the items they refer to
14671467
// unless their generics require monomorphization.
1468-
if !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
1468+
if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization()
14691469
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
14701470
{
14711471
collect_const_value(self.tcx, val, self.output);

tests/ui/generic-const-items/def-site-eval.fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: evaluation of `_::<'_>` failed
2-
--> $DIR/def-site-eval.rs:14:20
2+
--> $DIR/def-site-eval.rs:13:20
33
|
44
LL | const _<'_a>: () = panic!();
55
| ^^^^^^^^ evaluation panicked: explicit panic

tests/ui/generic-const-items/def-site-eval.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![allow(incomplete_features)]
55

66
//@ revisions: fail pass
7-
//@[fail] build-fail (we require monomorphization)
87
//@[pass] build-pass (we require monomorphization)
98

109
const _<_T>: () = panic!();

0 commit comments

Comments
 (0)