Skip to content

Commit

Permalink
Fix and test nested impl Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Jan 12, 2020
1 parent 871ee6c commit f2cebdc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/librustc_mir/borrow_check/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// For example consider `fn f<'a>(x: &'a i32) -> impl Sized + 'a { x }`.
/// This is lowered to give HIR something like
///
/// type _Return<'_a> = impl Sized + '_a;
/// fn f<'a>(x: &'a i32) -> _Return<'a> { x }
/// type f<'a>::_Return<'_a> = impl Sized + '_a;
/// fn f<'a>(x: &'a i32) -> f<'static>::_Return<'a> { x }
///
/// When checking the return type record the type from the return and the
/// type used in the return value. In this case they might be `_Return<'1>`
Expand All @@ -34,9 +34,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// `fn f<'a: 'b, 'b: 'a>(x: *mut &'b i32) -> impl Sized + 'a { x }`
///
/// Then we map the regions in both the type and the subst to their
/// `external_name` giving `concrete_type = &'a i32, substs = ['a]`. This
/// will then allow `infer_opaque_definition_from_instantiation` to
/// determine that `_Return<'_a> = &'_a i32`.
/// `external_name` giving `concrete_type = &'a i32`,
/// `substs = ['static, 'a]`. This will then allow
/// `infer_opaque_definition_from_instantiation` to determine that
/// `_Return<'_a> = &'_a i32`.
///
/// There's a slight complication around closures. Given
/// `fn f<'a: 'a>() { || {} }` the closure's type is something like
Expand Down Expand Up @@ -72,6 +73,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
infcx.tcx.lifetimes.re_static
})
}
// We don't fold regions in the predicates of opaque
// types to `ReVar`s. This means that in a case like
//
// fn f<'a: 'a>() -> impl Iterator<Item = impl Sized>
//
// The inner opaque type has `'static` in its substs.
ty::ReStatic => region,
_ => {
infcx.tcx.sess.delay_span_bug(
span,
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/impl-trait/nested-return-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Check that nested impl Trait items work in functions with generic parameters.
// check-pass

trait Captures<'a> {}

impl<T> Captures<'_> for T {}

fn nested_assoc_type<'a: 'a, T>() -> impl Iterator<Item = impl Sized> {
[1].iter()
}

fn nested_assoc_lifetime<'a: 'a, T>() -> impl Iterator<Item = impl Captures<'a>> {
[1].iter()
}

fn main() {}

0 comments on commit f2cebdc

Please sign in to comment.