Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3d6eae8

Browse files
committed
Move defs and bounds from Universal to LoweringContext
1 parent 4a8d2e3 commit 3d6eae8

File tree

3 files changed

+60
-87
lines changed

3 files changed

+60
-87
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8383
task_context: None,
8484
current_item: None,
8585
captured_lifetimes: None,
86+
impl_trait_defs: Vec::new(),
87+
impl_trait_bounds: Vec::new(),
8688
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
8789
allow_gen_future: Some([sym::gen_future][..].into()),
8890
allow_into_future: Some([sym::into_future][..].into()),
@@ -264,16 +266,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
264266
let body_id =
265267
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
266268

267-
let (generics, decl) =
268-
this.add_implicit_generics(generics, id, |this, idty, idpb| {
269-
let ret_id = asyncness.opt_return_id();
270-
this.lower_fn_decl(
271-
&decl,
272-
Some((id, idty, idpb)),
273-
FnDeclKind::Fn,
274-
ret_id,
275-
)
276-
});
269+
let (generics, decl) = this.add_implicit_generics(generics, id, |this| {
270+
let ret_id = asyncness.opt_return_id();
271+
this.lower_fn_decl(&decl, Some(id), FnDeclKind::Fn, ret_id)
272+
});
277273
let sig = hir::FnSig {
278274
decl,
279275
header: this.lower_fn_header(header),
@@ -387,7 +383,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
387383
// lifetime to be added, but rather a reference to a
388384
// parent lifetime.
389385
let (generics, (trait_ref, lowered_ty)) =
390-
self.add_implicit_generics(ast_generics, id, |this, _, _| {
386+
self.add_implicit_generics(ast_generics, id, |this| {
391387
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
392388
this.lower_trait_ref(
393389
trait_ref,
@@ -652,7 +648,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
652648
ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => {
653649
let fdec = &sig.decl;
654650
let (generics, (fn_dec, fn_args)) =
655-
self.add_implicit_generics(generics, i.id, |this, _, _| {
651+
self.add_implicit_generics(generics, i.id, |this| {
656652
(
657653
// Disallow `impl Trait` in foreign items.
658654
this.lower_fn_decl(fdec, None, FnDeclKind::ExternFn, None),
@@ -1231,8 +1227,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12311227
is_async: Option<NodeId>,
12321228
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
12331229
let header = self.lower_fn_header(sig.header);
1234-
let (generics, decl) = self.add_implicit_generics(generics, id, |this, idty, idpb| {
1235-
this.lower_fn_decl(&sig.decl, Some((id, idty, idpb)), kind, is_async)
1230+
let (generics, decl) = self.add_implicit_generics(generics, id, |this| {
1231+
this.lower_fn_decl(&sig.decl, Some(id), kind, is_async)
12361232
});
12371233
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
12381234
}
@@ -1292,7 +1288,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12921288
pub(super) fn lower_generics_mut(
12931289
&mut self,
12941290
generics: &Generics,
1295-
mut itctx: ImplTraitContext<'_, 'hir>,
1291+
mut itctx: ImplTraitContext,
12961292
) -> GenericsCtor<'hir> {
12971293
// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
12981294
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
@@ -1372,7 +1368,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13721368
pub(super) fn lower_generics(
13731369
&mut self,
13741370
generics: &Generics,
1375-
itctx: ImplTraitContext<'_, 'hir>,
1371+
itctx: ImplTraitContext,
13761372
) -> &'hir hir::Generics<'hir> {
13771373
let generics_ctor = self.lower_generics_mut(generics, itctx);
13781374
generics_ctor.into_generics(self.arena)

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 45 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ struct LoweringContext<'a, 'hir: 'a> {
121121
local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
122122
trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
123123

124+
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
125+
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
126+
124127
/// NodeIds that are lowered inside the current HIR owner.
125128
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
126129

@@ -244,13 +247,13 @@ pub trait ResolverAstLowering {
244247
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
245248
/// and if so, what meaning it has.
246249
#[derive(Debug)]
247-
enum ImplTraitContext<'b, 'a> {
250+
enum ImplTraitContext {
248251
/// Treat `impl Trait` as shorthand for a new universal generic parameter.
249252
/// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
250253
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
251254
///
252255
/// Newly generated parameters should be inserted into the given `Vec`.
253-
Universal(&'b mut Vec<hir::GenericParam<'a>>, &'b mut Vec<hir::WherePredicate<'a>>, LocalDefId),
256+
Universal(LocalDefId),
254257

255258
/// Treat `impl Trait` as shorthand for a new opaque type.
256259
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
@@ -290,11 +293,11 @@ enum ImplTraitPosition {
290293
ImplReturn,
291294
}
292295

293-
impl<'a> ImplTraitContext<'_, 'a> {
294-
fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
296+
impl ImplTraitContext {
297+
fn reborrow<'this>(&'this mut self) -> ImplTraitContext {
295298
use self::ImplTraitContext::*;
296299
match self {
297-
Universal(params, bounds, parent) => Universal(params, bounds, *parent),
300+
Universal(parent) => Universal(*parent),
298301
ReturnPositionOpaqueTy { origin } => ReturnPositionOpaqueTy { origin: *origin },
299302
TypeAliasesOpaqueTy => TypeAliasesOpaqueTy,
300303
Disallowed(pos) => Disallowed(*pos),
@@ -701,34 +704,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
701704
&mut self,
702705
generics: &Generics,
703706
parent_node_id: NodeId,
704-
f: impl FnOnce(
705-
&mut Self,
706-
&mut Vec<hir::GenericParam<'hir>>,
707-
&mut Vec<hir::WherePredicate<'hir>>,
708-
) -> T,
707+
f: impl FnOnce(&mut Self) -> T,
709708
) -> (&'hir hir::Generics<'hir>, T) {
710-
let mut impl_trait_defs = Vec::new();
711-
let mut impl_trait_bounds = Vec::new();
712-
let mut lowered_generics = self.lower_generics_mut(
713-
generics,
714-
ImplTraitContext::Universal(
715-
&mut impl_trait_defs,
716-
&mut impl_trait_bounds,
717-
self.current_hir_id_owner,
718-
),
719-
);
720-
let res = f(self, &mut impl_trait_defs, &mut impl_trait_bounds);
709+
let mut lowered_generics = self
710+
.lower_generics_mut(generics, ImplTraitContext::Universal(self.current_hir_id_owner));
711+
let res = f(self);
721712

722713
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
714+
let impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
723715
lowered_generics.params.extend(
724716
extra_lifetimes
725717
.into_iter()
726718
.filter_map(|(ident, node_id, res)| {
727719
self.lifetime_res_to_generic_param(ident, node_id, res)
728720
})
729-
.chain(impl_trait_defs),
721+
.chain(impl_trait_defs.into_iter()),
730722
);
731-
lowered_generics.predicates.extend(impl_trait_bounds);
723+
let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
724+
lowered_generics.predicates.extend(impl_trait_bounds.into_iter());
732725

733726
let lowered_generics = lowered_generics.into_generics(self.arena);
734727
(lowered_generics, res)
@@ -898,7 +891,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
898891
fn lower_assoc_ty_constraint(
899892
&mut self,
900893
constraint: &AssocConstraint,
901-
mut itctx: ImplTraitContext<'_, 'hir>,
894+
mut itctx: ImplTraitContext,
902895
) -> hir::TypeBinding<'hir> {
903896
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
904897

@@ -962,7 +955,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
962955
// so desugar to
963956
//
964957
// fn foo(x: dyn Iterator<Item = impl Debug>)
965-
ImplTraitContext::Universal(_, _, parent) if self.is_in_dyn_type => {
958+
ImplTraitContext::Universal(parent) if self.is_in_dyn_type => {
966959
parent_def_id = parent;
967960
(true, itctx)
968961
}
@@ -1036,7 +1029,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10361029
fn lower_generic_arg(
10371030
&mut self,
10381031
arg: &ast::GenericArg,
1039-
itctx: ImplTraitContext<'_, 'hir>,
1032+
itctx: ImplTraitContext,
10401033
) -> hir::GenericArg<'hir> {
10411034
match arg {
10421035
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
@@ -1103,7 +1096,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11031096
}
11041097
}
11051098

1106-
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_, 'hir>) -> &'hir hir::Ty<'hir> {
1099+
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> &'hir hir::Ty<'hir> {
11071100
self.arena.alloc(self.lower_ty_direct(t, itctx))
11081101
}
11091102

@@ -1113,7 +1106,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11131106
qself: &Option<QSelf>,
11141107
path: &Path,
11151108
param_mode: ParamMode,
1116-
itctx: ImplTraitContext<'_, 'hir>,
1109+
itctx: ImplTraitContext,
11171110
) -> hir::Ty<'hir> {
11181111
let id = self.lower_node_id(t.id);
11191112
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
@@ -1128,7 +1121,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11281121
self.ty(span, hir::TyKind::Tup(tys))
11291122
}
11301123

1131-
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_, 'hir>) -> hir::Ty<'hir> {
1124+
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext) -> hir::Ty<'hir> {
11321125
let kind = match t.kind {
11331126
TyKind::Infer => hir::TyKind::Infer,
11341127
TyKind::Err => hir::TyKind::Err,
@@ -1235,40 +1228,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12351228
|this| this.lower_param_bounds(bounds, nested_itctx),
12361229
)
12371230
}
1238-
ImplTraitContext::Universal(
1239-
in_band_ty_params,
1240-
in_band_ty_bounds,
1241-
parent_def_id,
1242-
) => {
1231+
ImplTraitContext::Universal(parent_def_id) => {
12431232
// Add a definition for the in-band `Param`.
12441233
let def_id = self.resolver.local_def_id(def_node_id);
12451234

1246-
let hir_bounds = self.lower_param_bounds(
1247-
bounds,
1248-
ImplTraitContext::Universal(
1249-
in_band_ty_params,
1250-
in_band_ty_bounds,
1251-
parent_def_id,
1252-
),
1253-
);
1235+
let hir_bounds = self
1236+
.lower_param_bounds(bounds, ImplTraitContext::Universal(parent_def_id));
12541237
// Set the name to `impl Bound1 + Bound2`.
12551238
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
1256-
in_band_ty_params.push(hir::GenericParam {
1239+
let param = hir::GenericParam {
12571240
hir_id: self.lower_node_id(def_node_id),
12581241
name: ParamName::Plain(self.lower_ident(ident)),
12591242
pure_wrt_drop: false,
12601243
span: self.lower_span(span),
12611244
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
12621245
colon_span: None,
1263-
});
1246+
};
1247+
self.impl_trait_defs.push(param);
1248+
12641249
if let Some(preds) = self.lower_generic_bound_predicate(
12651250
ident,
12661251
def_node_id,
12671252
&GenericParamKind::Type { default: None },
12681253
hir_bounds,
12691254
hir::PredicateOrigin::ImplTrait,
12701255
) {
1271-
in_band_ty_bounds.push(preds)
1256+
self.impl_trait_bounds.push(preds)
12721257
}
12731258

12741259
hir::TyKind::Path(hir::QPath::Resolved(
@@ -1442,21 +1427,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14421427
fn lower_fn_decl(
14431428
&mut self,
14441429
decl: &FnDecl,
1445-
mut in_band_ty_params: Option<(
1446-
NodeId,
1447-
&mut Vec<hir::GenericParam<'hir>>,
1448-
&mut Vec<hir::WherePredicate<'hir>>,
1449-
)>,
1430+
fn_node_id: Option<NodeId>,
14501431
kind: FnDeclKind,
14511432
make_ret_async: Option<NodeId>,
14521433
) -> &'hir hir::FnDecl<'hir> {
14531434
debug!(
14541435
"lower_fn_decl(\
14551436
fn_decl: {:?}, \
1456-
in_band_ty_params: {:?}, \
1437+
fn_node_id: {:?}, \
14571438
kind: {:?}, \
14581439
make_ret_async: {:?})",
1459-
decl, in_band_ty_params, kind, make_ret_async,
1440+
decl, fn_node_id, kind, make_ret_async,
14601441
);
14611442

14621443
let c_variadic = decl.c_variadic();
@@ -1469,10 +1450,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14691450
inputs = &inputs[..inputs.len() - 1];
14701451
}
14711452
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
1472-
if let Some((_, ibty, ibpb)) = &mut in_band_ty_params {
1453+
if fn_node_id.is_some() {
14731454
self.lower_ty_direct(
14741455
&param.ty,
1475-
ImplTraitContext::Universal(ibty, ibpb, self.current_hir_id_owner),
1456+
ImplTraitContext::Universal(self.current_hir_id_owner),
14761457
)
14771458
} else {
14781459
self.lower_ty_direct(
@@ -1494,15 +1475,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14941475
let output = if let Some(ret_id) = make_ret_async {
14951476
self.lower_async_fn_ret_ty(
14961477
&decl.output,
1497-
in_band_ty_params.expect("`make_ret_async` but no `fn_def_id`").0,
1478+
fn_node_id.expect("`make_ret_async` but no `fn_def_id`"),
14981479
ret_id,
14991480
)
15001481
} else {
15011482
match decl.output {
15021483
FnRetTy::Ty(ref ty) => {
1503-
let context = match in_band_ty_params {
1504-
Some((node_id, _, _)) if kind.impl_trait_return_allowed() => {
1505-
let fn_def_id = self.resolver.local_def_id(node_id);
1484+
let context = match fn_node_id {
1485+
Some(fn_node_id) if kind.impl_trait_return_allowed() => {
1486+
let fn_def_id = self.resolver.local_def_id(fn_node_id);
15061487
ImplTraitContext::ReturnPositionOpaqueTy {
15071488
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
15081489
}
@@ -1788,7 +1769,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17881769
fn lower_param_bound(
17891770
&mut self,
17901771
tpb: &GenericBound,
1791-
itctx: ImplTraitContext<'_, 'hir>,
1772+
itctx: ImplTraitContext,
17921773
) -> hir::GenericBound<'hir> {
17931774
match tpb {
17941775
GenericBound::Trait(p, modifier) => hir::GenericBound::Trait(
@@ -1966,11 +1947,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19661947
}
19671948
}
19681949

1969-
fn lower_trait_ref(
1970-
&mut self,
1971-
p: &TraitRef,
1972-
itctx: ImplTraitContext<'_, 'hir>,
1973-
) -> hir::TraitRef<'hir> {
1950+
fn lower_trait_ref(&mut self, p: &TraitRef, itctx: ImplTraitContext) -> hir::TraitRef<'hir> {
19741951
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
19751952
hir::QPath::Resolved(None, path) => path,
19761953
qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
@@ -1982,7 +1959,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19821959
fn lower_poly_trait_ref(
19831960
&mut self,
19841961
p: &PolyTraitRef,
1985-
mut itctx: ImplTraitContext<'_, 'hir>,
1962+
mut itctx: ImplTraitContext,
19861963
) -> hir::PolyTraitRef<'hir> {
19871964
let bound_generic_params = self.lower_generic_params(&p.bound_generic_params);
19881965

@@ -1993,22 +1970,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19931970
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
19941971
}
19951972

1996-
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext<'_, 'hir>) -> hir::MutTy<'hir> {
1973+
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy<'hir> {
19971974
hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
19981975
}
19991976

20001977
fn lower_param_bounds(
20011978
&mut self,
20021979
bounds: &[GenericBound],
2003-
itctx: ImplTraitContext<'_, 'hir>,
1980+
itctx: ImplTraitContext,
20041981
) -> hir::GenericBounds<'hir> {
20051982
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
20061983
}
20071984

20081985
fn lower_param_bounds_mut<'s>(
20091986
&'s mut self,
20101987
bounds: &'s [GenericBound],
2011-
mut itctx: ImplTraitContext<'s, 'hir>,
1988+
mut itctx: ImplTraitContext,
20121989
) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> {
20131990
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx.reborrow()))
20141991
}

0 commit comments

Comments
 (0)