mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 06:35:27 +00:00
Refactor PathKind
This commit is contained in:
parent
4a58522119
commit
aca022f1d4
@ -582,8 +582,14 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
|
||||
hir::PathKind::Abs => ps.push("".into()),
|
||||
hir::PathKind::Crate => ps.push("crate".into()),
|
||||
hir::PathKind::Plain => {}
|
||||
hir::PathKind::Self_ => ps.push("self".into()),
|
||||
hir::PathKind::Super => ps.push("super".into()),
|
||||
hir::PathKind::Super(0) => ps.push("self".into()),
|
||||
hir::PathKind::Super(lvl) => {
|
||||
let mut chain = "super".to_string();
|
||||
for _ in 0..*lvl {
|
||||
chain += "::super";
|
||||
}
|
||||
ps.push(chain.into());
|
||||
}
|
||||
hir::PathKind::Type(_) | hir::PathKind::DollarCrate(_) => return None,
|
||||
}
|
||||
ps.extend(path.segments().iter().map(|it| it.name.to_string().into()));
|
||||
|
@ -890,7 +890,7 @@ where
|
||||
// We rewrite simple path `macro_name` to `self::macro_name` to force resolve in module scope only.
|
||||
let mut path = mac.path.clone();
|
||||
if path.is_ident() {
|
||||
path.kind = PathKind::Self_;
|
||||
path.kind = PathKind::Super(0);
|
||||
}
|
||||
|
||||
self.def_collector.unexpanded_macros.push(MacroDirective {
|
||||
|
@ -10,6 +10,8 @@
|
||||
//!
|
||||
//! `ReachedFixedPoint` signals about this.
|
||||
|
||||
use std::iter::successors;
|
||||
|
||||
use hir_expand::name::Name;
|
||||
use ra_db::Edition;
|
||||
use test_utils::tested_by;
|
||||
@ -97,9 +99,6 @@ impl CrateDefMap {
|
||||
PathKind::Crate => {
|
||||
PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into())
|
||||
}
|
||||
PathKind::Self_ => {
|
||||
PerNs::types(ModuleId { krate: self.krate, local_id: original_module }.into())
|
||||
}
|
||||
// plain import or absolute path in 2015: crate-relative with
|
||||
// fallback to extern prelude (with the simplification in
|
||||
// rust-lang/rust#57745)
|
||||
@ -123,9 +122,22 @@ impl CrateDefMap {
|
||||
log::debug!("resolving {:?} in module", segment);
|
||||
self.resolve_name_in_module(db, original_module, &segment, prefer_module(idx))
|
||||
}
|
||||
PathKind::Super => {
|
||||
if let Some(p) = self.modules[original_module].parent {
|
||||
PerNs::types(ModuleId { krate: self.krate, local_id: p }.into())
|
||||
// PathKind::Self_ => {
|
||||
// PerNs::types(ModuleId { krate: self.krate, local_id: original_module }.into())
|
||||
// }
|
||||
// PathKind::Super => {
|
||||
// if let Some(p) = self.modules[original_module].parent {
|
||||
// PerNs::types(ModuleId { krate: self.krate, local_id: p }.into())
|
||||
// } else {
|
||||
// log::debug!("super path in root module");
|
||||
// return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
||||
// }
|
||||
// }
|
||||
PathKind::Super(lvl) => {
|
||||
let m = successors(Some(original_module), |m| self.modules[*m].parent)
|
||||
.nth(lvl as usize);
|
||||
if let Some(local_id) = m {
|
||||
PerNs::types(ModuleId { krate: self.krate, local_id }.into())
|
||||
} else {
|
||||
log::debug!("super path in root module");
|
||||
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
||||
@ -170,7 +182,7 @@ impl CrateDefMap {
|
||||
if module.krate != self.krate {
|
||||
let path = ModPath {
|
||||
segments: path.segments[i..].to_vec(),
|
||||
kind: PathKind::Self_,
|
||||
kind: PathKind::Super(0),
|
||||
};
|
||||
log::debug!("resolving {:?} in other crate", path);
|
||||
let defp_map = db.crate_def_map(module.krate);
|
||||
|
@ -56,7 +56,7 @@ impl ModPath {
|
||||
}
|
||||
|
||||
pub fn is_self(&self) -> bool {
|
||||
self.kind == PathKind::Self_ && self.segments.is_empty()
|
||||
self.kind == PathKind::Super(0) && self.segments.is_empty()
|
||||
}
|
||||
|
||||
/// If this path is a single identifier, like `foo`, return its name.
|
||||
@ -100,8 +100,7 @@ pub enum GenericArg {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum PathKind {
|
||||
Plain,
|
||||
Self_,
|
||||
Super,
|
||||
Super(u8),
|
||||
Crate,
|
||||
// Absolute path
|
||||
Abs,
|
||||
|
@ -95,11 +95,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
|
||||
break;
|
||||
}
|
||||
ast::PathSegmentKind::SelfKw => {
|
||||
kind = PathKind::Self_;
|
||||
kind = PathKind::Super(0);
|
||||
break;
|
||||
}
|
||||
ast::PathSegmentKind::SuperKw => {
|
||||
kind = PathKind::Super;
|
||||
kind = PathKind::Super(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -95,13 +95,13 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_simple_segments(PathKind::Self_, iter::empty())
|
||||
ModPath::from_simple_segments(PathKind::Super(0), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::SuperKw => {
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_simple_segments(PathKind::Super, iter::empty())
|
||||
ModPath::from_simple_segments(PathKind::Super(1), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::Type { .. } => {
|
||||
// not allowed in imports
|
||||
|
Loading…
Reference in New Issue
Block a user