Skip to content

Commit 6f11489

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 613bdd4 + 9f2fdff commit 6f11489

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
306306
hir::ItemKind::Static(ty, ..) => {
307307
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
308308
}
309-
hir::ItemKind::Const(ty, ..) => {
310-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
311-
}
309+
hir::ItemKind::Const(ty, ..) => check_const_item(tcx, def_id, ty.span, item.span),
312310
hir::ItemKind::Struct(_, hir_generics) => {
313311
let res = check_type_defn(tcx, item, false);
314312
check_variances_for_type_defn(tcx, item, hir_generics);
@@ -1280,6 +1278,7 @@ enum UnsizedHandling {
12801278
AllowIfForeignTail,
12811279
}
12821280

1281+
// FIXME(fmease): Rename to check_static_item once LTAs don't use it anymore (#136432)
12831282
fn check_item_type(
12841283
tcx: TyCtxt<'_>,
12851284
item_id: LocalDefId,
@@ -1338,6 +1337,34 @@ fn check_item_type(
13381337
})
13391338
}
13401339

1340+
fn check_const_item(
1341+
tcx: TyCtxt<'_>,
1342+
def_id: LocalDefId,
1343+
ty_span: Span,
1344+
item_span: Span,
1345+
) -> Result<(), ErrorGuaranteed> {
1346+
enter_wf_checking_ctxt(tcx, ty_span, def_id, |wfcx| {
1347+
let ty = tcx.type_of(def_id).instantiate_identity();
1348+
let ty = wfcx.normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty);
1349+
1350+
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into());
1351+
wfcx.register_bound(
1352+
traits::ObligationCause::new(
1353+
ty_span,
1354+
wfcx.body_def_id,
1355+
ObligationCauseCode::WellFormed(None),
1356+
),
1357+
wfcx.param_env,
1358+
ty,
1359+
tcx.require_lang_item(LangItem::Sized, None),
1360+
);
1361+
1362+
check_where_clauses(wfcx, item_span, def_id);
1363+
1364+
Ok(())
1365+
})
1366+
}
1367+
13411368
#[instrument(level = "debug", skip(tcx, hir_self_ty, hir_trait_ref))]
13421369
fn check_impl<'tcx>(
13431370
tcx: TyCtxt<'tcx>,

compiler/rustc_hir_analysis/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ use rustc_abi::ExternAbi;
9797
use rustc_hir as hir;
9898
use rustc_hir::def::DefKind;
9999
use rustc_middle::middle;
100-
use rustc_middle::mir::interpret::GlobalId;
101100
use rustc_middle::query::Providers;
102-
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
101+
use rustc_middle::ty::{Const, Ty, TyCtxt};
103102
use rustc_span::{ErrorGuaranteed, Span};
104103
use rustc_trait_selection::traits;
105104

@@ -172,11 +171,8 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
172171
let def_kind = tcx.def_kind(item_def_id);
173172
match def_kind {
174173
DefKind::Static { .. } => tcx.ensure_ok().eval_static_initializer(item_def_id),
175-
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
176-
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
177-
let cid = GlobalId { instance, promoted: None };
178-
let typing_env = ty::TypingEnv::fully_monomorphized();
179-
tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
174+
DefKind::Const if !tcx.generics_of(item_def_id).requires_monomorphization(tcx) => {
175+
tcx.ensure_ok().const_eval_poly(item_def_id.into())
180176
}
181177
_ => (),
182178
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'tcx> TyCtxtEnsureOk<'tcx> {
209209
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
210210
// encountered.
211211
let args = GenericArgs::identity_for_item(self.tcx, def_id);
212-
let instance = ty::Instance::new(def_id, self.tcx.erase_regions(args));
212+
let instance = ty::Instance::new(def_id, args);
213213
let cid = GlobalId { instance, promoted: None };
214214
let typing_env = ty::TypingEnv::post_analysis(self.tcx, def_id);
215215
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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!();
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/def-site-eval.rs:14:20
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/def-site-eval.rs:13:20
66
|
77
= 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)
88

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!();
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)