Skip to content

LTA: Actually check where-clauses for well-formedness at the def site #136432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

@fmease fmease Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've basically just added check_where_clause and inlined check_item_type. I could've made it work without inlining but that wouldn't've been super nice (it's already quite branch-y and that would've made it worse)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, check_item_type is jank and didn't really make sense since it's otherwise only used for consts/statics.

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 for well-formedness at the definition site.
#![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.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Ensure that we check the predicates at the definition site for well-formedness.
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]

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

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

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/lazy-type-alias/def-site-predicates-wf.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:7:5
|
LL | Vec<str>:;
| ^^^^^^^^ 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:11:15
|
LL | Vec<str>: 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`.
Loading