Skip to content

Commit e268763

Browse files
committed
LTA: Check where-clauses for well-formedness at the def site
1 parent e08cd3c commit e268763

File tree

6 files changed

+99
-11
lines changed

6 files changed

+99
-11
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,20 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
328328
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
329329
// `ForeignItem`s are handled separately.
330330
hir::ItemKind::ForeignMod { .. } => Ok(()),
331-
hir::ItemKind::TyAlias(hir_ty, hir_generics) => {
332-
if tcx.type_alias_is_lazy(item.owner_id) {
333-
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
334-
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
335-
let res = check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
336-
check_variances_for_type_defn(tcx, item, hir_generics);
337-
res
338-
} else {
331+
hir::ItemKind::TyAlias(hir_ty, hir_generics) if tcx.type_alias_is_lazy(item.owner_id) => {
332+
let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
333+
let ty = tcx.type_of(def_id).instantiate_identity();
334+
let item_ty = wfcx.normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
335+
wfcx.register_wf_obligation(
336+
hir_ty.span,
337+
Some(WellFormedLoc::Ty(def_id)),
338+
item_ty.into(),
339+
);
340+
check_where_clauses(wfcx, item.span, def_id);
339341
Ok(())
340-
}
342+
});
343+
check_variances_for_type_defn(tcx, item, hir_generics);
344+
res
341345
}
342346
_ => Ok(()),
343347
};
@@ -1276,7 +1280,6 @@ fn check_item_fn(
12761280

12771281
enum UnsizedHandling {
12781282
Forbid,
1279-
Allow,
12801283
AllowIfForeignTail,
12811284
}
12821285

@@ -1294,7 +1297,6 @@ fn check_item_type(
12941297

12951298
let forbid_unsized = match unsized_handling {
12961299
UnsizedHandling::Forbid => true,
1297-
UnsizedHandling::Allow => false,
12981300
UnsizedHandling::AllowIfForeignTail => {
12991301
let tail =
13001302
tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Ensure that we check generic parameter defaults at the definition site for well-formedness.
2+
#![feature(lazy_type_alias)]
3+
#![allow(incomplete_features)]
4+
5+
type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
6+
//~^ ERROR evaluation of constant value failed
7+
//~| 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,20 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/def-site-param-defaults-wf.rs:5:44
3+
|
4+
LL | type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
5+
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
6+
7+
error[E0277]: the size for values of type `str` cannot be known at compilation time
8+
--> $DIR/def-site-param-defaults-wf.rs:5:16
9+
|
10+
LL | type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
11+
| ^^^^^^^^ doesn't have a size known at compile-time
12+
|
13+
= help: the trait `Sized` is not implemented for `str`
14+
note: required by an implicit `Sized` bound in `Vec`
15+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
16+
17+
error: aborting due to 2 previous errors
18+
19+
Some errors have detailed explanations: E0080, E0277.
20+
For more information about an error, try `rustc --explain E0080`.
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:11:5
3+
|
4+
LL | <Vec<str> as Discard>::Output:;
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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/def-site-predicates-wf.rs:11:5
3+
|
4+
LL | <Vec<str> as Discard>::Output:;
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[E0277]: the size for values of type `str` cannot be known at compilation time
12+
--> $DIR/def-site-predicates-wf.rs:16:36
13+
|
14+
LL | <Vec<str> as Discard>::Output: Sized;
15+
| ^^^^^ doesn't have a size known at compile-time
16+
|
17+
= help: the trait `Sized` is not implemented for `str`
18+
note: required by an implicit `Sized` bound in `Vec`
19+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Ensure that we check the predicates at the definition site for well-formedness.
2+
#![feature(lazy_type_alias)]
3+
#![allow(incomplete_features)]
4+
5+
//@ revisions: current next
6+
//@[next] compile-flags: -Znext-solver
7+
//@ ignore-compare-mode-next-solver (explicit revisions)
8+
9+
type Alias0 = ()
10+
where
11+
<Vec<str> as Discard>::Output:; //~ ERROR the size for values of type `str` cannot be known at compilation time
12+
13+
// FIXME(#100041): This should error in the current solver, too.
14+
type Alias1 = ()
15+
where
16+
<Vec<str> as Discard>::Output: Sized; //[next]~ ERROR the size for values of type `str` cannot be known at compilation time
17+
18+
trait Discard { type Output; }
19+
impl<T> Discard for T { type Output = (); }
20+
21+
fn main() {}

0 commit comments

Comments
 (0)