Skip to content

Commit f89196e

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

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
307307
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
308308
}
309309
hir::ItemKind::Const(ty, ..) => {
310-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
310+
check_const_item(tcx, def_id, ty.span, item.span)
311311
}
312312
hir::ItemKind::Struct(_, hir_generics) => {
313313
let res = check_type_defn(tcx, item, false);
@@ -1280,6 +1280,7 @@ enum UnsizedHandling {
12801280
AllowIfForeignTail,
12811281
}
12821282

1283+
// FIXME(fmease): Rename to check_static_item once LTAs don't use it anymore (#136432)
12831284
fn check_item_type(
12841285
tcx: TyCtxt<'_>,
12851286
item_id: LocalDefId,
@@ -1338,6 +1339,34 @@ fn check_item_type(
13381339
})
13391340
}
13401341

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