@@ -1349,6 +1349,35 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1349
1349
hir:: Ty { kind, span : self . lower_span ( t. span ) , hir_id : self . lower_node_id ( t. id ) }
1350
1350
}
1351
1351
1352
+ /// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
1353
+ /// impl Trait`): this creates the associated Opaque Type (TAIT) definition and then returns a
1354
+ /// HIR type that references the TAIT.
1355
+ ///
1356
+ /// Given a function definition like:
1357
+ ///
1358
+ /// ```rust
1359
+ /// fn test<'a, T: Debug>(x: &'a T) -> impl Debug + 'a {
1360
+ /// x
1361
+ /// }
1362
+ /// ```
1363
+ ///
1364
+ /// we will create a TAIT definition in the HIR like
1365
+ ///
1366
+ /// ```
1367
+ /// type TestReturn<'a, T, 'x> = impl Debug + 'x
1368
+ /// ```
1369
+ ///
1370
+ /// and return a type like `TestReturn<'static, T, 'a>`, so that the function looks like:
1371
+ ///
1372
+ /// ```rust
1373
+ /// fn test<'a, T: Debug>(x: &'a T) -> TestReturn<'static, T, 'a>
1374
+ /// ```
1375
+ ///
1376
+ /// Note the subtely around type parameters! The new TAIT, `TestReturn`, inherits all the
1377
+ /// type parameters from the function `test` (this is implemented in the query layer, they aren't
1378
+ /// added explicitly in the HIR). But this includes all the lifetimes, and we only want to
1379
+ /// capture the lifetimes that are referenced in the bounds. Therefore, we add *extra* lifetime parameters
1380
+ /// for the lifetimes that get captured (`'x`, in our example above) and reference those.
1352
1381
#[ tracing:: instrument( level = "debug" , skip( self ) ) ]
1353
1382
fn lower_opaque_impl_trait (
1354
1383
& mut self ,
0 commit comments