Skip to content

Commit d04b5dd

Browse files
committed
Cleanup
1 parent ea47178 commit d04b5dd

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use rustc_hir::def_id::LocalDefId;
99
use rustc_span::hygiene::LocalExpnId;
1010
use rustc_span::symbol::{kw, sym, Symbol};
1111
use rustc_span::Span;
12-
use tracing::{debug, instrument};
12+
use tracing::debug;
1313

14-
use crate::{ImplTraitContext, PendingAnonConstInfo, Resolver};
14+
use crate::{ImplTraitContext, InvocationParent, PendingAnonConstInfo, Resolver};
1515

1616
pub(crate) fn collect_definitions(
1717
resolver: &mut Resolver<'_, '_>,
1818
fragment: &AstFragment,
1919
expansion: LocalExpnId,
2020
) {
21-
let (parent_def, pending_anon_const_info, impl_trait_context, in_attr) =
21+
let InvocationParent { parent_def, pending_anon_const_info, impl_trait_context, in_attr } =
2222
resolver.invocation_parents[&expansion];
2323
let mut visitor = DefCollector {
2424
resolver,
@@ -50,17 +50,6 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
5050
span: Span,
5151
) -> LocalDefId {
5252
let parent_def = self.parent_def;
53-
self.create_def_with_parent(parent_def, node_id, name, def_kind, span)
54-
}
55-
56-
fn create_def_with_parent(
57-
&mut self,
58-
parent_def: LocalDefId,
59-
node_id: NodeId,
60-
name: Symbol,
61-
def_kind: DefKind,
62-
span: Span,
63-
) -> LocalDefId {
6453
debug!(
6554
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
6655
node_id, def_kind, parent_def
@@ -133,7 +122,12 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
133122
let pending_anon_const_info = self.pending_anon_const_info.take();
134123
let old_parent = self.resolver.invocation_parents.insert(
135124
id,
136-
(self.parent_def, pending_anon_const_info, self.impl_trait_context, self.in_attr),
125+
InvocationParent {
126+
parent_def: self.parent_def,
127+
pending_anon_const_info,
128+
impl_trait_context: self.impl_trait_context,
129+
in_attr: self.in_attr,
130+
},
137131
);
138132
assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
139133
}
@@ -334,7 +328,6 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
334328
}
335329
}
336330

337-
#[instrument(level = "debug", skip(self))]
338331
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
339332
// HACK(min_generic_const_args): don't create defs for anon consts if we think they will
340333
// later be turned into ConstArgKind::Path's. because this is before resolve is done, we
@@ -343,18 +336,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
343336
// items will be messed up, but that's ok because there can't be any if we're just looking
344337
// for bare idents.
345338
if matches!(constant.value.maybe_unwrap_block().kind, ExprKind::MacCall(..)) {
346-
debug!("ATTN(strict): unwrapped const is macro");
347-
self.pending_anon_const_info = Some(PendingAnonConstInfo {
348-
parent_def: self.parent_def,
349-
id: constant.id,
350-
span: constant.value.span,
351-
});
339+
self.pending_anon_const_info =
340+
Some(PendingAnonConstInfo { id: constant.id, span: constant.value.span });
352341
visit::walk_anon_const(self, constant)
353342
} else if constant.value.is_potential_trivial_const_arg() {
354-
debug!("ATTN(strict): unwrapped const is potentially trivial");
355343
visit::walk_anon_const(self, constant)
356344
} else {
357-
debug!("ATTN(strict): unwrapped const is not trivial");
358345
let def =
359346
self.create_def(constant.id, kw::Empty, DefKind::AnonConst, constant.value.span);
360347
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
@@ -367,19 +354,10 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
367354
}
368355

