Skip to content

Commit

Permalink
rustc_resolve: fix special-case for one-segment import paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Aug 14, 2018
1 parent cd47831 commit f9b1176
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 238 deletions.
28 changes: 0 additions & 28 deletions src/librustc_metadata/creader.rs
Expand Up @@ -1139,32 +1139,4 @@ impl<'a> CrateLoader<'a> {

cnum
}

pub fn process_use_extern(
&mut self,
name: Symbol,
span: Span,
id: ast::NodeId,
definitions: &Definitions,
) -> CrateNum {
let cnum = self.resolve_crate(
&None, name, name, None, None, span, PathKind::Crate, DepKind::Explicit
).0;

let def_id = definitions.opt_local_def_id(id).unwrap();
let path_len = definitions.def_path(def_id.index).data.len();

self.update_extern_crate(
cnum,
ExternCrate {
src: ExternCrateSource::Use,
span,
path_len,
direct: true,
},
&mut FxHashSet(),
);

cnum
}
}
27 changes: 22 additions & 5 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -17,7 +17,7 @@ use macros::{InvocationData, LegacyScope};
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
use {PerNS, Resolver, ResolverArenas};
use {ModuleOrUniformRoot, PerNS, Resolver, ResolverArenas};
use Namespace::{self, TypeNS, ValueNS, MacroNS};
use {resolve_error, resolve_struct_error, ResolutionError};

Expand Down Expand Up @@ -175,7 +175,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
ModuleKind::Def(_, name) => name,
ModuleKind::Block(..) => unreachable!(),
};
source.name = crate_name;
// HACK(eddyb) unclear how good this is, but keeping `$crate`
// in `source` breaks `src/test/compile-fail/import-crate-var.rs`,
// while the current crate doesn't have a valid `crate_name`.
if crate_name != keywords::Invalid.name() {
source.name = crate_name;
}
if rename.is_none() {
ident.name = crate_name;
}
Expand All @@ -187,6 +192,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
}
}

if ident.name == keywords::Crate.name() {
self.session.span_err(ident.span,
"crate root imports need to be explicitly named: \
`use crate as name;`");
}

let subclass = SingleImport {
target: ident,
source,
Expand Down Expand Up @@ -299,7 +310,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
root_id: item.id,
id: item.id,
parent,
imported_module: Cell::new(Some(module)),
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
root_span: item.span,
span: item.span,
Expand Down Expand Up @@ -701,7 +712,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
root_id: item.id,
id: item.id,
parent: graph_root,
imported_module: Cell::new(Some(module)),
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
subclass: ImportDirectiveSubclass::MacroUse,
root_span: span,
span,
Expand All @@ -721,7 +732,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
} else {
for (name, span) in legacy_imports.imports {
let ident = Ident::with_empty_ctxt(name);
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, span);
let result = self.resolve_ident_in_module(
ModuleOrUniformRoot::Module(module),
ident,
MacroNS,
false,
span,
);
if let Ok(binding) = result {
let directive = macro_use_directive(span);
self.potentially_unused_imports.push(directive);
Expand Down

0 comments on commit f9b1176

Please sign in to comment.