Skip to content

Commit d163e5e

Browse files
committed
Auto merge of #123737 - compiler-errors:alias-wf, r=lcnr
Check alias args for WF even if they have escaping bound vars #### What This PR stops skipping arguments of aliases if they have escaping bound vars, instead recursing into them and only discarding the resulting obligations referencing bounds vars. #### An example: From the test: ``` trait Trait { type Gat<U: ?Sized>; } fn test<T>(f: for<'a> fn(<&'a T as Trait>::Gat<&'a [str]>)) where for<'a> &'a T: Trait {} //~^ ERROR the size for values of type `[()]` cannot be known at compilation time fn main() {} ``` We now prove that `str: Sized` in order for `&'a [str]` to be well-formed. We were previously unconditionally skipping over `&'a [str]` as it referenced a buond variable. We now recurse into it and instead only discard the `[str]: 'a` obligation because of the escaping bound vars. #### Why? This is a change that improves consistency about proving well-formedness earlier in the pipeline, which is necessary for future work on where-bounds in binders and correctly handling higher-ranked implied bounds. I don't expect this to fix any unsoundness. #### What doesn't it fix? Specifically, this doesn't check projection predicates' components are well-formed, because there are too many regressions: #123737 (comment)
2 parents 67f0d43 + 994b58f commit d163e5e

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

compiler/rustc_trait_selection/src/traits/wf.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,6 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
435435
}
436436
}
437437

438-
/// Pushes the obligations required for an alias (except inherent) to be WF
439-
/// into `self.out`.
440-
fn compute_alias_ty(&mut self, data: ty::AliasTy<'tcx>) {
441-
self.compute_alias_term(data.into());
442-
}
443-
444438
/// Pushes the obligations required for an alias (except inherent) to be WF
445439
/// into `self.out`.
446440
fn compute_alias_term(&mut self, data: ty::AliasTerm<'tcx>) {
@@ -498,7 +492,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
498492
self.out.extend(obligations);
499493
}
500494

501-
self.compute_projection_args(data.args);
495+
data.args.visit_with(self);
502496
}
503497

504498
fn compute_projection_args(&mut self, args: GenericArgsRef<'tcx>) {
@@ -702,8 +696,8 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
702696
}
703697

704698
ty::Alias(ty::Projection | ty::Opaque | ty::Weak, data) => {
705-
self.compute_alias_ty(data);
706-
return; // Subtree handled by compute_projection.
699+
let obligations = self.nominal_obligations(data.def_id, data.args);
700+
self.out.extend(obligations);
707701
}
708702
ty::Alias(ty::Inherent, data) => {
709703
self.compute_inherent_projection(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait Trait {
2+
type Gat<U: ?Sized>;
3+
}
4+
5+
fn test<T>(f: for<'a> fn(<&'a T as Trait>::Gat<&'a [str]>)) where for<'a> &'a T: Trait {}
6+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/well-formed-aliases.rs:5:52
3+
|
4+
LL | fn test<T>(f: for<'a> fn(<&'a T as Trait>::Gat<&'a [str]>)) where for<'a> &'a T: Trait {}
5+
| ^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `str`
8+
= note: slice and array elements must have `Sized` type
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)