Skip to content

Commit 3f34812

Browse files
committed
Auto merge of rust-lang#136429 - fmease:gci-fix-def-site-checks, r=<try>
GCI: At their def site, actually wfcheck the where-clause & always eval free lifetime-generic constants 1st commit: Partially addresses rust-lang#136204 by turning const eval errors from post to pre-mono for free lifetime-generic constants. Re. 2nd commit: Oof, yeah, I missed that in the initial impl! This doesn't fully address rust-lang#136204 because I still haven't figured out how & where to properly & best suppress const eval of free constants whose predicates don't hold at the def site. The motivating example is `#![feature(trivial_bounds)] const _UNUSED: () = () where String: Copy;` which can also be found over at the tracking issue rust-lang#113521. r? compiler-errors or reassign
2 parents 7c96085 + dabc874 commit 3f34812

File tree

8 files changed

+63
-37
lines changed

8 files changed

+63
-37
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,9 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
268268
check_item_fn(tcx, def_id, ident, item.span, sig.decl)
269269
}
270270
hir::ItemKind::Static(_, ty, ..) => {
271-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
272-
}
273-
hir::ItemKind::Const(_, ty, ..) => {
274-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
271+
check_static_item(tcx, def_id, ty.span, UnsizedHandling::Forbid)
275272
}
273+
hir::ItemKind::Const(_, ty, ..) => check_const_item(tcx, def_id, ty.span, item.span),
276274
hir::ItemKind::Struct(_, _, hir_generics) => {
277275
let res = check_type_defn(tcx, item, false);
278276
check_variances_for_type_defn(tcx, item, hir_generics);
@@ -335,7 +333,7 @@ fn check_foreign_item<'tcx>(
335333
check_item_fn(tcx, def_id, item.ident, item.span, sig.decl)
336334
}
337335
hir::ForeignItemKind::Static(ty, ..) => {
338-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::AllowIfForeignTail)
336+
check_static_item(tcx, def_id, ty.span, UnsizedHandling::AllowIfForeignTail)
339337
}
340338
hir::ForeignItemKind::Type => Ok(()),
341339
}
@@ -1300,14 +1298,13 @@ enum UnsizedHandling {
13001298
AllowIfForeignTail,
13011299
}
13021300

1303-
fn check_item_type(
1301+
#[instrument(level = "debug", skip(tcx, ty_span, unsized_handling))]
1302+
fn check_static_item(
13041303
tcx: TyCtxt<'_>,
13051304
item_id: LocalDefId,
13061305
ty_span: Span,
13071306
unsized_handling: UnsizedHandling,
13081307
) -> Result<(), ErrorGuaranteed> {
1309-
debug!("check_item_type: {:?}", item_id);
1310-
13111308
enter_wf_checking_ctxt(tcx, ty_span, item_id, |wfcx| {
13121309
let ty = tcx.type_of(item_id).instantiate_identity();
13131310
let item_ty = wfcx.normalize(ty_span, Some(WellFormedLoc::Ty(item_id)), ty);
@@ -1357,6 +1354,34 @@ fn check_item_type(
13571354
})
13581355
}
13591356

1357+
fn check_const_item(
1358+
tcx: TyCtxt<'_>,
1359+
def_id: LocalDefId,
1360+
ty_span: Span,
1361+
item_span: Span,
1362+
) -> Result<(), ErrorGuaranteed> {
1363+
enter_wf_checking_ctxt(tcx, ty_span, def_id, |wfcx| {
1364+
let ty = tcx.type_of(def_id).instantiate_identity();
1365+
let ty = wfcx.normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty);
1366+
1367+
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into());
1368+
wfcx.register_bound(
1369+
traits::ObligationCause::new(
1370+
ty_span,
1371+
wfcx.body_def_id,
1372+
ObligationCauseCode::SizedConstOrStatic,
1373+
),
1374+
wfcx.param_env,
1375+
ty,
1376+
tcx.require_lang_item(LangItem::Sized, None),
1377+
);
1378+
1379+
check_where_clauses(wfcx, item_span, def_id);
1380+
1381+
Ok(())
1382+
})
1383+
}
1384+
13601385
#[instrument(level = "debug", skip(tcx, hir_self_ty, hir_trait_ref))]
13611386
fn check_impl<'tcx>(
13621387
tcx: TyCtxt<'tcx>,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error[E0080]: evaluation of `_::<'_>` failed
2-
--> $DIR/def-site-eval.rs:14:20
1+
error[E0080]: evaluation of constant value failed
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
#![allow(incomplete_features)]
55

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

109
const _<_T>: () = panic!();
1110
const _<const _N: usize>: () = panic!();
1211

1312
#[cfg(fail)]
14-
const _<'_a>: () = panic!(); //[fail]~ ERROR evaluation of `_::<'_>` failed
13+
const _<'_a>: () = panic!(); //[fail]~ ERROR evaluation of constant value failed
1514

1615
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Ensure that we check the predicates for well-formedness at the definition site.
2+
#![feature(generic_const_items)]
3+
#![allow(incomplete_features)]
4+
5+
const _: () = ()
6+
where
7+
Vec<str>: Sized; //~ ERROR the size for values of type `str` cannot be known at compilation time
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/def-site-predicates-wf.rs:7:15
3+
|
4+
LL | Vec<str>: Sized;
5+
| ^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `str`
8+
note: required by an implicit `Sized` bound in `Vec`
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)