Refactor PathKind
This commit is contained in:
parent
4a58522119
commit
aca022f1d4
6 changed files with 34 additions and 17 deletions
|
@ -582,8 +582,14 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
|
||||||
hir::PathKind::Abs => ps.push("".into()),
|
hir::PathKind::Abs => ps.push("".into()),
|
||||||
hir::PathKind::Crate => ps.push("crate".into()),
|
hir::PathKind::Crate => ps.push("crate".into()),
|
||||||
hir::PathKind::Plain => {}
|
hir::PathKind::Plain => {}
|
||||||
hir::PathKind::Self_ => ps.push("self".into()),
|
hir::PathKind::Super(0) => ps.push("self".into()),
|
||||||
hir::PathKind::Super => ps.push("super".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,
|
hir::PathKind::Type(_) | hir::PathKind::DollarCrate(_) => return None,
|
||||||
}
|
}
|
||||||
ps.extend(path.segments().iter().map(|it| it.name.to_string().into()));
|
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.
|
// We rewrite simple path `macro_name` to `self::macro_name` to force resolve in module scope only.
|
||||||
let mut path = mac.path.clone();
|
let mut path = mac.path.clone();
|
||||||
if path.is_ident() {
|
if path.is_ident() {
|
||||||
path.kind = PathKind::Self_;
|
path.kind = PathKind::Super(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.def_collector.unexpanded_macros.push(MacroDirective {
|
self.def_collector.unexpanded_macros.push(MacroDirective {
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
//!
|
//!
|
||||||
//! `ReachedFixedPoint` signals about this.
|
//! `ReachedFixedPoint` signals about this.
|
||||||
|
|
||||||
|
use std::iter::successors;
|
||||||
|
|
||||||
use hir_expand::name::Name;
|
use hir_expand::name::Name;
|
||||||
use ra_db::Edition;
|
use ra_db::Edition;
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
@ -97,9 +99,6 @@ impl CrateDefMap {
|
||||||
PathKind::Crate => {
|
PathKind::Crate => {
|
||||||
PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into())
|
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
|
// plain import or absolute path in 2015: crate-relative with
|
||||||
// fallback to extern prelude (with the simplification in
|
// fallback to extern prelude (with the simplification in
|
||||||
// rust-lang/rust#57745)
|
// rust-lang/rust#57745)
|
||||||
|
@ -123,9 +122,22 @@ impl CrateDefMap {
|
||||||
log::debug!("resolving {:?} in module", segment);
|
log::debug!("resolving {:?} in module", segment);
|
||||||
self.resolve_name_in_module(db, original_module, &segment, prefer_module(idx))
|
self.resolve_name_in_module(db, original_module, &segment, prefer_module(idx))
|
||||||
}
|
}
|
||||||
PathKind::Super => {
|
// PathKind::Self_ => {
|
||||||
if let Some(p) = self.modules[original_module].parent {
|
// PerNs::types(ModuleId { krate: self.krate, local_id: original_module }.into())
|
||||||
PerNs::types(ModuleId { krate: self.krate, local_id: p }.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 {
|
} else {
|
||||||
log::debug!("super path in root module");
|
log::debug!("super path in root module");
|
||||||
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
||||||
|
@ -170,7 +182,7 @@ impl CrateDefMap {
|
||||||
if module.krate != self.krate {
|
if module.krate != self.krate {
|
||||||
let path = ModPath {
|
let path = ModPath {
|
||||||
segments: path.segments[i..].to_vec(),
|
segments: path.segments[i..].to_vec(),
|
||||||
kind: PathKind::Self_,
|
kind: PathKind::Super(0),
|
||||||
};
|
};
|
||||||
log::debug!("resolving {:?} in other crate", path);
|
log::debug!("resolving {:?} in other crate", path);
|
||||||
let defp_map = db.crate_def_map(module.krate);
|
let defp_map = db.crate_def_map(module.krate);
|
||||||
|
|
|
@ -56,7 +56,7 @@ impl ModPath {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_self(&self) -> bool {
|
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.
|
/// 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)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum PathKind {
|
pub enum PathKind {
|
||||||
Plain,
|
Plain,
|
||||||
Self_,
|
Super(u8),
|
||||||
Super,
|
|
||||||
Crate,
|
Crate,
|
||||||
// Absolute path
|
// Absolute path
|
||||||
Abs,
|
Abs,
|
||||||
|
|
|
@ -95,11 +95,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::SelfKw => {
|
ast::PathSegmentKind::SelfKw => {
|
||||||
kind = PathKind::Self_;
|
kind = PathKind::Super(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::SuperKw => {
|
ast::PathSegmentKind::SuperKw => {
|
||||||
kind = PathKind::Super;
|
kind = PathKind::Super(1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,13 +95,13 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
|
||||||
if prefix.is_some() {
|
if prefix.is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
ModPath::from_simple_segments(PathKind::Self_, iter::empty())
|
ModPath::from_simple_segments(PathKind::Super(0), iter::empty())
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::SuperKw => {
|
ast::PathSegmentKind::SuperKw => {
|
||||||
if prefix.is_some() {
|
if prefix.is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
ModPath::from_simple_segments(PathKind::Super, iter::empty())
|
ModPath::from_simple_segments(PathKind::Super(1), iter::empty())
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::Type { .. } => {
|
ast::PathSegmentKind::Type { .. } => {
|
||||||
// not allowed in imports
|
// not allowed in imports
|
||||||
|
|
Loading…
Add table
Reference in a new issue