Skip to content

Commit 689b791

Browse files
committed
use approx. bounds to decide whether to add outlives obligations
Before, if we had a projection like `<T as Foo<'0>>::Bar: 'x` and a where clause like `<T as Foo<'a>>::Bar: 'a`, we considered those to have nothing to do with one another. Therefore, we would use the "overconstrained" path of adding `T: 'x` and `'0: 'x` requirements. We now do a "fuzzy" match where we erase regions first and hence we see the env bound `'a`.
1 parent 4b193cb commit 689b791

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

src/librustc/infer/outlives/verify.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ impl<'cx, 'gcx, 'tcx> VerifyBoundCx<'cx, 'gcx, 'tcx> {
8989
&self,
9090
projection_ty: ty::ProjectionTy<'tcx>,
9191
) -> Vec<ty::Region<'tcx>> {
92-
self.declared_generic_bounds_from_env(GenericKind::Projection(projection_ty))
92+
let projection_ty = GenericKind::Projection(projection_ty).to_ty(self.tcx);
93+
let erased_projection_ty = self.tcx.erase_regions(&projection_ty);
94+
self.declared_generic_bounds_from_env_with_compare_fn(
95+
|ty| if let ty::Projection(..) = ty.sty {
96+
let erased_ty = self.tcx.erase_regions(&ty);
97+
erased_ty == erased_projection_ty
98+
} else {
99+
false
100+
},
101+
)
93102
}
94103

95104
/// Searches the where clauses in scope for regions that

src/test/ui/nll/ty-outlives/projection-one-region-closure.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ where
8585
// can do better here with a more involved verification step.
8686

8787
with_signature(cell, t, |cell, t| require(cell, t));
88-
//~^ ERROR the parameter type `T` may not live long enough
89-
//~| ERROR
88+
//~^ ERROR
9089
}
9190

9291
#[rustc_regions]

src/test/ui/nll/ty-outlives/projection-one-region-closure.stderr

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
119119
extern "rust-call" fn((std::cell::Cell<&'_#3r ()>, T))
120120
]
121121
= note: number of external vids: 4
122-
= note: where T: '_#3r
123-
= note: where '_#2r: '_#3r
122+
= note: where <T as Anything<ReClosureBound('_#2r)>>::AssocType: '_#3r
124123

125124
note: No external requirements
126125
--> $DIR/projection-one-region-closure.rs:72:1
@@ -130,7 +129,7 @@ LL | | where
130129
LL | | T: Anything<'b>,
131130
LL | | T::AssocType: 'a,
132131
... |
133-
LL | | //~| ERROR
132+
LL | | //~^ ERROR
134133
LL | | }
135134
| |_^
136135
|
@@ -140,27 +139,16 @@ LL | | }
140139
T
141140
]
142141

143-
error[E0309]: the parameter type `T` may not live long enough
142+
error[E0309]: the associated type `<T as Anything<'_#5r>>::AssocType` may not live long enough
144143
--> $DIR/projection-one-region-closure.rs:87:29
145144
|
146145
LL | with_signature(cell, t, |cell, t| require(cell, t));
147146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
148147
|
149-
= help: consider adding an explicit lifetime bound `T: ReEarlyBound(0, 'a)`...
150-
151-
error: unsatisfied lifetime constraints
152-
--> $DIR/projection-one-region-closure.rs:87:29
153-
|
154-
LL | fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
155-
| -- -- lifetime `'b` defined here
156-
| |
157-
| lifetime `'a` defined here
158-
...
159-
LL | with_signature(cell, t, |cell, t| require(cell, t));
160-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ closure body requires that `'b` must outlive `'a`
148+
= help: consider adding an explicit lifetime bound `<T as Anything<'_#5r>>::AssocType: ReEarlyBound(0, 'a)`...
161149

162150
note: External requirements
163-
--> $DIR/projection-one-region-closure.rs:99:29
151+
--> $DIR/projection-one-region-closure.rs:98:29
164152
|
165153
LL | with_signature(cell, t, |cell, t| require(cell, t));
166154
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -177,7 +165,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
177165
= note: where '_#2r: '_#3r
178166

179167
note: No external requirements
180-
--> $DIR/projection-one-region-closure.rs:93:1
168+
--> $DIR/projection-one-region-closure.rs:92:1
181169
|
182170
LL | / fn elements_outlive<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
183171
LL | | where
@@ -194,6 +182,6 @@ LL | | }
194182
T
195183
]
196184

197-
error: aborting due to 6 previous errors
185+
error: aborting due to 5 previous errors
198186

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

src/test/ui/nll/ty-outlives/projection-one-region-trait-bound-closure.stderr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ LL | with_signature(cell, t, |cell, t| require(cell, t));
101101
extern "rust-call" fn((std::cell::Cell<&'_#3r ()>, T))
102102
]
103103
= note: number of external vids: 4
104-
= note: where '_#2r: '_#3r
104+
= note: where <T as Anything<ReClosureBound('_#2r)>>::AssocType: '_#3r
105105

106106
note: No external requirements
107107
--> $DIR/projection-one-region-trait-bound-closure.rs:62:1
@@ -121,16 +121,13 @@ LL | | }
121121
T
122122
]
123123

124-
error: unsatisfied lifetime constraints
124+
error[E0309]: the associated type `<T as Anything<'_#5r>>::AssocType` may not live long enough
125125
--> $DIR/projection-one-region-trait-bound-closure.rs:77:29
126126
|
127-
LL | fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
128-
| -- -- lifetime `'b` defined here
129-
| |
130-
| lifetime `'a` defined here
131-
...
132127
LL | with_signature(cell, t, |cell, t| require(cell, t));
133-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ closure body requires that `'b` must outlive `'a`
128+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
129+
|
130+
= help: consider adding an explicit lifetime bound `<T as Anything<'_#5r>>::AssocType: ReEarlyBound(0, 'a)`...
134131

135132
note: External requirements
136133
--> $DIR/projection-one-region-trait-bound-closure.rs:87:29
@@ -200,3 +197,4 @@ LL | | }
200197

201198
error: aborting due to 3 previous errors
202199

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

0 commit comments

Comments
 (0)