}
pub trait Resolver {
- // Resolve a global hir path generated by the lowerer when expanding `for`, `if let`, etc.
+ // Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
// Obtain the resolution for a node id
let proj_start = p.segments.len() - resolution.depth;
let path = P(hir::Path {
- global: p.global,
def: resolution.base_def,
segments: p.segments[..proj_start].iter().enumerate().map(|(i, segment)| {
let param_mode = match (qself_position, param_mode) {
id: NodeId,
p: &Path,
name: Option<Name>,
- param_mode: ParamMode)
+ param_mode: ParamMode,
+ defaults_to_global: bool)
-> hir::Path {
+ let mut segments = p.segments.iter();
+ if defaults_to_global && p.is_global() {
+ segments.next();
+ }
+
hir::Path {
- global: p.global,
def: self.expect_full_def(id),
- segments: p.segments.iter().map(|segment| {
+ segments: segments.map(|segment| {
self.lower_path_segment(segment, param_mode)
}).chain(name.map(|name| {
hir::PathSegment {
fn lower_path(&mut self,
id: NodeId,
p: &Path,
- param_mode: ParamMode)
+ param_mode: ParamMode,
+ defaults_to_global: bool)
-> hir::Path {
- self.lower_path_extra(id, p, None, param_mode)
+ self.lower_path_extra(id, p, None, param_mode, defaults_to_global)
}
fn lower_path_segment(&mut self,
// Check if the where clause type is a plain type parameter.
match bound_pred.bounded_ty.node {
TyKind::Path(None, ref path)
- if !path.global && path.segments.len() == 1 &&
- bound_pred.bound_lifetimes.is_empty() => {
+ if path.segments.len() == 1 &&
+ bound_pred.bound_lifetimes.is_empty() => {
if let Some(Def::TyParam(def_id)) =
self.resolver.get_resolution(bound_pred.bounded_ty.id)
.map(|d| d.base_def) {
span}) => {
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
id: id,
- path: self.lower_path(id, path, ParamMode::Explicit),
+ path: self.lower_path(id, path, ParamMode::Explicit, false),
ty: self.lower_ty(ty),
span: span,
})
fn lower_trait_ref(&mut self, p: &TraitRef) -> hir::TraitRef {
hir::TraitRef {
- path: self.lower_path(p.ref_id, &p.path, ParamMode::Explicit),
+ path: self.lower_path(p.ref_id, &p.path, ParamMode::Explicit, false),
ref_id: p.ref_id,
}
}
};
let mut path = self.lower_path_extra(import.id, path, suffix,
- ParamMode::Explicit);
+ ParamMode::Explicit, true);
path.span = span;
self.items.insert(import.id, hir::Item {
id: import.id,
path
}
};
- let path = P(self.lower_path(id, path, ParamMode::Explicit));
+ let path = P(self.lower_path(id, path, ParamMode::Explicit, true));
let kind = match view_path.node {
ViewPathSimple(ident, _) => {
*name = ident.name;
Some(def) => {
hir::PatKind::Path(hir::QPath::Resolved(None, P(hir::Path {
span: pth1.span,
- global: false,
def: def,
segments: hir_vec![
hir::PathSegment::from_name(pth1.node.name)
Visibility::Crate(_) => hir::Visibility::Crate,
Visibility::Restricted { ref path, id } => {
hir::Visibility::Restricted {
- path: P(self.lower_path(id, path, ParamMode::Explicit)),
+ path: P(self.lower_path(id, path, ParamMode::Explicit, true)),
id: id
}
}
let expr_path = hir::ExprPath(hir::QPath::Resolved(None, P(hir::Path {
span: span,
- global: false,
def: def,
segments: hir_vec![hir::PathSegment::from_name(id)],
})));
/// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
/// The path is also resolved according to `is_value`.
fn std_path(&mut self, span: Span, components: &[&str], is_value: bool) -> hir::Path {
- let idents = self.crate_root.iter().chain(components);
-
- let segments: Vec<_> = idents.map(|name| {
- hir::PathSegment::from_name(Symbol::intern(name))
- }).collect();
-
let mut path = hir::Path {
span: span,
- global: true,
def: Def::Err,
- segments: segments.into(),
+ segments: iter::once(keywords::CrateRoot.name()).chain({
+ self.crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
+ }).map(hir::PathSegment::from_name).collect(),
};
self.resolver.resolve_hir_path(&mut path, is_value);
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Path {
pub span: Span,
- /// A `::foo` path, is relative to the crate root rather than current
- /// module (like paths in an import).
- pub global: bool,
/// The definition that the path resolved to.
pub def: Def,
/// The segments in the path: the things separated by `::`.
pub segments: HirVec<PathSegment>,
}
+impl Path {
+ pub fn is_global(&self) -> bool {
+ !self.segments.is_empty() && self.segments[0].name == keywords::CrateRoot.name()
+ }
+}
+
impl fmt::Debug for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "path({})", print::path_to_string(self))
-> io::Result<()> {
self.maybe_print_comment(path.span.lo)?;
- let mut first = !path.global;
- for segment in &path.segments {
- if first {
- first = false
- } else {
+ for (i, segment) in path.segments.iter().enumerate() {
+ if i > 0 {
word(&mut self.s, "::")?
}
-
- self.print_name(segment.name)?;
-
- self.print_path_parameters(&segment.parameters, colons_before_params)?;
+ if segment.name != keywords::CrateRoot.name() {
+ self.print_name(segment.name)?;
+ self.print_path_parameters(&segment.parameters, colons_before_params)?;
+ }
}
Ok(())
space(&mut self.s)?;
self.word_space("as")?;
- let mut first = !path.global;
- for segment in &path.segments[..path.segments.len() - 1] {
- if first {
- first = false
- } else {
+ for (i, segment) in path.segments[..path.segments.len() - 1].iter().enumerate() {
+ if i > 0 {
word(&mut self.s, "::")?
}
- self.print_name(segment.name)?;
- self.print_path_parameters(&segment.parameters, colons_before_params)?;
+ if segment.name != keywords::CrateRoot.name() {
+ self.print_name(segment.name)?;
+ self.print_path_parameters(&segment.parameters, colons_before_params)?;
+ }
}
word(&mut self.s, ">")?;
new_segs.push(new_seg);
hir::Path {
span: path.span,
- global: path.global,
def: path.def,
segments: new_segs.into()
}
let v = ctor.variant_for_adt(adt);
let qpath = hir::QPath::Resolved(None, P(hir::Path {
span: DUMMY_SP,
- global: false,
def: Def::Err,
segments: vec![hir::PathSegment::from_name(v.name)].into(),
}));
SawStructField,
SawVariant,
SawQPath,
- SawPath(bool),
SawPathSegment,
SawPathParameters,
SawBlock,
fn visit_path(&mut self, path: &'tcx Path, _: ast::NodeId) {
debug!("visit_path: st={:?}", self.st);
- SawPath(path.global).hash(self.st);
hash_span!(self, path.span);
visit::walk_path(self, path)
}
fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
// Lint for constants that look like binding identifiers (#7526)
if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.node {
- if !path.global && path.segments.len() == 1 && path.segments[0].parameters.is_empty() {
+ if path.segments.len() == 1 && path.segments[0].parameters.is_empty() {
if let Def::Const(..) = path.def {
NonUpperCaseGlobals::check_upper_case(cx,
"constant in pattern",
}
fn visit_path(&mut self, path: &'a Path, id: NodeId) {
- if path.global && path.segments.len() > 0 {
- let ident = path.segments[0].identifier;
+ if path.segments.len() >= 2 && path.is_global() {
+ let ident = path.segments[1].identifier;
if token::Ident(ident).is_path_segment_keyword() {
self.session.add_lint(lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH,
id,
use syntax::ext::expand::mark_tts;
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
+use syntax::parse::token;
use syntax::symbol::keywords;
use syntax::visit::{self, Visitor};
// 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<_> = match view_path.node {
+ let mut module_path: Vec<_> = match view_path.node {
ViewPathSimple(_, ref full_path) => {
full_path.segments
.split_last()
}
};
+ // This can be removed once warning cycle #36888 is complete.
+ if module_path.len() >= 2 && module_path[0].name == keywords::CrateRoot.name() &&
+ token::Ident(module_path[1]).is_path_segment_keyword() {
+ module_path.remove(0);
+ }
+
// Build up the import directives.
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
let rename = node.rename.unwrap_or(node.name);
(module_path.clone(), node.name, rename)
} else {
- let ident = match module_path.last() {
- Some(&ident) => ident,
- None => {
- resolve_error(
- self,
- source_item.span,
- ResolutionError::
- SelfImportOnlyInImportListWithNonEmptyPrefix
- );
- continue;
- }
- };
+ let ident = *module_path.last().unwrap();
+ if ident.name == keywords::CrateRoot.name() {
+ resolve_error(
+ self,
+ source_item.span,
+ ResolutionError::
+ SelfImportOnlyInImportListWithNonEmptyPrefix
+ );
+ continue;
+ }
let module_path = module_path.split_last().unwrap().1;
let rename = node.rename.unwrap_or(ident);
(module_path.to_vec(), ident, rename)
fn visit_poly_trait_ref(&mut self,
tref: &'tcx ast::PolyTraitRef,
m: &'tcx ast::TraitBoundModifier) {
- let ast::Path { ref segments, span, global } = tref.trait_ref.path;
+ let ast::Path { ref segments, span } = tref.trait_ref.path;
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
- let def = self.resolve_trait_reference(&path, global, None, span);
+ let def = self.resolve_trait_reference(&path, None, span);
self.record_def(tref.trait_ref.ref_id, def);
visit::walk_poly_trait_ref(self, tref, m);
}
}
}
-#[derive(Copy, Clone, PartialEq)]
-enum PathScope {
- Global,
- Lexical,
- Import,
-}
-
#[derive(Clone)]
enum PathResult<'a> {
Module(Module<'a>),
resolutions: RefCell<FxHashMap<(Ident, Namespace), &'a RefCell<NameResolution<'a>>>>,
legacy_macro_resolutions: RefCell<Vec<(Mark, Ident, Span)>>,
- macro_resolutions: RefCell<Vec<(Box<[Ident]>, PathScope, Span)>>,
+ macro_resolutions: RefCell<Vec<(Box<[Ident]>, Span)>>,
// Macro invocations that can expand into items in this module.
unresolved_invocations: RefCell<FxHashSet<Mark>>,
impl<'a> hir::lowering::Resolver for Resolver<'a> {
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool) {
let namespace = if is_value { ValueNS } else { TypeNS };
- let hir::Path { ref segments, span, global, ref mut def } = *path;
+ let hir::Path { ref segments, span, ref mut def } = *path;
let path: Vec<_> = segments.iter().map(|seg| Ident::with_empty_ctxt(seg.name)).collect();
- let scope = if global { PathScope::Global } else { PathScope::Lexical };
- match self.resolve_path(&path, scope, Some(namespace), Some(span)) {
+ match self.resolve_path(&path, Some(namespace), Some(span)) {
PathResult::Module(module) => *def = module.def().unwrap(),
PathResult::NonModule(path_res) if path_res.depth == 0 => *def = path_res.base_def,
- PathResult::NonModule(..) => match self.resolve_path(&path, scope, None, Some(span)) {
+ PathResult::NonModule(..) => match self.resolve_path(&path, None, Some(span)) {
PathResult::Failed(msg, _) => {
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
}
prefix.segments.iter().map(|seg| seg.identifier).collect();
// Resolve prefix of an import with empty braces (issue #28388)
if items.is_empty() && !prefix.segments.is_empty() {
- let (scope, span) = (PathScope::Import, prefix.span);
+ let span = prefix.span;
// FIXME(#38012) This should be a module path, not anything in TypeNS.
- let result =
- self.resolve_path(&path, scope, Some(TypeNS), Some(span));
+ let result = self.resolve_path(&path, Some(TypeNS), Some(span));
let (def, msg) = match result {
PathResult::Module(module) => (module.def().unwrap(), None),
PathResult::NonModule(res) if res.depth == 0 =>
(res.base_def, None),
PathResult::NonModule(_) => {
// Resolve a module path for better errors
- match self.resolve_path(&path, scope, None, Some(span)) {
+ match self.resolve_path(&path, None, Some(span)) {
PathResult::Failed(msg, _) => (Def::Err, Some(msg)),
_ => unreachable!(),
}
fn resolve_trait_reference(&mut self,
path: &[Ident],
- global: bool,
generics: Option<&Generics>,
span: Span)
-> PathResolution {
- let scope = if global { PathScope::Global } else { PathScope::Lexical };
- let def = match self.resolve_path(path, scope, None, Some(span)) {
+ let def = match self.resolve_path(path, None, Some(span)) {
PathResult::Module(module) => Some(module.def().unwrap()),
PathResult::NonModule(..) => return err_path_resolution(),
PathResult::Failed(msg, false) => {
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
return err_path_resolution();
}
- _ => match self.resolve_path(path, scope, Some(TypeNS), None) {
+ _ => match self.resolve_path(path, Some(TypeNS), None) {
PathResult::NonModule(path_resolution) => Some(path_resolution.base_def),
_ => None,
},
let mut new_val = None;
let mut new_id = None;
if let Some(trait_ref) = opt_trait_ref {
- let ast::Path { ref segments, span, global } = trait_ref.path;
+ let ast::Path { ref segments, span } = trait_ref.path;
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
- let path_res = self.resolve_trait_reference(&path, global, generics, span);
+ let path_res = self.resolve_trait_reference(&path, generics, span);
assert!(path_res.depth == 0);
self.record_def(trait_ref.ref_id, path_res);
if path_res.base_def != Def::Err {
path: &Path,
ns: Namespace)
-> Option<PathResolution> {
- let ast::Path { ref segments, global, span } = *path;
+ let ast::Path { ref segments, span } = *path;
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
- let scope = if global { PathScope::Global } else { PathScope::Lexical };
if let Some(qself) = maybe_qself {
if qself.position == 0 {
});
}
// Make sure the trait is valid.
- self.resolve_trait_reference(&path[..qself.position], global, None, span);
+ self.resolve_trait_reference(&path[..qself.position], None, span);
}
- let result = match self.resolve_path(&path, scope, Some(ns), Some(span)) {
+ let result = match self.resolve_path(&path, Some(ns), Some(span)) {
PathResult::NonModule(path_res) => match path_res.base_def {
Def::Trait(..) if maybe_qself.is_some() => return None,
_ => path_res,
// Such behavior is required for backward compatibility.
// The same fallback is used when `a` resolves to nothing.
PathResult::Module(..) | PathResult::Failed(..)
- if scope == PathScope::Lexical && (ns == TypeNS || path.len() > 1) &&
+ if (ns == TypeNS || path.len() > 1) &&
self.primitive_type_table.primitive_types.contains_key(&path[0].name) => {
PathResolution {
base_def: Def::PrimTy(self.primitive_type_table.primitive_types[&path[0].name]),
}
let unqualified_result = {
- match self.resolve_path(&[*path.last().unwrap()], PathScope::Lexical, Some(ns), None) {
+ match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) {
PathResult::NonModule(path_res) => path_res.base_def,
PathResult::Module(module) => module.def().unwrap(),
_ => return Some(result),
fn resolve_path(&mut self,
path: &[Ident],
- scope: PathScope,
opt_ns: Option<Namespace>, // `None` indicates a module path
record_used: Option<Span>)
-> PathResult<'a> {
- let (mut module, allow_self) = match scope {
- PathScope::Lexical => (None, true),
- PathScope::Import => (Some(self.graph_root), true),
- PathScope::Global => (Some(self.graph_root), false),
- };
- let mut allow_super = allow_self;
+ let mut module = None;
+ let mut allow_super = true;
for (i, &ident) in path.iter().enumerate() {
let is_last = i == path.len() - 1;
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
- if i == 0 && allow_self && ns == TypeNS && ident.name == keywords::SelfValue.name() {
+ if i == 0 && ns == TypeNS && ident.name == keywords::SelfValue.name() {
module = Some(self.module_map[&self.current_module.normal_ancestor_id.unwrap()]);
continue
- } else if i == 0 && allow_self && ns == TypeNS && ident.name == "$crate" {
- module = Some(self.resolve_crate_var(ident.ctxt));
- continue
} else if allow_super && ns == TypeNS && ident.name == keywords::Super.name() {
let current_module = if i == 0 { self.current_module } else { module.unwrap() };
let self_module = self.module_map[¤t_module.normal_ancestor_id.unwrap()];
}
allow_super = false;
+ if i == 0 && ns == TypeNS && ident.name == keywords::CrateRoot.name() {
+ module = Some(self.graph_root);
+ continue
+ } else if i == 0 && ns == TypeNS && ident.name == "$crate" {
+ module = Some(self.resolve_crate_var(ident.ctxt));
+ continue
+ }
+
let binding = if let Some(module) = module {
self.resolve_ident_in_module(module, ident, ns, false, record_used)
} else if opt_ns == Some(MacroNS) {
}
}
- PathResult::Module(module.unwrap())
+ PathResult::Module(module.unwrap_or(self.graph_root))
}
// Resolve a local definition, potentially adjusting for closures.
} else {
// Be helpful if the name refers to a struct
let path_name = path_names_to_string(path, 0);
- let ast::Path { ref segments, global, .. } = *path;
- let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
- let scope = if global { PathScope::Global } else { PathScope::Lexical };
- let type_res = match self.resolve_path(&path, scope, Some(TypeNS), None) {
+ let path: Vec<_> = path.segments.iter().map(|seg| seg.identifier).collect();
+ let type_res = match self.resolve_path(&path, Some(TypeNS), None) {
PathResult::NonModule(type_res) => Some(type_res),
_ => None,
};
} else {
// we display a help message if this is a module
if let PathResult::Module(module) =
- self.resolve_path(&path, scope, None, None) {
+ self.resolve_path(&path, None, None) {
def = module.def().unwrap();
context = UnresolvedNameContext::PathIsMod(parent);
}
segms.push(ident.into());
let path = Path {
span: span,
- global: false,
segments: segms,
};
// the entity is accessible in the following cases:
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
let mut path_resolution = err_path_resolution();
- let vis = match self.resolve_path(&path, PathScope::Import, None, Some(span)) {
+ let vis = match self.resolve_path(&path, None, Some(span)) {
PathResult::Module(module) => {
path_resolution = PathResolution::new(module.def().unwrap());
ty::Visibility::Restricted(module.normal_ancestor_id.unwrap())
}
fn names_to_string(names: &[Ident]) -> String {
- let mut first = true;
let mut result = String::new();
- for ident in names {
- if first {
- first = false
- } else {
- result.push_str("::")
+ for (i, ident) in names.iter().enumerate() {
+ if i > 0 {
+ result.push_str("::");
+ }
+ if ident.name != keywords::CrateRoot.name() {
+ result.push_str(&ident.name.as_str());
}
- result.push_str(&ident.name.as_str());
}
result
}
// except according to those terms.
use {AmbiguityError, Resolver, ResolutionError, resolve_error};
-use {Module, ModuleKind, NameBinding, NameBindingKind, PathScope, PathResult};
+use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
use Namespace::{self, MacroNS};
use build_reduced_graph::BuildReducedGraphVisitor;
use resolve_imports::ImportResolver;
use syntax::feature_gate::{emit_feature_err, GateIssue};
use syntax::fold::Folder;
use syntax::ptr::P;
+use syntax::symbol::keywords;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax::visit::Visitor;
use syntax_pos::{Span, DUMMY_SP};
fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
let ident = path.segments[0].identifier;
if ident.name == "$crate" {
- path.global = true;
+ path.segments[0].identifier.name = keywords::CrateRoot.name();
let module = self.0.resolve_crate_var(ident.ctxt);
- if module.is_local() {
- path.segments.remove(0);
- } else {
- path.segments[0].identifier = match module.kind {
- ModuleKind::Def(_, name) => ast::Ident::with_empty_ctxt(name),
+ if !module.is_local() {
+ path.segments.insert(1, match module.kind {
+ ModuleKind::Def(_, name) => ast::Ident::with_empty_ctxt(name).into(),
_ => unreachable!(),
- };
+ })
}
}
path
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
-> Result<Rc<SyntaxExtension>, Determinacy> {
- let ast::Path { ref segments, global, span } = *path;
+ let ast::Path { ref segments, span } = *path;
if segments.iter().any(|segment| segment.parameters.is_some()) {
let kind =
if segments.last().unwrap().parameters.is_some() { "macro" } else { "module" };
return Err(Determinacy::Determined);
}
- let path_scope = if global { PathScope::Global } else { PathScope::Lexical };
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
let invocation = self.invocations[&scope];
self.current_module = invocation.module.get();
- if path.len() > 1 || global {
+ if path.len() > 1 {
if !self.use_extern_macros {
let msg = "non-ident macro paths are experimental";
let feature = "use_extern_macros";
return Err(Determinacy::Determined);
}
- let ext = match self.resolve_path(&path, path_scope, Some(MacroNS), None) {
+ let ext = match self.resolve_path(&path, Some(MacroNS), None) {
PathResult::NonModule(path_res) => match path_res.base_def {
Def::Err => Err(Determinacy::Determined),
def @ _ => Ok(self.get_macro(def)),
_ => Err(Determinacy::Determined),
};
self.current_module.macro_resolutions.borrow_mut()
- .push((path.into_boxed_slice(), path_scope, span));
+ .push((path.into_boxed_slice(), span));
return ext;
}
pub fn finalize_current_module_macro_resolutions(&mut self) {
let module = self.current_module;
- for &(ref path, scope, span) in module.macro_resolutions.borrow().iter() {
- match self.resolve_path(path, scope, Some(MacroNS), Some(span)) {
+ for &(ref path, span) in module.macro_resolutions.borrow().iter() {
+ match self.resolve_path(path, Some(MacroNS), Some(span)) {
PathResult::NonModule(_) => {},
PathResult::Failed(msg, _) => {
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
use {AmbiguityError, Module, PerNS};
use Namespace::{self, TypeNS, MacroNS};
-use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError};
+use {NameBinding, NameBindingKind, PathResult, PrivacyError};
use Resolver;
use {names_to_string, module_to_string};
use {resolve_error, ResolutionError};
use syntax::ast::{Ident, NodeId};
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
use syntax::ext::hygiene::Mark;
+use syntax::parse::token;
use syntax::symbol::keywords;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::Span;
// For better failure detection, pretend that the import will not define any names
// while resolving its module path.
directive.vis.set(ty::Visibility::PrivateExternal);
- let result = self.resolve_path(&directive.module_path, PathScope::Import, None, None);
+ let result = self.resolve_path(&directive.module_path, None, None);
directive.vis.set(vis);
match result {
self.current_module = directive.parent;
let ImportDirective { ref module_path, span, .. } = *directive;
- let module_result = self.resolve_path(&module_path, PathScope::Import, None, Some(span));
+ let module_result = self.resolve_path(&module_path, None, Some(span));
let module = match module_result {
PathResult::Module(module) => module,
PathResult::Failed(msg, _) => {
- let mut path = vec![keywords::SelfValue.ident()];
- path.extend(module_path);
- let result = self.resolve_path(&path, PathScope::Import, None, None);
- return if let PathResult::Module(..) = result {
- Some(format!("Did you mean `self::{}`?", &names_to_string(module_path)))
+ let (mut self_path, mut self_result) = (module_path.clone(), None);
+ if !self_path.is_empty() && !token::Ident(self_path[0]).is_path_segment_keyword() {
+ self_path[0].name = keywords::SelfValue.name();
+ self_result = Some(self.resolve_path(&self_path, None, None));
+ }
+ return if let Some(PathResult::Module(..)) = self_result {
+ Some(format!("Did you mean `{}`?", names_to_string(&self_path)))
} else {
Some(msg)
};
}
fn import_path_to_string(names: &[Ident], subclass: &ImportDirectiveSubclass) -> String {
+ let global = !names.is_empty() && names[0].name == keywords::CrateRoot.name();
+ let names = if global { &names[1..] } else { names };
if names.is_empty() {
import_directive_subclass_to_string(subclass)
} else {
// a str representation of the entire prefix.
fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
let spans = self.span.spans_for_path_segments(path);
+ let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
// Paths to enums seem to not match their spans - the span includes all the
// variants too. But they seem to always be at the end, so I hope we can cope with
// always using the first ones. So, only error out if we don't have enough spans.
// What could go wrong...?
- if spans.len() < path.segments.len() {
+ if spans.len() < segments.len() {
if generated_code(path.span) {
return vec![];
}
error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:",
path_to_string(path),
spans.len(),
- path.segments.len());
+ segments.len());
for s in &spans {
let loc = self.sess.codemap().lookup_char_pos(s.lo);
error!(" '{}' in {}, line {}",
let mut result: Vec<(Span, String)> = vec![];
let mut segs = vec![];
- for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
+ for (i, (seg, span)) in segments.iter().zip(&spans).enumerate() {
segs.push(seg.clone());
let sub_path = ast::Path {
span: *span, // span for the last segment
- global: path.global,
segments: segs,
};
- let qualname = if i == 0 && path.global {
+ let qualname = if i == 0 && path.is_global() {
format!("::{}", path_to_string(&sub_path))
} else {
path_to_string(&sub_path)
result
}
- // The global arg allows us to override the global-ness of the path (which
- // actually means 'does the path start with `::`', rather than 'is the path
- // semantically global). We use the override for `use` imports (etc.) where
- // the syntax is non-global, but the semantics are global.
- fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
+ fn write_sub_paths(&mut self, path: &ast::Path) {
let sub_paths = self.process_path_prefixes(path);
- for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
- let qualname = if i == 0 && global && !path.global {
- format!("::{}", qualname)
- } else {
- qualname.clone()
- };
+ for (span, qualname) in sub_paths {
self.dumper.mod_ref(ModRefData {
- span: *span,
+ span: span,
qualname: qualname,
scope: self.cur_scope,
ref_id: None
// As write_sub_paths, but does not process the last ident in the path (assuming it
// will be processed elsewhere). See note on write_sub_paths about global.
- fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
+ fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
let sub_paths = self.process_path_prefixes(path);
let len = sub_paths.len();
if len <= 1 {
return;
}
- let sub_paths = &sub_paths[..len-1];
- for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
- let qualname = if i == 0 && global && !path.global {
- format!("::{}", qualname)
- } else {
- qualname.clone()
- };
+ for (span, qualname) in sub_paths.into_iter().take(len - 1) {
self.dumper.mod_ref(ModRefData {
- span: *span,
+ span: span,
qualname: qualname,
scope: self.cur_scope,
ref_id: None
Def::Union(..) |
Def::Variant(..) |
Def::TyAlias(..) |
- Def::AssociatedTy(..) => self.write_sub_paths_truncated(path, false),
+ Def::AssociatedTy(..) => self.write_sub_paths_truncated(path),
_ => {}
}
}
fields: &'l [ast::Field],
variant: &'l ty::VariantDef,
base: &'l Option<P<ast::Expr>>) {
- self.write_sub_paths_truncated(path, false);
+ self.write_sub_paths_truncated(path);
if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
down_cast_data!(struct_lit_data, TypeRefData, ex.span);
visibility: From::from(&item.vis),
}.lower(self.tcx));
}
- self.write_sub_paths_truncated(path, true);
+ self.write_sub_paths_truncated(path);
}
ast::ViewPathGlob(ref path) => {
// Make a comma-separated list of names of imported modules.
visibility: From::from(&item.vis),
}.lower(self.tcx));
}
- self.write_sub_paths(path, true);
+ self.write_sub_paths(path);
}
ast::ViewPathList(ref path, ref list) => {
for plid in list {
}
}
- self.write_sub_paths(path, true);
+ self.write_sub_paths(path);
}
}
}
}.lower(self.tcx));
}
- self.write_sub_paths_truncated(path, false);
+ self.write_sub_paths_truncated(path);
visit::walk_path(self, path);
}
segments.pop();
let trait_path = hir::Path {
span: p.span,
- global: p.global,
def: Def::Trait(cx.tcx.associated_item(p.def.def_id()).container.id()),
segments: segments.into(),
};
}
let trait_path = hir::Path {
span: self.span,
- global: false,
def: def,
segments: vec![].into(),
};
impl Clean<Path> for hir::Path {
fn clean(&self, cx: &DocContext) -> Path {
Path {
- global: self.global,
+ global: self.is_global(),
def: self.def,
- segments: self.segments.clean(cx),
+ segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
}
}
}
}
fn qpath_to_string(p: &hir::QPath) -> String {
- let (segments, global) = match *p {
- hir::QPath::Resolved(_, ref path) => {
- (&path.segments, path.global)
- }
- hir::QPath::TypeRelative(_, ref segment) => {
- return segment.name.to_string()
- }
+ let segments = match *p {
+ hir::QPath::Resolved(_, ref path) => &path.segments,
+ hir::QPath::TypeRelative(_, ref segment) => return segment.name.to_string(),
};
let mut s = String::new();
- let mut first = true;
- for i in segments.iter().map(|x| x.name.as_str()) {
- if !first || global {
+ for (i, seg) in segments.iter().enumerate() {
+ if i > 0 {
s.push_str("::");
- } else {
- first = false;
}
- s.push_str(&i);
+ if seg.name != keywords::CrateRoot.name() {
+ s.push_str(&*seg.name.as_str());
+ }
}
s
}
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub struct Path {
pub span: Span,
- /// A `::foo` path, is relative to the crate root rather than current
- /// module (like paths in an import).
- pub global: bool,
/// The segments in the path: the things separated by `::`.
+ /// Global paths begin with `keywords::CrateRoot`.
pub segments: Vec<PathSegment>,
}
pub fn from_ident(s: Span, identifier: Ident) -> Path {
Path {
span: s,
- global: false,
segments: vec![identifier.into()],
}
}
+
+ pub fn default_to_global(mut self) -> Path {
+ let name = self.segments[0].identifier.name;
+ if !self.is_global() && name != "$crate" &&
+ name != keywords::SelfValue.name() && name != keywords::Super.name() {
+ self.segments.insert(0, PathSegment::crate_root());
+ }
+ self
+ }
+
+ pub fn is_global(&self) -> bool {
+ !self.segments.is_empty() && self.segments[0].identifier.name == keywords::CrateRoot.name()
+ }
}
/// A segment of a path: an identifier, an optional lifetime, and a set of types.
}
}
+impl PathSegment {
+ pub fn crate_root() -> Self {
+ PathSegment {
+ identifier: keywords::CrateRoot.ident(),
+ parameters: None,
+ }
+ }
+}
+
/// Parameters of a path segment.
///
/// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
bindings: Vec<ast::TypeBinding> )
-> ast::Path {
let last_identifier = idents.pop().unwrap();
- let mut segments: Vec<ast::PathSegment> = idents.into_iter().map(Into::into).collect();
+ let mut segments: Vec<ast::PathSegment> = Vec::new();
+ if global {
+ segments.push(ast::PathSegment::crate_root());
+ }
+
+ segments.extend(idents.into_iter().map(Into::into));
let parameters = if lifetimes.is_empty() && types.is_empty() && bindings.is_empty() {
None
} else {
segments.push(ast::PathSegment { identifier: last_identifier, parameters: parameters });
ast::Path {
span: sp,
- global: global,
segments: segments,
}
}
pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
fn mac_placeholder() -> ast::Mac {
dummy_spanned(ast::Mac_ {
- path: ast::Path { span: DUMMY_SP, global: false, segments: Vec::new() },
+ path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
tts: Vec::new(),
})
}
i
}
-pub fn noop_fold_path<T: Folder>(Path {global, segments, span}: Path, fld: &mut T) -> Path {
+pub fn noop_fold_path<T: Folder>(Path { segments, span }: Path, fld: &mut T) -> Path {
Path {
- global: global,
segments: segments.move_map(|PathSegment {identifier, parameters}| PathSegment {
identifier: fld.fold_ident(identifier),
parameters: parameters.map(|ps| ps.map(|ps| fld.fold_path_parameters(ps))),
id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Path(None, ast::Path {
span: sp(0, 1),
- global: false,
segments: vec![Ident::from_str("a").into()],
}),
span: sp(0, 1),
id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Path(None, ast::Path {
span: sp(0, 6),
- global: true,
- segments: vec![Ident::from_str("a").into(), Ident::from_str("b").into()],
+ segments: vec![ast::PathSegment::crate_root(),
+ Ident::from_str("a").into(),
+ Ident::from_str("b").into()]
}),
span: sp(0, 6),
attrs: ThinVec::new(),
id: ast::DUMMY_NODE_ID,
node:ast::ExprKind::Path(None, ast::Path{
span: sp(7, 8),
- global: false,
segments: vec![Ident::from_str("d").into()],
}),
span:sp(7,8),
id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Path(None, ast::Path {
span:sp(0,1),
- global:false,
segments: vec![Ident::from_str("b").into()],
}),
span: sp(0,1),
ty: P(ast::Ty{id: ast::DUMMY_NODE_ID,
node: ast::TyKind::Path(None, ast::Path{
span:sp(10,13),
- global:false,
segments: vec![Ident::from_str("i32").into()],
}),
span:sp(10,13)
node: ast::ExprKind::Path(None,
ast::Path{
span:sp(17,18),
- global:false,
segments: vec![Ident::from_str("b").into()],
}),
span: sp(17,18),
} else {
ast::Path {
span: span,
- global: false,
segments: vec![]
}
};
// Parse any number of segments and bound sets. A segment is an
// identifier followed by an optional lifetime and a set of types.
// A bound set is a set of type parameter bounds.
- let segments = match mode {
+ let mut segments = match mode {
PathStyle::Type => {
self.parse_path_segments_without_colons()?
}
}
};
+ if is_global {
+ segments.insert(0, ast::PathSegment::crate_root());
+ }
+
// Assemble the span.
let span = mk_sp(lo, self.prev_span.hi);
// Assemble the result.
Ok(ast::Path {
span: span,
- global: is_global,
segments: segments,
})
}
} else if self.eat_keyword(keywords::Crate) {
pub_crate(self)
} else {
- let path = self.parse_path(PathStyle::Mod)?;
+ let path = self.parse_path(PathStyle::Mod)?.default_to_global();
self.expect(&token::CloseDelim(token::Paren))?;
Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
}
if self.check(&token::OpenDelim(token::Brace)) || self.check(&token::BinOp(token::Star)) ||
self.is_import_coupler() {
// `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`.
+ self.eat(&token::ModSep);
let prefix = ast::Path {
- global: self.eat(&token::ModSep),
- segments: Vec::new(),
+ segments: vec![ast::PathSegment::crate_root()],
span: mk_sp(lo, self.span.hi),
};
let view_path_kind = if self.eat(&token::BinOp(token::Star)) {
};
Ok(P(spanned(lo, self.span.hi, view_path_kind)))
} else {
- let prefix = self.parse_path(PathStyle::Mod)?;
+ let prefix = self.parse_path(PathStyle::Mod)?.default_to_global();
if self.is_import_coupler() {
// `foo::bar::{a, b}` or `foo::bar::*`
self.bump();
}
pub fn path_to_string(p: &ast::Path) -> String {
- to_string(|s| s.print_path(p, false, 0))
+ to_string(|s| s.print_path(p, false, 0, false))
}
pub fn ident_to_string(id: ast::Ident) -> String {
match *vis {
ast::Visibility::Public => format!("pub {}", s),
ast::Visibility::Crate(_) => format!("pub(crate) {}", s),
- ast::Visibility::Restricted { ref path, .. } => format!("pub({}) {}", path, s),
+ ast::Visibility::Restricted { ref path, .. } =>
+ format!("pub({}) {}", to_string(|s| s.print_path(path, false, 0, true)), s),
ast::Visibility::Inherited => s.to_string()
}
}
&generics));
}
ast::TyKind::Path(None, ref path) => {
- try!(self.print_path(path, false, 0));
+ try!(self.print_path(path, false, 0, false));
}
ast::TyKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, false))
}
ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => {
try!(self.print_visibility(&item.vis));
- try!(self.print_path(&node.path, false, 0));
+ try!(self.print_path(&node.path, false, 0, false));
try!(word(&mut self.s, "! "));
try!(self.print_ident(item.ident));
try!(self.cbox(INDENT_UNIT));
}
fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
- self.print_path(&t.path, false, 0)
+ self.print_path(&t.path, false, 0, false)
}
fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
match *vis {
ast::Visibility::Public => self.word_nbsp("pub"),
ast::Visibility::Crate(_) => self.word_nbsp("pub(crate)"),
- ast::Visibility::Restricted { ref path, .. } =>
- self.word_nbsp(&format!("pub({})", path)),
+ ast::Visibility::Restricted { ref path, .. } => {
+ let path = to_string(|s| s.print_path(path, false, 0, true));
+ self.word_nbsp(&format!("pub({})", path))
+ }
ast::Visibility::Inherited => Ok(())
}
}
}
ast::TraitItemKind::Macro(codemap::Spanned { ref node, .. }) => {
// code copied from ItemKind::Mac:
- self.print_path(&node.path, false, 0)?;
+ self.print_path(&node.path, false, 0, false)?;
word(&mut self.s, "! ")?;
self.cbox(INDENT_UNIT)?;
self.popen()?;
}
ast::ImplItemKind::Macro(codemap::Spanned { ref node, .. }) => {
// code copied from ItemKind::Mac:
- try!(self.print_path(&node.path, false, 0));
+ try!(self.print_path(&node.path, false, 0, false));
try!(word(&mut self.s, "! "));
try!(self.cbox(INDENT_UNIT));
try!(self.popen());
pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
-> io::Result<()> {
- try!(self.print_path(&m.node.path, false, 0));
+ try!(self.print_path(&m.node.path, false, 0, false));
try!(word(&mut self.s, "!"));
match delim {
token::Paren => try!(self.popen()),
fields: &[ast::Field],
wth: &Option<P<ast::Expr>>,
attrs: &[Attribute]) -> io::Result<()> {
- try!(self.print_path(path, true, 0));
+ try!(self.print_path(path, true, 0, false));
try!(word(&mut self.s, "{"));
try!(self.print_inner_attributes_inline(attrs));
try!(self.commasep_cmnt(
}
}
ast::ExprKind::Path(None, ref path) => {
- try!(self.print_path(path, true, 0))
+ try!(self.print_path(path, true, 0, false))
}
ast::ExprKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, true))
fn print_path(&mut self,
path: &ast::Path,
colons_before_params: bool,
- depth: usize)
+ depth: usize,
+ defaults_to_global: bool)
-> io::Result<()>
{
try!(self.maybe_print_comment(path.span.lo));
- let mut first = !path.global;
- for segment in &path.segments[..path.segments.len()-depth] {
- if first {
- first = false
- } else {
+ let mut segments = path.segments[..path.segments.len()-depth].iter();
+ if defaults_to_global && path.is_global() {
+ segments.next();
+ }
+ for (i, segment) in segments.enumerate() {
+ if i > 0 {
try!(word(&mut self.s, "::"))
}
-
- try!(self.print_ident(segment.identifier));
-
- if let Some(ref parameters) = segment.parameters {
- try!(self.print_path_parameters(parameters, colons_before_params))
+ if segment.identifier.name != keywords::CrateRoot.name() {
+ try!(self.print_ident(segment.identifier));
+ if let Some(ref parameters) = segment.parameters {
+ try!(self.print_path_parameters(parameters, colons_before_params));
+ }
}
}
try!(space(&mut self.s));
try!(self.word_space("as"));
let depth = path.segments.len() - qself.position;
- try!(self.print_path(&path, false, depth));
+ try!(self.print_path(&path, false, depth, false));
}
try!(word(&mut self.s, ">"));
try!(word(&mut self.s, "::"));
}
}
PatKind::TupleStruct(ref path, ref elts, ddpos) => {
- try!(self.print_path(path, true, 0));
+ try!(self.print_path(path, true, 0, false));
try!(self.popen());
if let Some(ddpos) = ddpos {
try!(self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p)));
try!(self.pclose());
}
PatKind::Path(None, ref path) => {
- try!(self.print_path(path, true, 0));
+ try!(self.print_path(path, true, 0, false));
}
PatKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, false));
}
PatKind::Struct(ref path, ref fields, etc) => {
- try!(self.print_path(path, true, 0));
+ try!(self.print_path(path, true, 0, false));
try!(self.nbsp());
try!(self.word_space("{"));
try!(self.commasep_cmnt(
try!(self.print_lifetime_bounds(lifetime, bounds));
}
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
- try!(self.print_path(path, false, 0));
+ try!(self.print_path(path, false, 0, false));
try!(space(&mut self.s));
try!(self.word_space("="));
try!(self.print_type(&ty));
pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
match vp.node {
ast::ViewPathSimple(ident, ref path) => {
- try!(self.print_path(path, false, 0));
+ try!(self.print_path(path, false, 0, true));
if path.segments.last().unwrap().identifier.name !=
ident.name {
}
ast::ViewPathGlob(ref path) => {
- try!(self.print_path(path, false, 0));
+ try!(self.print_path(path, false, 0, true));
word(&mut self.s, "::*")
}
if path.segments.is_empty() {
try!(word(&mut self.s, "{"));
} else {
- try!(self.print_path(path, false, 0));
+ try!(self.print_path(path, false, 0, true));
try!(word(&mut self.s, "::{"));
}
try!(self.commasep(Inconsistent, &idents[..], |s, w| {
}],
vis: ast::Visibility::Inherited,
node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
- global: false,
- segments: vec![name, "prelude", "v1"].into_iter().map(|name| {
+ segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| {
ast::Ident::from_str(name).into()
}).collect(),
span: span,
(53, Default, "default")
(54, StaticLifetime, "'static")
(55, Union, "union")
+
+ // A virtual keyword that resolves to the crate root when used in a lexical scope.
+ (56, CrateRoot, "{{root}}")
}
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.
fn path_node(ids: Vec<Ident>) -> ast::Path {
ast::Path {
span: DUMMY_SP,
- global: false,
segments: ids.into_iter().map(Into::into).collect(),
}
}
fn path(&self) -> ast::Path {
ast::Path {
span: self.span,
- global: false,
segments: vec![self.ident.into()],
}
}
impl<'a, 'b> visit::Visitor<'a> for Visitor<'a, 'b> {
fn visit_ty(&mut self, ty: &'a ast::Ty) {
- match ty.node {
- ast::TyKind::Path(_, ref path) if !path.global => {
- if let Some(segment) = path.segments.first() {
- if self.ty_param_names.contains(&segment.identifier.name) {
- self.types.push(P(ty.clone()));
- }
+ if let ast::TyKind::Path(_, ref path) = ty.node {
+ if let Some(segment) = path.segments.first() {
+ if self.ty_param_names.contains(&segment.identifier.name) {
+ self.types.push(P(ty.clone()));
}
}
- _ => {}
}
visit::walk_ty(self, ty)
// Make sure primitive type fallback doesn't work with global paths
let _: ::u8;
- //~^ ERROR type name `u8` is undefined or not in scope
+ //~^ ERROR type name `::u8` is undefined or not in scope
}