Simplify ImplTraitContext

This commit is contained in:
Michael Goulet 2024-03-06 18:44:55 +00:00
parent cf299ddb6e
commit 99df5a2b42
3 changed files with 22 additions and 29 deletions

View File

@ -275,11 +275,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
} }
Some(ty) => this.lower_ty( Some(ty) => this.lower_ty(
ty, ty,
ImplTraitContext::TypeAliasesOpaqueTy { ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::TyAlias { origin: hir::OpaqueTyOrigin::TyAlias {
parent: this.local_def_id(id), parent: this.local_def_id(id),
in_assoc_ty: false, in_assoc_ty: false,
}, },
fn_kind: None,
}, },
), ),
}, },
@ -941,11 +942,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(ty) => { Some(ty) => {
let ty = this.lower_ty( let ty = this.lower_ty(
ty, ty,
ImplTraitContext::TypeAliasesOpaqueTy { ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::TyAlias { origin: hir::OpaqueTyOrigin::TyAlias {
parent: this.local_def_id(i.id), parent: this.local_def_id(i.id),
in_assoc_ty: true, in_assoc_ty: true,
}, },
fn_kind: None,
}, },
); );
hir::ImplItemKind::Type(ty) hir::ImplItemKind::Type(ty)

View File

@ -265,15 +265,11 @@ enum ImplTraitContext {
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`. /// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
/// ///
ReturnPositionOpaqueTy { OpaqueTy {
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
origin: hir::OpaqueTyOrigin,
fn_kind: FnDeclKind,
},
/// Impl trait in type aliases.
TypeAliasesOpaqueTy {
/// Origin: Always OpaqueTyOrigin::TypeAliasImplTrait
origin: hir::OpaqueTyOrigin, origin: hir::OpaqueTyOrigin,
/// Only used to change the lifetime capture rules, since
/// RPITIT captures all in scope, RPIT does not.
fn_kind: Option<FnDeclKind>,
}, },
/// `impl Trait` is unstably accepted in this position. /// `impl Trait` is unstably accepted in this position.
FeatureGated(ImplTraitPosition, Symbol), FeatureGated(ImplTraitPosition, Symbol),
@ -1078,9 +1074,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Disallow ATB in dyn types // Disallow ATB in dyn types
if self.is_in_dyn_type { if self.is_in_dyn_type {
let suggestion = match itctx { let suggestion = match itctx {
ImplTraitContext::ReturnPositionOpaqueTy { .. } ImplTraitContext::OpaqueTy { .. } | ImplTraitContext::Universal => {
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
| ImplTraitContext::Universal => {
let bound_end_span = constraint let bound_end_span = constraint
.gen_args .gen_args
.as_ref() .as_ref()
@ -1420,17 +1414,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
TyKind::ImplTrait(def_node_id, bounds) => { TyKind::ImplTrait(def_node_id, bounds) => {
let span = t.span; let span = t.span;
match itctx { match itctx {
ImplTraitContext::ReturnPositionOpaqueTy { origin, fn_kind } => self ImplTraitContext::OpaqueTy { origin, fn_kind } => self.lower_opaque_impl_trait(
.lower_opaque_impl_trait(
span, span,
origin, origin,
*def_node_id, *def_node_id,
bounds, bounds,
Some(fn_kind), fn_kind,
itctx, itctx,
), ),
ImplTraitContext::TypeAliasesOpaqueTy { origin } => self
.lower_opaque_impl_trait(span, origin, *def_node_id, bounds, None, itctx),
ImplTraitContext::Universal => { ImplTraitContext::Universal => {
let span = t.span; let span = t.span;
@ -1824,9 +1815,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
FnDeclKind::Fn FnDeclKind::Fn
| FnDeclKind::Inherent | FnDeclKind::Inherent
| FnDeclKind::Trait | FnDeclKind::Trait
| FnDeclKind::Impl => ImplTraitContext::ReturnPositionOpaqueTy { | FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)), origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)),
fn_kind: kind, fn_kind: Some(kind),
}, },
FnDeclKind::ExternFn => { FnDeclKind::ExternFn => {
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn) ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
@ -1920,9 +1911,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
output, output,
coro, coro,
opaque_ty_span, opaque_ty_span,
ImplTraitContext::ReturnPositionOpaqueTy { ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
fn_kind, fn_kind: Some(fn_kind),
}, },
); );
arena_vec![this; bound] arena_vec![this; bound]

View File

@ -423,7 +423,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug // fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
// // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^ // // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
// ``` // ```
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::ReturnPositionOpaqueTy { .. }) => { FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
if self.tcx.features().impl_trait_in_fn_trait_return { if self.tcx.features().impl_trait_in_fn_trait_return {
self.lower_ty(ty, itctx) self.lower_ty(ty, itctx)
} else { } else {