Skip to content

Commit 17b7791

Browse files
authored
Rollup merge of #69805 - petrochenkov:importname, r=Centril
resolve: Modernize some naming `ImportDirective` -> `Import` `ImportDirectiveSubclass` -> `ImportKind` `ImportKind::SingleImport` -> `ImportKind::Single` `ImportKind::GlobImport` -> `ImportKind::Glob`
2 parents f497325 + 66d7a88 commit 17b7791

8 files changed

+283
-325
lines changed

src/librustc_resolve/build_reduced_graph.rs

+32-45
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//! Imports are also considered items and placed into modules here, but not resolved yet.
77
88
use crate::def_collector::collect_definitions;
9-
use crate::imports::ImportDirective;
10-
use crate::imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
9+
use crate::imports::{Import, ImportKind};
1110
use crate::macros::{LegacyBinding, LegacyScope};
1211
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
1312
use crate::{CrateLint, Determinacy, PathResult, ResolutionError, VisResolutionError};
@@ -308,11 +307,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
308307
})
309308
}
310309

311-
// Add an import directive to the current module.
312-
fn add_import_directive(
310+
// Add an import to the current module.
311+
fn add_import(
313312
&mut self,
314313
module_path: Vec<Segment>,
315-
subclass: ImportDirectiveSubclass<'a>,
314+
kind: ImportKind<'a>,
316315
span: Span,
317316
id: NodeId,
318317
item: &ast::Item,
@@ -321,11 +320,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
321320
vis: ty::Visibility,
322321
) {
323322
let current_module = self.parent_scope.module;
324-
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
323+
let import = self.r.arenas.alloc_import(Import {
324+
kind,
325325
parent_scope: self.parent_scope,
326326
module_path,
327327
imported_module: Cell::new(None),
328-
subclass,
329328
span,
330329
id,
331330
use_span: item.span,
@@ -337,25 +336,25 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
337336
used: Cell::new(false),
338337
});
339338

340-
debug!("add_import_directive({:?})", directive);
339+
debug!("add_import({:?})", import);
341340

342-
self.r.indeterminate_imports.push(directive);
343-
match directive.subclass {
341+
self.r.indeterminate_imports.push(import);
342+
match import.kind {
344343
// Don't add unresolved underscore imports to modules
345-
SingleImport { target: Ident { name: kw::Underscore, .. }, .. } => {}
346-
SingleImport { target, type_ns_only, .. } => {
344+
ImportKind::Single { target: Ident { name: kw::Underscore, .. }, .. } => {}
345+
ImportKind::Single { target, type_ns_only, .. } => {
347346
self.r.per_ns(|this, ns| {
348347
if !type_ns_only || ns == TypeNS {
349348
let key = this.new_key(target, ns);
350349
let mut resolution = this.resolution(current_module, key).borrow_mut();
351-
resolution.add_single_import(directive);
350+
resolution.add_single_import(import);
352351
}
353352
});
354353
}
355354
// We don't add prelude imports to the globs since they only affect lexical scopes,
356355
// which are not relevant to import resolution.
357-
GlobImport { is_prelude: true, .. } => {}
358-
GlobImport { .. } => current_module.globs.borrow_mut().push(directive),
356+
ImportKind::Glob { is_prelude: true, .. } => {}
357+
ImportKind::Glob { .. } => current_module.globs.borrow_mut().push(import),
359358
_ => unreachable!(),
360359
}
361360
}
@@ -480,7 +479,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
480479
);
481480
}
482481

483-
let subclass = SingleImport {
482+
let kind = ImportKind::Single {
484483
source: source.ident,
485484
target: ident,
486485
source_bindings: PerNS {
@@ -496,9 +495,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
496495
type_ns_only,
497496
nested,
498497
};
499-
self.add_import_directive(
498+
self.add_import(
500499
module_path,
501-
subclass,
500+
kind,
502501
use_tree.span,
503502
id,
504503
item,
@@ -508,20 +507,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
508507
);
509508
}
510509
ast::UseTreeKind::Glob => {
511-
let subclass = GlobImport {
510+
let kind = ImportKind::Glob {
512511
is_prelude: attr::contains_name(&item.attrs, sym::prelude_import),
513512
max_vis: Cell::new(ty::Visibility::Invisible),
514513
};
515-
self.add_import_directive(
516-
prefix,
517-
subclass,
518-
use_tree.span,
519-
id,
520-
item,
521-
root_span,
522-
item.id,
523-
vis,
524-
);
514+
self.add_import(prefix, kind, use_tree.span, id, item, root_span, item.id, vis);
525515
}
526516
ast::UseTreeKind::Nested(ref items) => {
527517
// Ensure there is at most one `self` in the list
@@ -637,15 +627,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
637627
let used = self.process_legacy_macro_imports(item, module);
638628
let binding =
639629
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
640-
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
630+
let import = self.r.arenas.alloc_import(Import {
631+
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
641632
root_id: item.id,
642633
id: item.id,
643634
parent_scope: self.parent_scope,
644635
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
645-
subclass: ImportDirectiveSubclass::ExternCrate {
646-
source: orig_name,
647-
target: ident,
648-
},
649636
has_attributes: !item.attrs.is_empty(),
650637
use_span_with_attributes: item.span_with_attributes(),
651638
use_span: item.span,
@@ -655,8 +642,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
655642
vis: Cell::new(vis),
656643
used: Cell::new(used),
657644
});
658-
self.r.potentially_unused_imports.push(directive);
659-
let imported_binding = self.r.import(binding, directive);
645+
self.r.potentially_unused_imports.push(import);
646+
let imported_binding = self.r.import(binding, import);
660647
if ptr::eq(parent, self.r.graph_root) {
661648
if let Some(entry) = self.r.extern_prelude.get(&ident.modern()) {
662649
if expansion != ExpnId::root()
@@ -992,13 +979,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
992979
}
993980
}
994981

995-
let macro_use_directive = |this: &Self, span| {
996-
this.r.arenas.alloc_import_directive(ImportDirective {
982+
let macro_use_import = |this: &Self, span| {
983+
this.r.arenas.alloc_import(Import {
984+
kind: ImportKind::MacroUse,
997985
root_id: item.id,
998986
id: item.id,
999987
parent_scope: this.parent_scope,
1000988
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
1001-
subclass: ImportDirectiveSubclass::MacroUse,
1002989
use_span_with_attributes: item.span_with_attributes(),
1003990
has_attributes: !item.attrs.is_empty(),
1004991
use_span: item.span,
@@ -1012,11 +999,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
1012999

10131000
let allow_shadowing = self.parent_scope.expansion == ExpnId::root();
10141001
if let Some(span) = import_all {
1015-
let directive = macro_use_directive(self, span);
1016-
self.r.potentially_unused_imports.push(directive);
1002+
let import = macro_use_import(self, span);
1003+
self.r.potentially_unused_imports.push(import);
10171004
module.for_each_child(self, |this, ident, ns, binding| {
10181005
if ns == MacroNS {
1019-
let imported_binding = this.r.import(binding, directive);
1006+
let imported_binding = this.r.import(binding, import);
10201007
this.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
10211008
}
10221009
});
@@ -1031,9 +1018,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10311018
ident.span,
10321019
);
10331020
if let Ok(binding) = result {
1034-
let directive = macro_use_directive(self, ident.span);
1035-
self.r.potentially_unused_imports.push(directive);
1036-
let imported_binding = self.r.import(binding, directive);
1021+
let import = macro_use_import(self, ident.span);
1022+
self.r.potentially_unused_imports.push(import);
1023+
let imported_binding = self.r.import(binding, import);
10371024
self.legacy_import_macro(
10381025
ident.name,
10391026
imported_binding,

src/librustc_resolve/check_unused.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// Although this is mostly a lint pass, it lives in here because it depends on
55
// resolve data structures and because it finalises the privacy information for
6-
// `use` directives.
6+
// `use` items.
77
//
88
// Unused trait imports can't be checked until the method resolution. We save
99
// candidates here, and do the actual check in librustc_typeck/check_unused.rs.
@@ -23,7 +23,7 @@
2323
// - `check_crate` finally emits the diagnostics based on the data generated
2424
// in the last step
2525

26-
use crate::imports::ImportDirectiveSubclass;
26+
use crate::imports::ImportKind;
2727
use crate::Resolver;
2828

2929
use rustc::{lint, ty};
@@ -58,7 +58,7 @@ struct UnusedImportCheckVisitor<'a, 'b> {
5858
}
5959

6060
impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
61-
// We have information about whether `use` (import) directives are actually
61+
// We have information about whether `use` (import) items are actually
6262
// used now. If an import is not used at all, we signal a lint error.
6363
fn check_import(&mut self, id: ast::NodeId) {
6464
let mut used = false;
@@ -223,33 +223,33 @@ fn calc_unused_spans(
223223

224224
impl Resolver<'_> {
225225
crate fn check_unused(&mut self, krate: &ast::Crate) {
226-
for directive in self.potentially_unused_imports.iter() {
227-
match directive.subclass {
228-
_ if directive.used.get()
229-
|| directive.vis.get() == ty::Visibility::Public
230-
|| directive.span.is_dummy() =>
226+
for import in self.potentially_unused_imports.iter() {
227+
match import.kind {
228+
_ if import.used.get()
229+
|| import.vis.get() == ty::Visibility::Public
230+
|| import.span.is_dummy() =>
231231
{
232-
if let ImportDirectiveSubclass::MacroUse = directive.subclass {
233-
if !directive.span.is_dummy() {
232+
if let ImportKind::MacroUse = import.kind {
233+
if !import.span.is_dummy() {
234234
self.lint_buffer.buffer_lint(
235235
lint::builtin::MACRO_USE_EXTERN_CRATE,
236-
directive.id,
237-
directive.span,
238-
"deprecated `#[macro_use]` directive used to \
236+
import.id,
237+
import.span,
238+
"deprecated `#[macro_use]` attribute used to \
239239
import macros should be replaced at use sites \
240-
with a `use` statement to import the macro \
240+
with a `use` item to import the macro \
241241
instead",
242242
);
243243
}
244244
}
245245
}
246-
ImportDirectiveSubclass::ExternCrate { .. } => {
247-
self.maybe_unused_extern_crates.push((directive.id, directive.span));
246+
ImportKind::ExternCrate { .. } => {
247+
self.maybe_unused_extern_crates.push((import.id, import.span));
248248
}
249-
ImportDirectiveSubclass::MacroUse => {
249+
ImportKind::MacroUse => {
250250
let lint = lint::builtin::UNUSED_IMPORTS;
251251
let msg = "unused `#[macro_use]` import";
252-
self.lint_buffer.buffer_lint(lint, directive.id, directive.span, msg);
252+
self.lint_buffer.buffer_lint(lint, import.id, import.span, msg);
253253
}
254254
_ => {}
255255
}

src/librustc_resolve/diagnostics.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::source_map::SourceMap;
1818
use rustc_span::symbol::{kw, Symbol};
1919
use rustc_span::{BytePos, MultiSpan, Span};
2020

21-
use crate::imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver};
21+
use crate::imports::{Import, ImportKind, ImportResolver};
2222
use crate::path_names_to_string;
2323
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
2424
use crate::{BindingError, CrateLint, HasGenericParams, LegacyScope, Module, ModuleOrUniformRoot};
@@ -1125,7 +1125,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11251125
/// ```
11261126
pub(crate) fn check_for_module_export_macro(
11271127
&mut self,
1128-
directive: &'b ImportDirective<'b>,
1128+
import: &'b Import<'b>,
11291129
module: ModuleOrUniformRoot<'b>,
11301130
ident: Ident,
11311131
) -> Option<(Option<Suggestion>, Vec<String>)> {
@@ -1150,28 +1150,26 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11501150
let binding = resolution.borrow().binding()?;
11511151
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
11521152
let module_name = crate_module.kind.name().unwrap();
1153-
let import = match directive.subclass {
1154-
ImportDirectiveSubclass::SingleImport { source, target, .. }
1155-
if source != target =>
1156-
{
1153+
let import_snippet = match import.kind {
1154+
ImportKind::Single { source, target, .. } if source != target => {
11571155
format!("{} as {}", source, target)
11581156
}
11591157
_ => format!("{}", ident),
11601158
};
11611159

11621160
let mut corrections: Vec<(Span, String)> = Vec::new();
1163-
if !directive.is_nested() {
1161+
if !import.is_nested() {
11641162
// Assume this is the easy case of `use issue_59764::foo::makro;` and just remove
11651163
// intermediate segments.
1166-
corrections.push((directive.span, format!("{}::{}", module_name, import)));
1164+
corrections.push((import.span, format!("{}::{}", module_name, import_snippet)));
11671165
} else {
11681166
// Find the binding span (and any trailing commas and spaces).
11691167
// ie. `use a::b::{c, d, e};`
11701168
// ^^^
11711169
let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
11721170
self.r.session,
1173-
directive.span,
1174-
directive.use_span,
1171+
import.span,
1172+
import.use_span,
11751173
);
11761174
debug!(
11771175
"check_for_module_export_macro: found_closing_brace={:?} binding_span={:?}",
@@ -1208,7 +1206,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12081206
let (has_nested, after_crate_name) = find_span_immediately_after_crate_name(
12091207
self.r.session,
12101208
module_name,
1211-
directive.use_span,
1209+
import.use_span,
12121210
);
12131211
debug!(
12141212
"check_for_module_export_macro: has_nested={:?} after_crate_name={:?}",
@@ -1224,11 +1222,11 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12241222
start_point,
12251223
if has_nested {
12261224
// In this case, `start_snippet` must equal '{'.
1227-
format!("{}{}, ", start_snippet, import)
1225+
format!("{}{}, ", start_snippet, import_snippet)
12281226
} else {
12291227
// In this case, add a `{`, then the moved import, then whatever
12301228
// was there before.
1231-
format!("{{{}, {}", import, start_snippet)
1229+
format!("{{{}, {}", import_snippet, start_snippet)
12321230
},
12331231
));
12341232
}

0 commit comments

Comments
 (0)