@@ -174,6 +174,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
174
174
// Include projections defined on supertraits.
175
175
projection_bounds. push ( ( pred, span) ) ;
176
176
}
177
+
178
+ self . check_elaborated_projection_mentions_input_lifetimes ( pred, span) ;
177
179
}
178
180
_ => ( ) ,
179
181
}
@@ -358,6 +360,54 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
358
360
359
361
Ty :: new_dynamic ( tcx, existential_predicates, region_bound, representation)
360
362
}
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
+ }
361
411
}
362
412
363
413
fn replace_dummy_self_with_error < ' tcx , T : TypeFoldable < TyCtxt < ' tcx > > > (
0 commit comments