Skip to content

Commit

Permalink
Use Idents instead of Names in ImportDirective's paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Oct 19, 2016
1 parent 6a6ef91 commit 7b81106
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -95,22 +95,22 @@ impl<'b> Resolver<'b> {
// Extract and intern the module part of the path. For
// globs and lists, the path is found directly in the AST;
// for simple paths we have to munge the path a little.
let module_path: Vec<Name> = match view_path.node {
let module_path: Vec<_> = match view_path.node {
ViewPathSimple(_, ref full_path) => {
full_path.segments
.split_last()
.unwrap()
.1
.iter()
.map(|seg| seg.identifier.name)
.map(|seg| seg.identifier)
.collect()
}

ViewPathGlob(ref module_ident_path) |
ViewPathList(ref module_ident_path, _) => {
module_ident_path.segments
.iter()
.map(|seg| seg.identifier.name)
.map(|seg| seg.identifier)
.collect()
}
};
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'b> Resolver<'b> {
(module_path.clone(), node.name.name, rename)
} else {
let name = match module_path.last() {
Some(name) => *name,
Some(ident) => ident.name,
None => {
resolve_error(
self,
Expand Down
65 changes: 29 additions & 36 deletions src/librustc_resolve/lib.rs
Expand Up @@ -1178,18 +1178,18 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
}

trait Named {
fn name(&self) -> Name;
fn ident(&self) -> Ident;
}

impl Named for ast::PathSegment {
fn name(&self) -> Name {
self.identifier.name
fn ident(&self) -> Ident {
self.identifier
}
}

impl Named for hir::PathSegment {
fn name(&self) -> Name {
self.name
fn ident(&self) -> Ident {
Ident::with_empty_ctxt(self.name)
}
}

Expand Down Expand Up @@ -1364,7 +1364,7 @@ impl<'a> Resolver<'a> {
/// Resolves the given module path from the given root `search_module`.
fn resolve_module_path_from_root(&mut self,
mut search_module: Module<'a>,
module_path: &[Name],
module_path: &[Ident],
index: usize,
span: Option<Span>)
-> ResolveResult<Module<'a>> {
Expand All @@ -1387,7 +1387,7 @@ impl<'a> Resolver<'a> {
// upward though scope chains; we simply resolve names directly in
// modules as we go.
while index < module_path_len {
let name = module_path[index];
let name = module_path[index].name;
match self.resolve_name_in_module(search_module, name, TypeNS, false, span) {
Failed(_) => {
let segment_name = name.as_str();
Expand Down Expand Up @@ -1441,7 +1441,7 @@ impl<'a> Resolver<'a> {
/// Attempts to resolve the module part of an import directive or path
/// rooted at the given module.
fn resolve_module_path(&mut self,
module_path: &[Name],
module_path: &[Ident],
use_lexical_scope: UseLexicalScopeFlag,
span: Option<Span>)
-> ResolveResult<Module<'a>> {
Expand Down Expand Up @@ -1479,7 +1479,7 @@ impl<'a> Resolver<'a> {
// This is not a crate-relative path. We resolve the
// first component of the path in the current lexical
// scope and then proceed to resolve below that.
let ident = Ident::with_empty_ctxt(module_path[0]);
let ident = module_path[0];
let lexical_binding =
self.resolve_ident_in_lexical_scope(ident, TypeNS, span);
if let Some(binding) = lexical_binding.and_then(LexicalScopeBinding::item) {
Expand Down Expand Up @@ -1577,11 +1577,11 @@ impl<'a> Resolver<'a> {
/// Resolves a "module prefix". A module prefix is one or both of (a) `self::`;
/// (b) some chain of `super::`.
/// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) *
fn resolve_module_prefix(&mut self, module_path: &[Name], span: Option<Span>)
fn resolve_module_prefix(&mut self, module_path: &[Ident], span: Option<Span>)
-> ResolveResult<ModulePrefixResult<'a>> {
// Start at the current module if we see `self` or `super`, or at the
// top of the crate otherwise.
let mut i = match &*module_path[0].as_str() {
let mut i = match &*module_path[0].name.as_str() {
"self" => 1,
"super" => 0,
_ => return Success(NoPrefixFound),
Expand All @@ -1591,7 +1591,7 @@ impl<'a> Resolver<'a> {
self.module_map[&self.current_module.normal_ancestor_id.unwrap()];

// Now loop through all the `super`s we find.
while i < module_path.len() && "super" == module_path[i].as_str() {
while i < module_path.len() && "super" == module_path[i].name.as_str() {
debug!("(resolving module prefix) resolving `super` at {}",
module_to_string(&containing_module));
if let Some(parent) = containing_module.parent {
Expand Down Expand Up @@ -2681,12 +2681,8 @@ impl<'a> Resolver<'a> {
namespace: Namespace)
-> Result<&'a NameBinding<'a>,
bool /* true if an error was reported */> {
let module_path = segments.split_last()
.unwrap()
.1
.iter()
.map(|ps| ps.identifier.name)
.collect::<Vec<_>>();
let module_path =
segments.split_last().unwrap().1.iter().map(|ps| ps.identifier).collect::<Vec<_>>();

let containing_module;
match self.resolve_module_path(&module_path, UseLexicalScope, Some(span)) {
Expand Down Expand Up @@ -2715,7 +2711,7 @@ impl<'a> Resolver<'a> {
bool /* true if an error was reported */>
where T: Named,
{
let module_path = segments.split_last().unwrap().1.iter().map(T::name).collect::<Vec<_>>();
let module_path = segments.split_last().unwrap().1.iter().map(T::ident).collect::<Vec<_>>();
let root_module = self.graph_root;

let containing_module;
Expand All @@ -2734,7 +2730,7 @@ impl<'a> Resolver<'a> {
}
}

let name = segments.last().unwrap().name();
let name = segments.last().unwrap().ident().name;
let result =
self.resolve_name_in_module(containing_module, name, namespace, false, Some(span));
result.success().ok_or(false)
Expand Down Expand Up @@ -2976,9 +2972,8 @@ impl<'a> Resolver<'a> {
msg = format!("did you mean {}?", msg);
} else {
// we display a help message if this is a module
let name_path = path.segments.iter()
.map(|seg| seg.identifier.name)
.collect::<Vec<_>>();
let name_path: Vec<_> =
path.segments.iter().map(|seg| seg.identifier).collect();

match self.resolve_module_path(&name_path[..],
UseLexicalScope,
Expand Down Expand Up @@ -3317,7 +3312,7 @@ impl<'a> Resolver<'a> {
}
};

let segments: Vec<_> = path.segments.iter().map(|seg| seg.identifier.name).collect();
let segments: Vec<_> = path.segments.iter().map(|seg| seg.identifier).collect();
let mut path_resolution = err_path_resolution();
let vis = match self.resolve_module_path(&segments, DontUseLexicalScope, Some(path.span)) {
Success(module) => {
Expand Down Expand Up @@ -3469,26 +3464,24 @@ impl<'a> Resolver<'a> {
}
}

fn names_to_string(names: &[Name]) -> String {
fn names_to_string(names: &[Ident]) -> String {
let mut first = true;
let mut result = String::new();
for name in names {
for ident in names {
if first {
first = false
} else {
result.push_str("::")
}
result.push_str(&name.as_str());
result.push_str(&ident.name.as_str());
}
result
}

fn path_names_to_string(path: &Path, depth: usize) -> String {
let names: Vec<ast::Name> = path.segments[..path.segments.len() - depth]
.iter()
.map(|seg| seg.identifier.name)
.collect();
names_to_string(&names[..])
let names: Vec<_> =
path.segments[..path.segments.len() - depth].iter().map(|seg| seg.identifier).collect();
names_to_string(&names)
}

/// When an entity with a given name is not available in scope, we search for
Expand Down Expand Up @@ -3551,15 +3544,15 @@ fn show_candidates(session: &mut DiagnosticBuilder,
fn module_to_string(module: Module) -> String {
let mut names = Vec::new();

fn collect_mod(names: &mut Vec<ast::Name>, module: Module) {
fn collect_mod(names: &mut Vec<Ident>, module: Module) {
if let ModuleKind::Def(_, name) = module.kind {
if let Some(parent) = module.parent {
names.push(name);
names.push(Ident::with_empty_ctxt(name));
collect_mod(names, parent);
}
} else {
// danger, shouldn't be ident?
names.push(token::intern("<opaque>"));
names.push(token::str_to_ident("<opaque>"));
collect_mod(names, module.parent.unwrap());
}
}
Expand All @@ -3568,7 +3561,7 @@ fn module_to_string(module: Module) -> String {
if names.is_empty() {
return "???".to_string();
}
names_to_string(&names.into_iter().rev().collect::<Vec<ast::Name>>())
names_to_string(&names.into_iter().rev().collect::<Vec<_>>())
}

fn err_path_resolution() -> PathResolution {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -24,7 +24,7 @@ use rustc::ty;
use rustc::lint::builtin::PRIVATE_IN_PUBLIC;
use rustc::hir::def::*;

use syntax::ast::{NodeId, Name};
use syntax::ast::{Ident, NodeId, Name};
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::Span;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<'a> ImportDirectiveSubclass<'a> {
pub struct ImportDirective<'a> {
pub id: NodeId,
parent: Module<'a>,
module_path: Vec<Name>,
module_path: Vec<Ident>,
imported_module: Cell<Option<Module<'a>>>, // the resolution of `module_path`
subclass: ImportDirectiveSubclass<'a>,
span: Span,
Expand Down Expand Up @@ -252,7 +252,7 @@ impl<'a> Resolver<'a> {

// Add an import directive to the current module.
pub fn add_import_directive(&mut self,
module_path: Vec<Name>,
module_path: Vec<Ident>,
subclass: ImportDirectiveSubclass<'a>,
span: Span,
id: NodeId,
Expand Down Expand Up @@ -816,7 +816,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
}
}

fn import_path_to_string(names: &[Name], subclass: &ImportDirectiveSubclass) -> String {
fn import_path_to_string(names: &[Ident], subclass: &ImportDirectiveSubclass) -> String {
if names.is_empty() {
import_directive_subclass_to_string(subclass)
} else {
Expand Down

0 comments on commit 7b81106

Please sign in to comment.