Skip to content

Commit 037ab1d

Browse files
committed
Remove generics_def_id_map from the resolver.
1 parent 84f0c3f commit 037ab1d

File tree

4 files changed

+39
-58
lines changed

4 files changed

+39
-58
lines changed

compiler/rustc_ast_lowering/src/item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8585
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
8686
allow_gen_future: Some([sym::gen_future][..].into()),
8787
allow_into_future: Some([sym::into_future][..].into()),
88+
generics_def_id_map: Default::default(),
8889
};
8990
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
9091

compiler/rustc_ast_lowering/src/lib.rs

+38-45
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ struct LoweringContext<'a, 'hir> {
132132
allow_try_trait: Option<Lrc<[Symbol]>>,
133133
allow_gen_future: Option<Lrc<[Symbol]>>,
134134
allow_into_future: Option<Lrc<[Symbol]>>,
135+
136+
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
137+
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
138+
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
139+
/// field from the original parameter 'a to the new parameter 'a1.
140+
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
135141
}
136142

137143
trait ResolverAstLoweringExt {
@@ -142,12 +148,6 @@ trait ResolverAstLoweringExt {
142148
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
143149
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
144150
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
145-
/// Record the map from `from` local def id to `to` local def id, on `generics_def_id_map`
146-
/// field.
147-
fn record_def_id_remap(&mut self, from: LocalDefId, to: LocalDefId);
148-
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
149-
/// `generics_def_id_map` field.
150-
fn get_remapped_def_id(&self, local_def_id: LocalDefId) -> LocalDefId;
151151
}
152152

153153
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -215,41 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
215215
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
216216
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
217217
}
218-
219-
/// Push a remapping into the top-most map.
220-
/// Panics if no map has been pushed.
221-
/// Remapping is used when creating lowering `-> impl Trait` return
222-
/// types to create the resulting opaque type.
223-
#[instrument(level = "debug", skip(self))]
224-
fn record_def_id_remap(&mut self, from: LocalDefId, to: LocalDefId) {
225-
self.generics_def_id_map.last_mut().expect("no map pushed").insert(from, to);
226-
}
227-
228-
fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
229-
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
230-
// push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
231-
//
232-
// Consider:
233-
//
234-
// `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
235-
//
236-
// We would end with a generics_def_id_map like:
237-
//
238-
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
239-
//
240-
// for the opaque type generated on `impl Sized + 'b`, We want the result to be:
241-
// impl_sized#'b, so iterating forward is the wrong thing to do.
242-
for map in self.generics_def_id_map.iter().rev() {
243-
if let Some(r) = map.get(&local_def_id) {
244-
debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
245-
local_def_id = *r;
246-
} else {
247-
debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
248-
}
249-
}
250-
251-
local_def_id
252-
}
253218
}
254219

255220
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -522,13 +487,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
522487
self.resolver
523488
.node_id_to_def_id
524489
.get(&node)
525-
.map(|local_def_id| self.resolver.get_remapped_def_id(*local_def_id))
490+
.map(|local_def_id| self.get_remapped_def_id(*local_def_id))
526491
}
527492

528493
fn local_def_id(&self, node: NodeId) -> LocalDefId {
529494
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
530495
}
531496

497+
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
498+
/// `generics_def_id_map` field.
499+
fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
500+
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
501+
// push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
502+
//
503+
// Consider:
504+
//
505+
// `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
506+
//
507+
// We would end with a generics_def_id_map like:
508+
//
509+
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
510+
//
511+
// for the opaque type generated on `impl Sized + 'b`, We want the result to be:
512+
// impl_sized#'b, so iterating forward is the wrong thing to do.
513+
for map in self.generics_def_id_map.iter().rev() {
514+
if let Some(r) = map.get(&local_def_id) {
515+
debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
516+
local_def_id = *r;
517+
} else {
518+
debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
519+
}
520+
}
521+
522+
local_def_id
523+
}
524+
532525
/// Freshen the `LoweringContext` and ready it to lower a nested item.
533526
/// The lowered item is registered into `self.children`.
534527
///
@@ -597,9 +590,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
597590
remap: FxHashMap<LocalDefId, LocalDefId>,
598591
f: impl FnOnce(&mut Self) -> R,
599592
) -> R {
600-
self.resolver.generics_def_id_map.push(remap);
593+
self.generics_def_id_map.push(remap);
601594
let res = f(self);
602-
self.resolver.generics_def_id_map.pop();
595+
self.generics_def_id_map.pop();
603596
res
604597
}
605598

@@ -2024,7 +2017,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20242017
let name = match res {
20252018
LifetimeRes::Param { param, .. } => {
20262019
let p_name = ParamName::Plain(ident);
2027-
let param = self.resolver.get_remapped_def_id(param);
2020+
let param = self.get_remapped_def_id(param);
20282021

20292022
hir::LifetimeName::Param(param, p_name)
20302023
}

compiler/rustc_middle/src/ty/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ pub struct ResolverAstLowering {
178178
pub label_res_map: NodeMap<ast::NodeId>,
179179
/// Resolutions for lifetimes.
180180
pub lifetimes_res_map: NodeMap<LifetimeRes>,
181-
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
182-
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
183-
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
184-
/// field from the original parameter 'a to the new parameter 'a1.
185-
pub generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
186181
/// Lifetime parameters that lowering will have to introduce.
187182
pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, LifetimeRes)>>,
188183

compiler/rustc_resolve/src/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,6 @@ pub struct Resolver<'a> {
911911
label_res_map: NodeMap<NodeId>,
912912
/// Resolutions for lifetimes.
913913
lifetimes_res_map: NodeMap<LifetimeRes>,
914-
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
915-
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
916-
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
917-
/// field from the original parameter 'a to the new parameter 'a1.
918-
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
919914
/// Lifetime parameters that lowering will have to introduce.
920915
extra_lifetime_params_map: NodeMap<Vec<(Ident, NodeId, LifetimeRes)>>,
921916

@@ -1278,7 +1273,6 @@ impl<'a> Resolver<'a> {
12781273
import_res_map: Default::default(),
12791274
label_res_map: Default::default(),
12801275
lifetimes_res_map: Default::default(),
1281-
generics_def_id_map: Vec::new(),
12821276
extra_lifetime_params_map: Default::default(),
12831277
extern_crate_map: Default::default(),
12841278
reexport_map: FxHashMap::default(),
@@ -1445,7 +1439,6 @@ impl<'a> Resolver<'a> {
14451439
import_res_map: self.import_res_map,
14461440
label_res_map: self.label_res_map,
14471441
lifetimes_res_map: self.lifetimes_res_map,
1448-
generics_def_id_map: self.generics_def_id_map,
14491442
extra_lifetime_params_map: self.extra_lifetime_params_map,
14501443
next_node_id: self.next_node_id,
14511444
node_id_to_def_id: self.node_id_to_def_id,
@@ -1490,7 +1483,6 @@ impl<'a> Resolver<'a> {
14901483
import_res_map: self.import_res_map.clone(),
14911484
label_res_map: self.label_res_map.clone(),
14921485
lifetimes_res_map: self.lifetimes_res_map.clone(),
1493-
generics_def_id_map: self.generics_def_id_map.clone(),
14941486
extra_lifetime_params_map: self.extra_lifetime_params_map.clone(),
14951487
next_node_id: self.next_node_id.clone(),
14961488
node_id_to_def_id: self.node_id_to_def_id.clone(),

0 commit comments

Comments
 (0)