Skip to content

Commit d26047d

Browse files
authored
Rollup merge of #138977 - oli-obk:invoc-parent-keep-aggregated, r=compiler-errors
Don't deaggregate InvocationParent just to reaggregate it again Also makes it easier to add more things to it in the future (which I am doing in some local experiments, so not really a reason to do this just now, but I think this PR stands on its own).
2 parents cb39217 + 781785d commit d26047d

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

compiler/rustc_resolve/src/def_collector.rs

+15-24
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ pub(crate) fn collect_definitions(
1919
fragment: &AstFragment,
2020
expansion: LocalExpnId,
2121
) {
22-
let InvocationParent { parent_def, impl_trait_context, in_attr } =
23-
resolver.invocation_parents[&expansion];
24-
let mut visitor = DefCollector { resolver, parent_def, expansion, impl_trait_context, in_attr };
22+
let invocation_parent = resolver.invocation_parents[&expansion];
23+
let mut visitor = DefCollector { resolver, expansion, invocation_parent };
2524
fragment.visit_with(&mut visitor);
2625
}
2726

2827
/// Creates `DefId`s for nodes in the AST.
2928
struct DefCollector<'a, 'ra, 'tcx> {
3029
resolver: &'a mut Resolver<'ra, 'tcx>,
31-
parent_def: LocalDefId,
32-
impl_trait_context: ImplTraitContext,
33-
in_attr: bool,
30+
invocation_parent: InvocationParent,
3431
expansion: LocalExpnId,
3532
}
3633

@@ -42,7 +39,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
4239
def_kind: DefKind,
4340
span: Span,
4441
) -> LocalDefId {
45-
let parent_def = self.parent_def;
42+
let parent_def = self.invocation_parent.parent_def;
4643
debug!(
4744
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
4845
node_id, def_kind, parent_def
@@ -60,19 +57,20 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
6057
}
6158

6259
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
63-
let orig_parent_def = mem::replace(&mut self.parent_def, parent_def);
60+
let orig_parent_def = mem::replace(&mut self.invocation_parent.parent_def, parent_def);
6461
f(self);
65-
self.parent_def = orig_parent_def;
62+
self.invocation_parent.parent_def = orig_parent_def;
6663
}
6764

6865
fn with_impl_trait<F: FnOnce(&mut Self)>(
6966
&mut self,
7067
impl_trait_context: ImplTraitContext,
7168
f: F,
7269
) {
73-
let orig_itc = mem::replace(&mut self.impl_trait_context, impl_trait_context);
70+
let orig_itc =
71+
mem::replace(&mut self.invocation_parent.impl_trait_context, impl_trait_context);
7472
f(self);
75-
self.impl_trait_context = orig_itc;
73+
self.invocation_parent.impl_trait_context = orig_itc;
7674
}
7775

7876
fn collect_field(&mut self, field: &'a FieldDef, index: Option<usize>) {
@@ -96,14 +94,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
9694

9795
fn visit_macro_invoc(&mut self, id: NodeId) {
9896
let id = id.placeholder_to_expn_id();
99-
let old_parent = self.resolver.invocation_parents.insert(
100-
id,
101-
InvocationParent {
102-
parent_def: self.parent_def,
103-
impl_trait_context: self.impl_trait_context,
104-
in_attr: self.in_attr,
105-
},
106-
);
97+
let old_parent = self.resolver.invocation_parents.insert(id, self.invocation_parent);
10798
assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
10899
}
109100
}
@@ -367,7 +358,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
367358
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
368359
return;
369360
}
370-
_ => self.parent_def,
361+
_ => self.invocation_parent.parent_def,
371362
};
372363

373364
self.with_parent(parent_def, |this| visit::walk_expr(this, expr))
@@ -382,13 +373,13 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
382373
// output or built artifacts, so replace them here...
383374
// Perhaps we should instead format APITs more robustly.
384375
let name = Symbol::intern(&pprust::ty_to_string(ty).replace('\n', " "));
385-
let kind = match self.impl_trait_context {
376+
let kind = match self.invocation_parent.impl_trait_context {
386377
ImplTraitContext::Universal => DefKind::TyParam,
387378
ImplTraitContext::Existential => DefKind::OpaqueTy,
388379
ImplTraitContext::InBinding => return visit::walk_ty(self, ty),
389380
};
390381
let id = self.create_def(*id, Some(name), kind, ty.span);
391-
match self.impl_trait_context {
382+
match self.invocation_parent.impl_trait_context {
392383
// Do not nest APIT, as we desugar them as `impl_trait: bounds`,
393384
// so the `impl_trait` node is not a parent to `bounds`.
394385
ImplTraitContext::Universal => visit::walk_ty(self, ty),
@@ -459,9 +450,9 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
459450
}
460451

461452
fn visit_attribute(&mut self, attr: &'a Attribute) -> Self::Result {
462-
let orig_in_attr = mem::replace(&mut self.in_attr, true);
453+
let orig_in_attr = mem::replace(&mut self.invocation_parent.in_attr, true);
463454
visit::walk_attribute(self, attr);
464-
self.in_attr = orig_in_attr;
455+
self.invocation_parent.in_attr = orig_in_attr;
465456
}
466457

467458
fn visit_inline_asm(&mut self, asm: &'a InlineAsm) {

0 commit comments

Comments
 (0)