Skip to content

Commit 637bfe6

Browse files
committed
resolve: Not all imports have their own NodeId
1 parent 2afca78 commit 637bfe6

File tree

6 files changed

+142
-104
lines changed

6 files changed

+142
-104
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
364364
module_path: Vec<Segment>,
365365
kind: ImportKind<'a>,
366366
span: Span,
367-
id: NodeId,
368367
item: &ast::Item,
369368
root_span: Span,
370369
root_id: NodeId,
@@ -377,7 +376,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
377376
module_path,
378377
imported_module: Cell::new(None),
379378
span,
380-
id,
381379
use_span: item.span,
382380
use_span_with_attributes: item.span_with_attributes(),
383381
has_attributes: !item.attrs.is_empty(),
@@ -574,27 +572,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
574572
},
575573
type_ns_only,
576574
nested,
575+
id,
577576
additional_ids: (id1, id2),
578577
};
579578

580-
self.add_import(
581-
module_path,
582-
kind,
583-
use_tree.span,
584-
id,
585-
item,
586-
root_span,
587-
item.id,
588-
vis,
589-
);
579+
self.add_import(module_path, kind, use_tree.span, item, root_span, item.id, vis);
590580
}
591581
ast::UseTreeKind::Glob => {
592582
let kind = ImportKind::Glob {
593583
is_prelude: self.r.session.contains_name(&item.attrs, sym::prelude_import),
594584
max_vis: Cell::new(None),
585+
id,
595586
};
596587
self.r.visibilities.insert(self.r.local_def_id(id), vis);
597-
self.add_import(prefix, kind, use_tree.span, id, item, root_span, item.id, vis);
588+
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
598589
}
599590
ast::UseTreeKind::Nested(ref items) => {
600591
// Ensure there is at most one `self` in the list
@@ -881,9 +872,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
881872
})
882873
.unwrap_or((true, None, self.r.dummy_binding));
883874
let import = self.r.arenas.alloc_import(Import {
884-
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
875+
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
885876
root_id: item.id,
886-
id: item.id,
887877
parent_scope: self.parent_scope,
888878
imported_module: Cell::new(module),
889879
has_attributes: !item.attrs.is_empty(),
@@ -1118,7 +1108,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11181108
this.r.arenas.alloc_import(Import {
11191109
kind: ImportKind::MacroUse,
11201110
root_id: item.id,
1121-
id: item.id,
11221111
parent_scope: this.parent_scope,
11231112
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
11241113
use_span_with_attributes: item.span_with_attributes(),

compiler/rustc_resolve/src/check_unused.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl Resolver<'_> {
234234
if !import.span.is_dummy() {
235235
self.lint_buffer.buffer_lint(
236236
MACRO_USE_EXTERN_CRATE,
237-
import.id,
237+
import.root_id,
238238
import.span,
239239
"deprecated `#[macro_use]` attribute used to \
240240
import macros should be replaced at use sites \
@@ -244,13 +244,13 @@ impl Resolver<'_> {
244244
}
245245
}
246246
}
247-
ImportKind::ExternCrate { .. } => {
248-
let def_id = self.local_def_id(import.id);
247+
ImportKind::ExternCrate { id, .. } => {
248+
let def_id = self.local_def_id(id);
249249
self.maybe_unused_extern_crates.push((def_id, import.span));
250250
}
251251
ImportKind::MacroUse => {
252252
let msg = "unused `#[macro_use]` import";
253-
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, import.id, import.span, msg);
253+
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, import.root_id, import.span, msg);
254254
}
255255
_ => {}
256256
}

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl<'a> Resolver<'a> {
353353
}
354354
}
355355
}
356-
ImportKind::ExternCrate { source, target } => {
356+
ImportKind::ExternCrate { source, target, .. } => {
357357
suggestion = Some(format!(
358358
"extern crate {} as {};",
359359
source.unwrap_or(target.name),

compiler/rustc_resolve/src/effective_visibilities.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,40 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
5757
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
5858
binding.kind
5959
{
60-
let mut update = |node_id| self.update(
61-
self.r.local_def_id(node_id),
62-
binding.vis.expect_local(),
63-
prev_parent_id,
64-
level,
65-
);
66-
// In theory all the import IDs have individual visibilities and effective
67-
// visibilities, but in practice these IDs go straigth to HIR where all
68-
// their few uses assume that their (effective) visibility applies to the
69-
// whole syntactic `use` item. So we update them all to the maximum value
70-
// among the potential individual effective visibilities. Maybe HIR for
71-
// imports shouldn't use three IDs at all.
72-
update(import.id);
73-
if let ImportKind::Single { additional_ids, .. } = import.kind {
74-
update(additional_ids.0);
75-
update(additional_ids.1);
60+
let mut update = |node_id| {
61+
self.update(
62+
self.r.local_def_id(node_id),
63+
binding.vis.expect_local(),
64+
prev_parent_id,
65+
level,
66+
)
67+
};
68+
match import.kind {
69+
ImportKind::Single { id, additional_ids, .. } => {
70+
// In theory all the import IDs have individual visibilities and
71+
// effective visibilities, but in practice these IDs go straigth to
72+
// HIR where all their few uses assume that their (effective)
73+
// visibility applies to the whole syntactic `use` item. So we
74+
// update them all to the maximum value among the potential
75+
// individual effective visibilities. Maybe HIR for imports
76+
// shouldn't use three IDs at all.
77+
update(id);
78+
update(additional_ids.0);
79+
update(additional_ids.1);
80+
prev_parent_id = self.r.local_def_id(id);
81+
}
82+
ImportKind::Glob { id, .. } | ImportKind::ExternCrate { id, .. } => {
83+
update(id);
84+
prev_parent_id = self.r.local_def_id(id);
85+
}
86+
ImportKind::MacroUse => {
87+
// In theory we should reset the parent id to something private
88+
// here, but `macro_use` imports always refer to external items,
89+
// so it doesn't matter and we can just do nothing.
90+
}
7691
}
7792

7893
level = Level::Reexported;
79-
prev_parent_id = self.r.local_def_id(import.id);
8094
binding = nested_binding;
8195
}
8296
}

0 commit comments

Comments
 (0)