Skip to content

Commit d85720a

Browse files
committed
Document lower_opaque_impl_trait
1 parent 6289d0e commit d85720a

File tree

1 file changed

+29
-0
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+29
-0
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,35 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13491349
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
13501350
}
13511351

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.
13521381
#[tracing::instrument(level = "debug", skip(self))]
13531382
fn lower_opaque_impl_trait(
13541383
&mut self,

0 commit comments

Comments
 (0)