Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Modernize some naming #69805

Merged
merged 2 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 32 additions & 45 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
//! Imports are also considered items and placed into modules here, but not resolved yet.

use crate::def_collector::collect_definitions;
use crate::imports::ImportDirective;
use crate::imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
use crate::imports::{Import, ImportKind};
use crate::macros::{LegacyBinding, LegacyScope};
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
use crate::{CrateLint, Determinacy, PathResult, ResolutionError, VisResolutionError};
Expand Down Expand Up @@ -308,11 +307,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
})
}

// Add an import directive to the current module.
fn add_import_directive(
// Add an import to the current module.
fn add_import(
&mut self,
module_path: Vec<Segment>,
subclass: ImportDirectiveSubclass<'a>,
kind: ImportKind<'a>,
span: Span,
id: NodeId,
item: &ast::Item,
Expand All @@ -321,11 +320,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
vis: ty::Visibility,
) {
let current_module = self.parent_scope.module;
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
let import = self.r.arenas.alloc_import(Import {
kind,
parent_scope: self.parent_scope,
module_path,
imported_module: Cell::new(None),
subclass,
span,
id,
use_span: item.span,
Expand All @@ -337,25 +336,25 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
used: Cell::new(false),
});

debug!("add_import_directive({:?})", directive);
debug!("add_import({:?})", import);

self.r.indeterminate_imports.push(directive);
match directive.subclass {
self.r.indeterminate_imports.push(import);
match import.kind {
// Don't add unresolved underscore imports to modules
SingleImport { target: Ident { name: kw::Underscore, .. }, .. } => {}
SingleImport { target, type_ns_only, .. } => {
ImportKind::Single { target: Ident { name: kw::Underscore, .. }, .. } => {}
ImportKind::Single { target, type_ns_only, .. } => {
self.r.per_ns(|this, ns| {
if !type_ns_only || ns == TypeNS {
let key = this.new_key(target, ns);
let mut resolution = this.resolution(current_module, key).borrow_mut();
resolution.add_single_import(directive);
resolution.add_single_import(import);
}
});
}
// We don't add prelude imports to the globs since they only affect lexical scopes,
// which are not relevant to import resolution.
GlobImport { is_prelude: true, .. } => {}
GlobImport { .. } => current_module.globs.borrow_mut().push(directive),
ImportKind::Glob { is_prelude: true, .. } => {}
ImportKind::Glob { .. } => current_module.globs.borrow_mut().push(import),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -480,7 +479,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
);
}

let subclass = SingleImport {
let kind = ImportKind::Single {
source: source.ident,
target: ident,
source_bindings: PerNS {
Expand All @@ -496,9 +495,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
type_ns_only,
nested,
};
self.add_import_directive(
self.add_import(
module_path,
subclass,
kind,
use_tree.span,
id,
item,
Expand All @@ -508,20 +507,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
);
}
ast::UseTreeKind::Glob => {
let subclass = GlobImport {
let kind = ImportKind::Glob {
is_prelude: attr::contains_name(&item.attrs, sym::prelude_import),
max_vis: Cell::new(ty::Visibility::Invisible),
};
self.add_import_directive(
prefix,
subclass,
use_tree.span,
id,
item,
root_span,
item.id,
vis,
);
self.add_import(prefix, kind, use_tree.span, id, item, root_span, item.id, vis);
}
ast::UseTreeKind::Nested(ref items) => {
// Ensure there is at most one `self` in the list
Expand Down Expand Up @@ -637,15 +627,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let used = self.process_legacy_macro_imports(item, module);
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
let import = self.r.arenas.alloc_import(Import {
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
root_id: item.id,
id: item.id,
parent_scope: self.parent_scope,
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
subclass: ImportDirectiveSubclass::ExternCrate {
source: orig_name,
target: ident,
},
has_attributes: !item.attrs.is_empty(),
use_span_with_attributes: item.span_with_attributes(),
use_span: item.span,
Expand All @@ -655,8 +642,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
vis: Cell::new(vis),
used: Cell::new(used),
});
self.r.potentially_unused_imports.push(directive);
let imported_binding = self.r.import(binding, directive);
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
if ptr::eq(parent, self.r.graph_root) {
if let Some(entry) = self.r.extern_prelude.get(&ident.modern()) {
if expansion != ExpnId::root()
Expand Down Expand Up @@ -992,13 +979,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
}

let macro_use_directive = |this: &Self, span| {
this.r.arenas.alloc_import_directive(ImportDirective {
let macro_use_import = |this: &Self, span| {
this.r.arenas.alloc_import(Import {
kind: ImportKind::MacroUse,
root_id: item.id,
id: item.id,
parent_scope: this.parent_scope,
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
subclass: ImportDirectiveSubclass::MacroUse,
use_span_with_attributes: item.span_with_attributes(),
has_attributes: !item.attrs.is_empty(),
use_span: item.span,
Expand All @@ -1012,11 +999,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {

let allow_shadowing = self.parent_scope.expansion == ExpnId::root();
if let Some(span) = import_all {
let directive = macro_use_directive(self, span);
self.r.potentially_unused_imports.push(directive);
let import = macro_use_import(self, span);
self.r.potentially_unused_imports.push(import);
module.for_each_child(self, |this, ident, ns, binding| {
if ns == MacroNS {
let imported_binding = this.r.import(binding, directive);
let imported_binding = this.r.import(binding, import);
this.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
}
});
Expand All @@ -1031,9 +1018,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
ident.span,
);
if let Ok(binding) = result {
let directive = macro_use_directive(self, ident.span);
self.r.potentially_unused_imports.push(directive);
let imported_binding = self.r.import(binding, directive);
let import = macro_use_import(self, ident.span);
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
self.legacy_import_macro(
ident.name,
imported_binding,
Expand Down
36 changes: 18 additions & 18 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// Although this is mostly a lint pass, it lives in here because it depends on
// resolve data structures and because it finalises the privacy information for
// `use` directives.
// `use` items.
//
// Unused trait imports can't be checked until the method resolution. We save
// candidates here, and do the actual check in librustc_typeck/check_unused.rs.
Expand All @@ -23,7 +23,7 @@
// - `check_crate` finally emits the diagnostics based on the data generated
// in the last step

use crate::imports::ImportDirectiveSubclass;
use crate::imports::ImportKind;
use crate::Resolver;

use rustc::{lint, ty};
Expand Down Expand Up @@ -58,7 +58,7 @@ struct UnusedImportCheckVisitor<'a, 'b> {
}

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

impl Resolver<'_> {
crate fn check_unused(&mut self, krate: &ast::Crate) {
for directive in self.potentially_unused_imports.iter() {
match directive.subclass {
_ if directive.used.get()
|| directive.vis.get() == ty::Visibility::Public
|| directive.span.is_dummy() =>
for import in self.potentially_unused_imports.iter() {
match import.kind {
_ if import.used.get()
|| import.vis.get() == ty::Visibility::Public
|| import.span.is_dummy() =>
{
if let ImportDirectiveSubclass::MacroUse = directive.subclass {
if !directive.span.is_dummy() {
if let ImportKind::MacroUse = import.kind {
if !import.span.is_dummy() {
self.lint_buffer.buffer_lint(
lint::builtin::MACRO_USE_EXTERN_CRATE,
directive.id,
directive.span,
"deprecated `#[macro_use]` directive used to \
import.id,
import.span,
"deprecated `#[macro_use]` attribute used to \
import macros should be replaced at use sites \
with a `use` statement to import the macro \
with a `use` item to import the macro \
instead",
);
}
}
}
ImportDirectiveSubclass::ExternCrate { .. } => {
self.maybe_unused_extern_crates.push((directive.id, directive.span));
ImportKind::ExternCrate { .. } => {
self.maybe_unused_extern_crates.push((import.id, import.span));
}
ImportDirectiveSubclass::MacroUse => {
ImportKind::MacroUse => {
let lint = lint::builtin::UNUSED_IMPORTS;
let msg = "unused `#[macro_use]` import";
self.lint_buffer.buffer_lint(lint, directive.id, directive.span, msg);
self.lint_buffer.buffer_lint(lint, import.id, import.span, msg);
}
_ => {}
}
Expand Down
24 changes: 11 additions & 13 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::{BytePos, MultiSpan, Span};

use crate::imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver};
use crate::imports::{Import, ImportKind, ImportResolver};
use crate::path_names_to_string;
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
use crate::{BindingError, CrateLint, HasGenericParams, LegacyScope, Module, ModuleOrUniformRoot};
Expand Down Expand Up @@ -1126,7 +1126,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
/// ```
pub(crate) fn check_for_module_export_macro(
&mut self,
directive: &'b ImportDirective<'b>,
import: &'b Import<'b>,
module: ModuleOrUniformRoot<'b>,
ident: Ident,
) -> Option<(Option<Suggestion>, Vec<String>)> {
Expand All @@ -1151,28 +1151,26 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let binding = resolution.borrow().binding()?;
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
let module_name = crate_module.kind.name().unwrap();
let import = match directive.subclass {
ImportDirectiveSubclass::SingleImport { source, target, .. }
if source != target =>
{
let import_snippet = match import.kind {
ImportKind::Single { source, target, .. } if source != target => {
format!("{} as {}", source, target)
}
_ => format!("{}", ident),
};

let mut corrections: Vec<(Span, String)> = Vec::new();
if !directive.is_nested() {
if !import.is_nested() {
// Assume this is the easy case of `use issue_59764::foo::makro;` and just remove
// intermediate segments.
corrections.push((directive.span, format!("{}::{}", module_name, import)));
corrections.push((import.span, format!("{}::{}", module_name, import_snippet)));
} else {
// Find the binding span (and any trailing commas and spaces).
// ie. `use a::b::{c, d, e};`
// ^^^
let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
self.r.session,
directive.span,
directive.use_span,
import.span,
import.use_span,
);
debug!(
"check_for_module_export_macro: found_closing_brace={:?} binding_span={:?}",
Expand Down Expand Up @@ -1209,7 +1207,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let (has_nested, after_crate_name) = find_span_immediately_after_crate_name(
self.r.session,
module_name,
directive.use_span,
import.use_span,
);
debug!(
"check_for_module_export_macro: has_nested={:?} after_crate_name={:?}",
Expand All @@ -1225,11 +1223,11 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
start_point,
if has_nested {
// In this case, `start_snippet` must equal '{'.
format!("{}{}, ", start_snippet, import)
format!("{}{}, ", start_snippet, import_snippet)
} else {
// In this case, add a `{`, then the moved import, then whatever
// was there before.
format!("{{{}, {}", import, start_snippet)
format!("{{{}, {}", import_snippet, start_snippet)
},
));
}
Expand Down
Loading