Skip to content

Commit 83a14a0

Browse files
committed
GCI: Check where-clauses for well-formedness at the def site
1 parent c98f662 commit 83a14a0

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
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>,
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)