Skip to content

Commit b590e30

Browse files
committed
Refactor generic collection.
1 parent 6857a8d commit b590e30

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
299299
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
300300

301301
let (generics, decl) =
302-
this.add_in_band_defs(generics, fn_def_id, |this, idty| {
302+
this.add_implicit_generics(generics, fn_def_id, |this, idty| {
303303
let ret_id = asyncness.opt_return_id();
304304
this.lower_fn_decl(&decl, Some((id, idty)), FnDeclKind::Fn, ret_id)
305305
});
@@ -417,7 +417,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
417417
// parent lifetime.
418418
let lowered_trait_def_id = hir_id.expect_owner();
419419
let (generics, (trait_ref, lowered_ty)) =
420-
self.add_in_band_defs(ast_generics, lowered_trait_def_id, |this, _| {
420+
self.add_implicit_generics(ast_generics, lowered_trait_def_id, |this, _| {
421421
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
422422
this.lower_trait_ref(
423423
trait_ref,
@@ -743,7 +743,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
743743
ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => {
744744
let fdec = &sig.decl;
745745
let (generics, (fn_dec, fn_args)) =
746-
self.add_in_band_defs(generics, def_id, |this, _| {
746+
self.add_implicit_generics(generics, def_id, |this, _| {
747747
(
748748
// Disallow `impl Trait` in foreign items.
749749
this.lower_fn_decl(fdec, None, FnDeclKind::ExternFn, None),
@@ -1345,7 +1345,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13451345
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
13461346
let fn_def_id = self.resolver.local_def_id(id);
13471347
let header = self.lower_fn_header(sig.header);
1348-
let (generics, decl) = self.add_in_band_defs(generics, fn_def_id, |this, idty| {
1348+
let (generics, decl) = self.add_implicit_generics(generics, fn_def_id, |this, idty| {
13491349
this.lower_fn_decl(&sig.decl, Some((id, idty)), kind, is_async)
13501350
});
13511351
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })

compiler/rustc_ast_lowering/src/lib.rs

+33-52
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub enum LifetimeRes {
173173
Fresh {
174174
/// Id of the generic parameter that introduced it.
175175
param: LocalDefId,
176-
/// Id to create the HirId.
176+
/// Id to create the HirId. This is used when creating the `Fresh` lifetime parameters.
177177
introducer: Option<NodeId>,
178178
/// Id of the introducing place. See `Param`.
179179
binder: NodeId,
@@ -693,27 +693,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
693693
Ident::new(ident.name, self.lower_span(ident.span))
694694
}
695695

696-
/// Creates a new `hir::GenericParam` for every new lifetime and
697-
/// type parameter encountered while evaluating `f`. Definitions
698-
/// are created with the parent provided. If no `parent_id` is
699-
/// provided, no definitions will be returned.
700-
fn collect_in_band_defs<T>(
701-
&mut self,
702-
parent_def_id: LocalDefId,
703-
f: impl FnOnce(&mut Self) -> T,
704-
) -> (FxIndexMap<NodeId, Span>, T) {
705-
let lifetime_stash = std::mem::take(&mut self.lifetimes_to_define);
706-
let was_collecting =
707-
std::mem::replace(&mut self.is_collecting_anonymous_lifetimes, Some(parent_def_id));
708-
709-
let res = f(self);
710-
711-
self.is_collecting_anonymous_lifetimes = was_collecting;
712-
let lifetimes_to_define = std::mem::replace(&mut self.lifetimes_to_define, lifetime_stash);
713-
714-
(lifetimes_to_define, res)
715-
}
716-
717696
/// Converts a lifetime into a new generic parameter.
718697
fn fresh_lifetime_to_generic_param(
719698
&mut self,
@@ -733,9 +712,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
733712
}
734713

735714
/// Evaluates `f` with the lifetimes in `params` in-scope.
736-
/// This is used to track which lifetimes have already been defined, and
737-
/// which are new in-band lifetimes that need to have a definition created
738-
/// for them.
715+
/// This is used to track which lifetimes have already been defined,
716+
/// which need to be duplicated for async fns.
739717
fn with_in_scope_lifetime_defs<T>(
740718
&mut self,
741719
params: &[GenericParam],
@@ -758,37 +736,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
758736
res
759737
}
760738

761-
/// Appends in-band lifetime defs and argument-position `impl
762-
/// Trait` defs to the existing set of generics.
763-
fn add_in_band_defs<T>(
739+
/// Creates a new `hir::GenericParam` for every new `Fresh` lifetime and
740+
/// universal `impl Trait` type parameter encountered while evaluating `f`.
741+
/// Definitions are created with the provided `parent_def_id`.
742+
fn add_implicit_generics<T>(
764743
&mut self,
765744
generics: &Generics,
766745
parent_def_id: LocalDefId,
767746
f: impl FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
768747
) -> (hir::Generics<'hir>, T) {
769-
let (lifetimes_to_define, (mut lowered_generics, impl_trait_defs, res)) = self
770-
.collect_in_band_defs(parent_def_id, |this| {
771-
this.with_in_scope_lifetime_defs(&generics.params, |this| {
772-
let mut impl_trait_defs = Vec::new();
773-
// Note: it is necessary to lower generics *before* calling `f`.
774-
// When lowering `async fn`, there's a final step when lowering
775-
// the return type that assumes that all in-scope lifetimes have
776-
// already been added to either `in_scope_lifetimes` or
777-
// `lifetimes_to_define`. If we swapped the order of these two,
778-
// in-band-lifetimes introduced by generics or where-clauses
779-
// wouldn't have been added yet.
780-
let generics = this.lower_generics_mut(
781-
generics,
782-
ImplTraitContext::Universal(
783-
&mut impl_trait_defs,
784-
this.current_hir_id_owner,
785-
),
786-
);
787-
let res = f(this, &mut impl_trait_defs);
788-
(generics, impl_trait_defs, res)
789-
})
748+
let lifetime_stash = std::mem::take(&mut self.lifetimes_to_define);
749+
let was_collecting =
750+
std::mem::replace(&mut self.is_collecting_anonymous_lifetimes, Some(parent_def_id));
751+
752+
let mut impl_trait_defs = Vec::new();
753+
754+
let (mut lowered_generics, res) =
755+
self.with_in_scope_lifetime_defs(&generics.params, |this| {
756+
// Note: it is necessary to lower generics *before* calling `f`.
757+
// When lowering `async fn`, there's a final step when lowering
758+
// the return type that assumes that all in-scope lifetimes have
759+
// already been added to either `in_scope_lifetimes` or
760+
// `lifetimes_to_define`. If we swapped the order of these two,
761+
// fresh lifetimes introduced by generics or where-clauses
762+
// wouldn't have been added yet.
763+
let generics = this.lower_generics_mut(
764+
generics,
765+
ImplTraitContext::Universal(&mut impl_trait_defs, this.current_hir_id_owner),
766+
);
767+
let res = f(this, &mut impl_trait_defs);
768+
(generics, res)
790769
});
791770

771+
self.is_collecting_anonymous_lifetimes = was_collecting;
772+
let lifetimes_to_define = std::mem::replace(&mut self.lifetimes_to_define, lifetime_stash);
773+
792774
lowered_generics.params.extend(
793775
lifetimes_to_define
794776
.into_iter()
@@ -1700,15 +1682,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17001682
// Calculate all the lifetimes that should be captured
17011683
// by the opaque type. This should include all in-scope
17021684
// lifetime parameters, including those defined in-band.
1703-
//
17041685

17051686
// Input lifetime like `'a`:
17061687
let mut captures = FxHashMap::default();
17071688
for &(p_name, def_id) in &self.in_scope_lifetimes {
17081689
let Ident { name, span } = p_name.ident();
17091690
let node_id = self.resolver.next_node_id();
17101691

1711-
// Add a definition for the in-band lifetime def.
1692+
// Add a definition for the in scope lifetime def.
17121693
self.resolver.create_def(
17131694
opaque_ty_def_id,
17141695
node_id,
@@ -1735,7 +1716,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17351716
let def_id = self.resolver.local_def_id(node_id);
17361717
let new_node_id = self.resolver.next_node_id();
17371718

1738-
// Add a definition for the in-band lifetime def.
1719+
// Add a definition for the `Fresh` lifetime def.
17391720
let new_def_id = self.resolver.create_def(
17401721
opaque_ty_def_id,
17411722
new_node_id,

0 commit comments

Comments
 (0)