Skip to content

Commit 99df5a2

Browse files
Simplify ImplTraitContext
1 parent cf299dd commit 99df5a2

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
275275
}
276276
Some(ty) => this.lower_ty(
277277
ty,
278-
ImplTraitContext::TypeAliasesOpaqueTy {
278+
ImplTraitContext::OpaqueTy {
279279
origin: hir::OpaqueTyOrigin::TyAlias {
280280
parent: this.local_def_id(id),
281281
in_assoc_ty: false,
282282
},
283+
fn_kind: None,
283284
},
284285
),
285286
},
@@ -941,11 +942,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
941942
Some(ty) => {
942943
let ty = this.lower_ty(
943944
ty,
944-
ImplTraitContext::TypeAliasesOpaqueTy {
945+
ImplTraitContext::OpaqueTy {
945946
origin: hir::OpaqueTyOrigin::TyAlias {
946947
parent: this.local_def_id(i.id),
947948
in_assoc_ty: true,
948949
},
950+
fn_kind: None,
949951
},
950952
);
951953
hir::ImplItemKind::Type(ty)

compiler/rustc_ast_lowering/src/lib.rs

+17-26
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,11 @@ enum ImplTraitContext {
265265
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
266266
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
267267
///
268-
ReturnPositionOpaqueTy {
269-
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
270-
origin: hir::OpaqueTyOrigin,
271-
fn_kind: FnDeclKind,
272-
},
273-
/// Impl trait in type aliases.
274-
TypeAliasesOpaqueTy {
275-
/// Origin: Always OpaqueTyOrigin::TypeAliasImplTrait
268+
OpaqueTy {
276269
origin: hir::OpaqueTyOrigin,
270+
/// Only used to change the lifetime capture rules, since
271+
/// RPITIT captures all in scope, RPIT does not.
272+
fn_kind: Option<FnDeclKind>,
277273
},
278274
/// `impl Trait` is unstably accepted in this position.
279275
FeatureGated(ImplTraitPosition, Symbol),
@@ -1078,9 +1074,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10781074
// Disallow ATB in dyn types
10791075
if self.is_in_dyn_type {
10801076
let suggestion = match itctx {
1081-
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1082-
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1083-
| ImplTraitContext::Universal => {
1077+
ImplTraitContext::OpaqueTy { .. } | ImplTraitContext::Universal => {
10841078
let bound_end_span = constraint
10851079
.gen_args
10861080
.as_ref()
@@ -1420,17 +1414,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14201414
TyKind::ImplTrait(def_node_id, bounds) => {
14211415
let span = t.span;
14221416
match itctx {
1423-
ImplTraitContext::ReturnPositionOpaqueTy { origin, fn_kind } => self
1424-
.lower_opaque_impl_trait(
1425-
span,
1426-
origin,
1427-
*def_node_id,
1428-
bounds,
1429-
Some(fn_kind),
1430-
itctx,
1431-
),
1432-
ImplTraitContext::TypeAliasesOpaqueTy { origin } => self
1433-
.lower_opaque_impl_trait(span, origin, *def_node_id, bounds, None, itctx),
1417+
ImplTraitContext::OpaqueTy { origin, fn_kind } => self.lower_opaque_impl_trait(
1418+
span,
1419+
origin,
1420+
*def_node_id,
1421+
bounds,
1422+
fn_kind,
1423+
itctx,
1424+
),
14341425
ImplTraitContext::Universal => {
14351426
let span = t.span;
14361427

@@ -1824,9 +1815,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18241815
FnDeclKind::Fn
18251816
| FnDeclKind::Inherent
18261817
| FnDeclKind::Trait
1827-
| FnDeclKind::Impl => ImplTraitContext::ReturnPositionOpaqueTy {
1818+
| FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
18281819
origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)),
1829-
fn_kind: kind,
1820+
fn_kind: Some(kind),
18301821
},
18311822
FnDeclKind::ExternFn => {
18321823
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
@@ -1920,9 +1911,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19201911
output,
19211912
coro,
19221913
opaque_ty_span,
1923-
ImplTraitContext::ReturnPositionOpaqueTy {
1914+
ImplTraitContext::OpaqueTy {
19241915
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
1925-
fn_kind,
1916+
fn_kind: Some(fn_kind),
19261917
},
19271918
);
19281919
arena_vec![this; bound]

compiler/rustc_ast_lowering/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
423423
// fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
424424
// // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
425425
// ```
426-
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::ReturnPositionOpaqueTy { .. }) => {
426+
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
427427
if self.tcx.features().impl_trait_in_fn_trait_return {
428428
self.lower_ty(ty, itctx)
429429
} else {

0 commit comments

Comments
 (0)