Skip to content

Commit

Permalink
LTA: Check where-clauses for well-formedness at the def site
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Feb 2, 2025
1 parent e08cd3c commit e268763
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 11 deletions.
24 changes: 13 additions & 11 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,20 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
// `ForeignItem`s are handled separately.
hir::ItemKind::ForeignMod { .. } => Ok(()),
hir::ItemKind::TyAlias(hir_ty, hir_generics) => {
if tcx.type_alias_is_lazy(item.owner_id) {
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
let res = check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
check_variances_for_type_defn(tcx, item, hir_generics);
res
} else {
hir::ItemKind::TyAlias(hir_ty, hir_generics) if tcx.type_alias_is_lazy(item.owner_id) => {
let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
let ty = tcx.type_of(def_id).instantiate_identity();
let item_ty = wfcx.normalize(hir_ty.span, Some(WellFormedLoc::Ty(def_id)), ty);
wfcx.register_wf_obligation(
hir_ty.span,
Some(WellFormedLoc::Ty(def_id)),
item_ty.into(),
);
check_where_clauses(wfcx, item.span, def_id);
Ok(())
}
});
check_variances_for_type_defn(tcx, item, hir_generics);
res
}
_ => Ok(()),
};
Expand Down Expand Up @@ -1276,7 +1280,6 @@ fn check_item_fn(

enum UnsizedHandling {
Forbid,
Allow,
AllowIfForeignTail,
}

Expand All @@ -1294,7 +1297,6 @@ fn check_item_type(

let forbid_unsized = match unsized_handling {
UnsizedHandling::Forbid => true,
UnsizedHandling::Allow => false,
UnsizedHandling::AllowIfForeignTail => {
let tail =
tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/lazy-type-alias/def-site-param-defaults-wf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Ensure that we check generic parameter defaults at the definition site for well-formedness.
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]

type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
//~^ ERROR evaluation of constant value failed
//~| ERROR the size for values of type `str` cannot be known at compilation time

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/lazy-type-alias/def-site-param-defaults-wf.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0080]: evaluation of constant value failed
--> $DIR/def-site-param-defaults-wf.rs:5:44
|
LL | type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/def-site-param-defaults-wf.rs:5:16
|
LL | type Alias<T = Vec<str>, const N: usize = {0 - 1}> = T;
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0080, E0277.
For more information about an error, try `rustc --explain E0080`.
13 changes: 13 additions & 0 deletions tests/ui/lazy-type-alias/def-site-predicates-wf.current.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/def-site-predicates-wf.rs:11:5
|
LL | <Vec<str> as Discard>::Output:;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
23 changes: 23 additions & 0 deletions tests/ui/lazy-type-alias/def-site-predicates-wf.next.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/def-site-predicates-wf.rs:11:5
|
LL | <Vec<str> as Discard>::Output:;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/def-site-predicates-wf.rs:16:36
|
LL | <Vec<str> as Discard>::Output: Sized;
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
21 changes: 21 additions & 0 deletions tests/ui/lazy-type-alias/def-site-predicates-wf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Ensure that we check the predicates at the definition site for well-formedness.
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]

//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@ ignore-compare-mode-next-solver (explicit revisions)

type Alias0 = ()
where
<Vec<str> as Discard>::Output:; //~ ERROR the size for values of type `str` cannot be known at compilation time

// FIXME(#100041): This should error in the current solver, too.
type Alias1 = ()
where
<Vec<str> as Discard>::Output: Sized; //[next]~ ERROR the size for values of type `str` cannot be known at compilation time

trait Discard { type Output; }
impl<T> Discard for T { type Output = (); }

fn main() {}

0 comments on commit e268763

Please sign in to comment.