Skip to content

Commit 1ca424c

Browse files
committed
Auto merge of rust-lang#122341 - compiler-errors:alias-wfness, r=lcnr
Consolidate WF for aliases Make RPITs/TAITs/weak (type) aliases/projections all enforce: 1. their nominal predicates 2. their args are WF This possibly does extra work, but is also nice for consistency sake. r? lcnr
2 parents 72d7897 + 04524c8 commit 1ca424c

File tree

6 files changed

+68
-26
lines changed

6 files changed

+68
-26
lines changed

compiler/rustc_trait_selection/src/traits/wf.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn clause_obligations<'tcx>(
164164
wf.compute(ty.into());
165165
}
166166
ty::ClauseKind::Projection(t) => {
167-
wf.compute_projection(t.projection_ty);
167+
wf.compute_alias(t.projection_ty);
168168
wf.compute(match t.term.unpack() {
169169
ty::TermKind::Ty(ty) => ty.into(),
170170
ty::TermKind::Const(c) => c.into(),
@@ -436,9 +436,9 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
436436
}
437437
}
438438

439-
/// Pushes the obligations required for `trait_ref::Item` to be WF
439+
/// Pushes the obligations required for an alias (except inherent) to be WF
440440
/// into `self.out`.
441-
fn compute_projection(&mut self, data: ty::AliasTy<'tcx>) {
441+
fn compute_alias(&mut self, data: ty::AliasTy<'tcx>) {
442442
// A projection is well-formed if
443443
//
444444
// (a) its predicates hold (*)
@@ -466,6 +466,9 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
466466
self.compute_projection_args(data.args);
467467
}
468468

469+
/// Pushes the obligations required for an inherent alias to be WF
470+
/// into `self.out`.
471+
// FIXME(inherent_associated_types): Merge this function with `fn compute_alias`.
469472
fn compute_inherent_projection(&mut self, data: ty::AliasTy<'tcx>) {
470473
// An inherent projection is well-formed if
471474
//
@@ -688,8 +691,8 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
688691
// Simple cases that are WF if their type args are WF.
689692
}
690693

691-
ty::Alias(ty::Projection, data) => {
692-
self.compute_projection(data);
694+
ty::Alias(ty::Projection | ty::Opaque | ty::Weak, data) => {
695+
self.compute_alias(data);
693696
return; // Subtree handled by compute_projection.
694697
}
695698
ty::Alias(ty::Inherent, data) => {
@@ -791,21 +794,6 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
791794
// types appearing in the fn signature.
792795
}
793796

794-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
795-
// All of the requirements on type parameters
796-
// have already been checked for `impl Trait` in
797-
// return position. We do need to check type-alias-impl-trait though.
798-
if self.tcx().is_type_alias_impl_trait(def_id) {
799-
let obligations = self.nominal_obligations(def_id, args);
800-
self.out.extend(obligations);
801-
}
802-
}
803-
804-
ty::Alias(ty::Weak, ty::AliasTy { def_id, args, .. }) => {
805-
let obligations = self.nominal_obligations(def_id, args);
806-
self.out.extend(obligations);
807-
}
808-
809797
ty::Dynamic(data, r, _) => {
810798
// WfObject
811799
//

tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ impl Foo<char> for Bar {
99
//~^ ERROR: the trait bound `impl Foo<u8>: Foo<char>` is not satisfied [E0277]
1010
//~| ERROR: the trait bound `Bar: Foo<u8>` is not satisfied [E0277]
1111
//~| ERROR: impl has stricter requirements than trait
12+
//~| ERROR: the trait bound `F2: Foo<u8>` is not satisfied
1213
self
1314
}
1415
}

tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ note: required by a bound in `Foo::{synthetic#0}`
1111
LL | fn foo<F2>(self) -> impl Foo<T>;
1212
| ^^^^^^ required by this bound in `Foo::{synthetic#0}`
1313

14+
error[E0277]: the trait bound `F2: Foo<u8>` is not satisfied
15+
--> $DIR/return-dont-satisfy-bounds.rs:8:34
16+
|
17+
LL | fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
18+
| ^^^^^^^^^^^^ the trait `Foo<u8>` is not implemented for `F2`
19+
|
20+
note: required by a bound in `<Bar as Foo<char>>::foo`
21+
--> $DIR/return-dont-satisfy-bounds.rs:8:16
22+
|
23+
LL | fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
24+
| ^^^^^^^ required by this bound in `<Bar as Foo<char>>::foo`
25+
help: consider further restricting this bound
26+
|
27+
LL | fn foo<F2: Foo<u8> + Foo<u8>>(self) -> impl Foo<u8> {
28+
| +++++++++
29+
1430
error[E0276]: impl has stricter requirements than trait
1531
--> $DIR/return-dont-satisfy-bounds.rs:8:16
1632
|
@@ -32,7 +48,7 @@ LL | self
3248
= help: the trait `Foo<char>` is implemented for `Bar`
3349
= help: for that trait implementation, expected `char`, found `u8`
3450

35-
error: aborting due to 3 previous errors
51+
error: aborting due to 4 previous errors
3652

3753
Some errors have detailed explanations: E0276, E0277.
3854
For more information about an error, try `rustc --explain E0276`.

tests/ui/lifetimes/issue-76168-hr-outlives-3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ async fn wrapper<F>(f: F)
77
//~^ ERROR: expected a `FnOnce(&'a mut i32)` closure, found `i32`
88
//~| ERROR: expected a `FnOnce(&'a mut i32)` closure, found `i32`
99
//~| ERROR: expected a `FnOnce(&'a mut i32)` closure, found `i32`
10+
//~| ERROR: expected a `FnOnce(&'a mut i32)` closure, found `i32`
1011
where
1112
F:,
1213
for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,

tests/ui/lifetimes/issue-76168-hr-outlives-3.stderr

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | / async fn wrapper<F>(f: F)
55
LL | |
66
LL | |
77
LL | |
8-
LL | | where
8+
... |
99
LL | | F:,
1010
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
1111
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
@@ -27,15 +27,30 @@ LL | / async fn wrapper<F>(f: F)
2727
LL | |
2828
LL | |
2929
LL | |
30-
LL | | where
30+
... |
3131
LL | | F:,
3232
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
3333
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
3434
|
3535
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
3636

3737
error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
38-
--> $DIR/issue-76168-hr-outlives-3.rs:13:1
38+
--> $DIR/issue-76168-hr-outlives-3.rs:6:1
39+
|
40+
LL | / async fn wrapper<F>(f: F)
41+
LL | |
42+
LL | |
43+
LL | |
44+
... |
45+
LL | | F:,
46+
LL | | for<'a> <i32 as FnOnce<(&'a mut i32,)>>::Output: Future<Output = ()> + 'a,
47+
| |__________________________________________________________________________^ expected an `FnOnce(&'a mut i32)` closure, found `i32`
48+
|
49+
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
50+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
51+
52+
error[E0277]: expected a `FnOnce(&'a mut i32)` closure, found `i32`
53+
--> $DIR/issue-76168-hr-outlives-3.rs:14:1
3954
|
4055
LL | / {
4156
LL | |
@@ -46,6 +61,6 @@ LL | | }
4661
|
4762
= help: the trait `for<'a> FnOnce<(&'a mut i32,)>` is not implemented for `i32`
4863

49-
error: aborting due to 4 previous errors
64+
error: aborting due to 5 previous errors
5065

5166
For more information about this error, try `rustc --explain E0277`.

tests/ui/traits/non_lifetime_binders/type-match-with-late-bound.stderr

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ LL | #![feature(non_lifetime_binders)]
77
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
88
= note: `#[warn(incomplete_features)]` on by default
99

10+
error[E0309]: the placeholder type `!1_"F"` may not live long enough
11+
--> $DIR/type-match-with-late-bound.rs:8:1
12+
|
13+
LL | async fn walk2<'a, T: 'a>(_: T)
14+
| ^ -- the placeholder type `!1_"F"` must be valid for the lifetime `'a` as defined here...
15+
| _|
16+
| |
17+
LL | | where
18+
LL | | for<F> F: 'a,
19+
| |_________________^ ...so that the type `F` will meet its required lifetime bounds...
20+
|
21+
note: ...that is required by this bound
22+
--> $DIR/type-match-with-late-bound.rs:10:15
23+
|
24+
LL | for<F> F: 'a,
25+
| ^^
26+
help: consider adding an explicit lifetime bound
27+
|
28+
LL | for<F> F: 'a, !1_"F": 'a
29+
| ~~~~~~~~~~~~
30+
1031
error[E0309]: the placeholder type `!1_"F"` may not live long enough
1132
--> $DIR/type-match-with-late-bound.rs:11:1
1233
|
@@ -35,6 +56,6 @@ help: consider adding an explicit lifetime bound
3556
LL | for<F> F: 'a, !2_"F": 'a
3657
| ~~~~~~~~~~~~
3758

38-
error: aborting due to 2 previous errors; 1 warning emitted
59+
error: aborting due to 3 previous errors; 1 warning emitted
3960

4061
For more information about this error, try `rustc --explain E0309`.

0 commit comments

Comments
 (0)