369356
let grandparent_def = if let Some(pending_anon) = self.pending_anon_const_info {
370-
debug!("ATTN(lazy): pending anon={pending_anon:?} expr={expr:?}");
371357
self.pending_anon_const_info = None;
372358
if !expr.is_potential_trivial_const_arg() {
373-
debug!("ATTN(lazy): pending anon is not trivial");
374-
self.create_def_with_parent(
375-
pending_anon.parent_def,
376-
pending_anon.id,
377-
kw::Empty,
378-
DefKind::AnonConst,
379-
pending_anon.span,
380-
)
359+
self.create_def(pending_anon.id, kw::Empty, DefKind::AnonConst, pending_anon.span)
381360
} else {
382-
debug!("ATTN(lazy): pending anon is potentially trivial");
383361
self.parent_def
384362
}
385363
} else {

compiler/rustc_resolve/src/lib.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,24 @@ impl<'a> ParentScope<'a> {
171171
}
172172

173173
#[derive(Copy, Debug, Clone)]
174-
struct PendingAnonConstInfo {
174+
struct InvocationParent {
175175
parent_def: LocalDefId,
176+
pending_anon_const_info: Option<PendingAnonConstInfo>,
177+
impl_trait_context: ImplTraitContext,
178+
in_attr: bool,
179+
}
180+
181+
impl InvocationParent {
182+
const ROOT: Self = Self {
183+
parent_def: CRATE_DEF_ID,
184+
pending_anon_const_info: None,
185+
impl_trait_context: ImplTraitContext::Existential,
186+
in_attr: false,
187+
};
188+
}
189+
190+
#[derive(Copy, Debug, Clone)]
191+
struct PendingAnonConstInfo {
176192
id: NodeId,
177193
span: Span,
178194
}
@@ -1150,10 +1166,7 @@ pub struct Resolver<'a, 'tcx> {
11501166
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
11511167
/// we know what parent node that fragment should be attached to thanks to this table,
11521168
/// and how the `impl Trait` fragments were introduced.
1153-
invocation_parents: FxHashMap<
1154-
LocalExpnId,
1155-
(LocalDefId, Option<PendingAnonConstInfo>, ImplTraitContext, bool /*in_attr*/),
1156-
>,
1169+
invocation_parents: FxHashMap<LocalExpnId, InvocationParent>,
11571170

11581171
/// Some way to know that we are in a *trait* impl in `visit_assoc_item`.
11591172
/// FIXME: Replace with a more general AST map (together with some other fields).
@@ -1390,8 +1403,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13901403
node_id_to_def_id.insert(CRATE_NODE_ID, crate_feed);
13911404

13921405
let mut invocation_parents = FxHashMap::default();
1393-
invocation_parents
1394-
.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, None, ImplTraitContext::Existential, false));
1406+
invocation_parents.insert(LocalExpnId::ROOT, InvocationParent::ROOT);
13951407

13961408
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = tcx
13971409
.sess

compiler/rustc_resolve/src/macros.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ use crate::errors::{
4242
use crate::imports::Import;
4343
use crate::Namespace::*;
4444
use crate::{
45-
BindingKey, BuiltinMacroState, DeriveData, Determinacy, Finalize, MacroData, ModuleKind,
46-
ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult, ResolutionError,
47-
Resolver, ScopeSet, Segment, ToNameBinding, Used,
45+
BindingKey, BuiltinMacroState, DeriveData, Determinacy, Finalize, InvocationParent, MacroData,
46+
ModuleKind, ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult,
47+
ResolutionError, Resolver, ScopeSet, Segment, ToNameBinding, Used,
4848
};
4949

5050
type Res = def::Res<NodeId>;
@@ -183,7 +183,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
183183
}
184184

185185
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
186-
self.invocation_parents[&id].0
186+
self.invocation_parents[&id].parent_def
187187
}
188188

189189
fn resolve_dollar_crates(&mut self) {
@@ -303,12 +303,12 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
303303
.invocation_parents
304304
.get(&invoc_id)
305305
.or_else(|| self.invocation_parents.get(&eager_expansion_root))
306-
.filter(|&&(mod_def_id, _, _, in_attr)| {
306+
.filter(|&&InvocationParent { parent_def: mod_def_id, in_attr, .. }| {
307307
in_attr
308308
&& invoc.fragment_kind == AstFragmentKind::Expr
309309
&& self.tcx.def_kind(mod_def_id) == DefKind::Mod
310310
})
311-
.map(|&(mod_def_id, ..)| mod_def_id);
311+
.map(|&InvocationParent { parent_def: mod_def_id, .. }| mod_def_id);
312312
let (ext, res) = self.smart_resolve_macro_path(
313313
path,
314314
kind,
@@ -951,7 +951,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
951951
let node_id = self
952952
.invocation_parents
953953
.get(&parent_scope.expansion)
954-
.map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[id.0]);
954+
.map_or(ast::CRATE_NODE_ID, |parent| {
955+
self.def_id_to_node_id[parent.parent_def]
956+
});
955957
self.lint_buffer.buffer_lint(
956958
LEGACY_DERIVE_HELPERS,
957959
node_id,

0 commit comments

Comments
 (0)