From c2683aa5694878120bee5dd8cbb361a04e512ebb Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 15 Mar 2021 11:02:35 +0000 Subject: [PATCH] Explain each variant of TAIT usage with examples --- compiler/rustc_typeck/src/check/check.rs | 45 +++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 5e0a0524690..e2fc1da5c78 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -88,16 +88,59 @@ pub(super) fn check_fn<'a, 'tcx>( let declared_ret_ty = fn_sig.output(); let feature = match tcx.hir().get(fn_id) { + // TAIT usage in function return position. + // Example: + // + // ```rust + // type Foo = impl Debug; + // fn bar() -> Foo { 42 } + // ``` Node::Item(hir::Item { kind: ItemKind::Fn(..), .. }) | + // TAIT usage in associated function return position. + // + // Example with a free type alias: + // + // ```rust + // type Foo = impl Debug; + // impl SomeTrait for SomeType { + // fn bar() -> Foo { 42 } + // } + // ``` + // + // Example with an associated TAIT: + // + // ```rust + // impl SomeTrait for SomeType { + // type Foo = impl Debug; + // fn bar() -> Self::Foo { 42 } + // } + // ``` Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => None, - // I don't know if TAIT uses in trait declarations make sense at all + // Forbid TAIT in trait declarations for now. + // Examples: + // + // ```rust + // type Foo = impl Debug; + // trait Bar { + // fn bar() -> Foo; + // } + // trait Bop { + // type Bop: PartialEq; + // } + // ``` Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. }) | // Forbid TAIT in closure return position for now. + // Example: + // + // ```rust + // type Foo = impl Debug; + // let x = |y| -> Foo { 42 + y }; + // ``` Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => Some(sym::type_alias_impl_trait), node => bug!("Item being checked wasn't a function/closure: {:?}", node), };