Skip to content

Commit 809121f

Browse files
Check elaborated projections from dyn don't mention unconstrained late bound lifetimes
1 parent 5fe0e40 commit 809121f

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+50
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
174174
// Include projections defined on supertraits.
175175
projection_bounds.push((pred, span));
176176
}
177+
178+
self.check_elaborated_projection_mentions_input_lifetimes(pred, span);
177179
}
178180
_ => (),
179181
}
@@ -358,6 +360,54 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
358360

359361
Ty::new_dynamic(tcx, existential_predicates, region_bound, representation)
360362
}
363+
364+
/// Check that elaborating the principal of a trait ref doesn't lead to projections
365+
/// that are unconstrained. This can happen because an otherwise unconstrained
366+
/// *type variable* can be substituted with a type that has late-bound regions. See
367+
/// `elaborated-predicates-unconstrained-late-bound.rs` for a test.
368+
fn check_elaborated_projection_mentions_input_lifetimes(
369+
&self,
370+
pred: ty::PolyProjectionPredicate<'tcx>,
371+
span: Span,
372+
) {
373+
let tcx = self.tcx();
374+
375+
// Find any late-bound regions declared in `ty` that are not
376+
// declared in the trait-ref or assoc_item. These are not well-formed.
377+
//
378+
// Example:
379+
//
380+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
381+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
382+
let late_bound_in_projection_term =
383+
tcx.collect_constrained_late_bound_regions(pred.map_bound(|pred| pred.projection_term));
384+
let late_bound_in_term =
385+
tcx.collect_referenced_late_bound_regions(pred.map_bound(|pred| pred.term));
386+
debug!(?late_bound_in_projection_term);
387+
debug!(?late_bound_in_term);
388+
389+
// FIXME: point at the type params that don't have appropriate lifetimes:
390+
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
391+
// ---- ---- ^^^^^^^
392+
// NOTE(associated_const_equality): This error should be impossible to trigger
393+
// with associated const equality constraints.
394+
self.validate_late_bound_regions(
395+
late_bound_in_projection_term,
396+
late_bound_in_term,
397+
|br_name| {
398+
let item_name = tcx.item_name(pred.projection_def_id());
399+
struct_span_code_err!(
400+
self.dcx(),
401+
span,
402+
E0582,
403+
"binding for associated type `{}` references {}, \
404+
which does not appear in the trait input types",
405+
item_name,
406+
br_name
407+
)
408+
},
409+
);
410+
}
361411
}
362412

363413
fn replace_dummy_self_with_error<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Make sure that when elaborating the principal of a dyn trait for projection predicates
2+
// we don't end up in a situation where we have an unconstrained late-bound lifetime in
3+
// the output of a projection.
4+
5+
// Fix for <https://github.com/rust-lang/rust/issues/130347>.
6+
7+
trait A<T>: B<T = T> {}
8+
9+
trait B {
10+
type T;
11+
}
12+
13+
struct Erase<T: ?Sized + B>(T::T);
14+
15+
fn main() {
16+
let x = {
17+
let x = String::from("hello");
18+
19+
Erase::<dyn for<'a> A<&'a _>>(x.as_str())
20+
//~^ ERROR binding for associated type `T` references lifetime `'a`, which does not appear in the trait input types
21+
};
22+
23+
dbg!(x.0);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0582]: binding for associated type `T` references lifetime `'a`, which does not appear in the trait input types
2+
--> $DIR/elaborated-predicates-unconstrained-late-bound.rs:19:21
3+
|
4+
LL | Erase::<dyn for<'a> A<&'a _>>(x.as_str())
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0582`.

tests/ui/traits/object/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait SuperGeneric<'a> {
1313
}
1414
trait AnyGeneric<'a>: SuperGeneric<'a> {}
1515
trait FixedGeneric1<'a>: SuperGeneric<'a, Assoc2 = &'a u8> {}
16-
trait FixedGeneric2<'a>: Super<Assoc = &'a u8> {}
16+
// trait FixedGeneric2<'a>: Super<Assoc = &'a u8> {} // Unsound!
1717
trait FixedHrtb: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> {}
1818
trait AnyDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super {}
1919
trait FixedDifferentBinders: for<'a> SuperGeneric<'a, Assoc2 = &'a u8> + Super<Assoc = u8> {}
@@ -32,7 +32,7 @@ fn dyn_fixed_static(x: &dyn FixedStatic) { x } //~ERROR mismatched types
3232
fn dyn_super_generic(x: &dyn for<'a> SuperGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
3333
fn dyn_any_generic(x: &dyn for<'a> AnyGeneric<'a, Assoc2 = &'a u8>) { x } //~ERROR mismatched types
3434
fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x } //~ERROR mismatched types
35-
fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x } //~ERROR mismatched types
35+
// fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x } // Unsound!
3636
fn dyn_fixed_generic_multi(x: &dyn for<'a> FixedGeneric1<'a, Assoc2 = &u8>) { x } //~ERROR mismatched types
3737
fn dyn_fixed_hrtb(x: &dyn FixedHrtb) { x } //~ERROR mismatched types
3838
fn dyn_any_different_binders(x: &dyn AnyDifferentBinders<Assoc = u8>) { x } //~ERROR mismatched types

tests/ui/traits/object/pretty.stderr

+1-12
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,6 @@ LL | fn dyn_fixed_generic1(x: &dyn for<'a> FixedGeneric1<'a>) { x }
106106
= note: expected unit type `()`
107107
found reference `&dyn for<'a> FixedGeneric1<'a>`
108108

109-
error[E0308]: mismatched types
110-
--> $DIR/pretty.rs:35:60
111-
|
112-
LL | fn dyn_fixed_generic2(x: &dyn for<'a> FixedGeneric2<'a>) { x }
113-
| - ^ expected `()`, found `&dyn FixedGeneric2<'a>`
114-
| |
115-
| help: try adding a return type: `-> &dyn for<'a> FixedGeneric2<'a>`
116-
|
117-
= note: expected unit type `()`
118-
found reference `&dyn for<'a> FixedGeneric2<'a>`
119-
120109
error[E0308]: mismatched types
121110
--> $DIR/pretty.rs:36:79
122111
|
@@ -172,6 +161,6 @@ LL | fn dyn_has_gat(x: &dyn HasGat<u8, Assoc<bool> = ()>) { x }
172161
= note: expected unit type `()`
173162
found reference `&dyn HasGat<u8, Assoc<bool> = ()>`
174163

175-
error: aborting due to 15 previous errors; 1 warning emitted
164+
error: aborting due to 14 previous errors; 1 warning emitted
176165

177166
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)