Move store TypeRef of type based path in PathKind

This commit is contained in:
uHOOCCOOHu 2019-09-15 19:48:24 +08:00
parent 4926bed426
commit de9670fe45
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
4 changed files with 11 additions and 21 deletions

View file

@ -512,7 +512,7 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
hir::PathKind::Plain => {} hir::PathKind::Plain => {}
hir::PathKind::Self_ => ps.push("self".into()), hir::PathKind::Self_ => ps.push("self".into()),
hir::PathKind::Super => ps.push("super".into()), hir::PathKind::Super => ps.push("super".into()),
hir::PathKind::Type => return None, hir::PathKind::Type(_) => return None,
} }
for s in path.segments.iter() { for s in path.segments.iter() {
ps.push(s.name.to_string().into()); ps.push(s.name.to_string().into());

View file

@ -382,7 +382,7 @@ impl CrateDefMap {
return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
} }
} }
PathKind::Type => { PathKind::Type(_) => {
// This is handled in `infer::infer_path_expr` // This is handled in `infer::infer_path_expr`
// The result returned here does not matter // The result returned here does not matter
return ResolvePathResult::empty(ReachedFixedPoint::Yes); return ResolvePathResult::empty(ReachedFixedPoint::Yes);
@ -406,11 +406,8 @@ impl CrateDefMap {
curr_per_ns = match curr { curr_per_ns = match curr {
ModuleDef::Module(module) => { ModuleDef::Module(module) => {
if module.krate != self.krate { if module.krate != self.krate {
let path = Path { let path =
segments: path.segments[i..].to_vec(), Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ };
kind: PathKind::Self_,
type_ref: None,
};
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);
let (def, s) = defp_map.resolve_path(db, module.module_id, &path); let (def, s) = defp_map.resolve_path(db, module.module_id, &path);

View file

@ -10,7 +10,6 @@ use crate::{name, type_ref::TypeRef, AsName, Name};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path { pub struct Path {
pub kind: PathKind, pub kind: PathKind,
pub type_ref: Option<Box<TypeRef>>,
pub segments: Vec<PathSegment>, pub segments: Vec<PathSegment>,
} }
@ -43,7 +42,7 @@ pub enum GenericArg {
// or lifetime... // or lifetime...
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathKind { pub enum PathKind {
Plain, Plain,
Self_, Self_,
@ -52,7 +51,7 @@ pub enum PathKind {
// Absolute path // Absolute path
Abs, Abs,
// Type based path like `<T>::foo` // Type based path like `<T>::foo`
Type, Type(Box<TypeRef>),
} }
impl Path { impl Path {
@ -69,7 +68,6 @@ impl Path {
pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path { pub fn from_simple_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> Path {
Path { Path {
kind, kind,
type_ref: None,
segments: segments segments: segments
.into_iter() .into_iter()
.map(|name| PathSegment { name, args_and_bindings: None }) .map(|name| PathSegment { name, args_and_bindings: None })
@ -81,7 +79,6 @@ impl Path {
pub fn from_ast(mut path: ast::Path) -> Option<Path> { pub fn from_ast(mut path: ast::Path) -> Option<Path> {
let mut kind = PathKind::Plain; let mut kind = PathKind::Plain;
let mut segments = Vec::new(); let mut segments = Vec::new();
let mut path_type_ref = None;
loop { loop {
let segment = path.segment()?; let segment = path.segment()?;
@ -112,8 +109,7 @@ impl Path {
match trait_ref { match trait_ref {
// <T>::foo // <T>::foo
None => { None => {
kind = PathKind::Type; kind = PathKind::Type(Box::new(self_type));
path_type_ref = Some(Box::new(self_type));
} }
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
Some(trait_ref) => { Some(trait_ref) => {
@ -154,7 +150,7 @@ impl Path {
}; };
} }
segments.reverse(); segments.reverse();
return Some(Path { kind, type_ref: path_type_ref, segments }); return Some(Path { kind, segments });
fn qualifier(path: &ast::Path) -> Option<ast::Path> { fn qualifier(path: &ast::Path) -> Option<ast::Path> {
if let Some(q) = path.qualifier() { if let Some(q) = path.qualifier() {
@ -309,11 +305,8 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
let res = match segment.kind()? { let res = match segment.kind()? {
ast::PathSegmentKind::Name(name) => { ast::PathSegmentKind::Name(name) => {
// no type args in use // no type args in use
let mut res = prefix.unwrap_or_else(|| Path { let mut res = prefix
kind: PathKind::Plain, .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
type_ref: None,
segments: Vec::with_capacity(1),
});
res.segments.push(PathSegment { res.segments.push(PathSegment {
name: name.as_name(), name: name.as_name(),
args_and_bindings: None, // no type args in use args_and_bindings: None, // no type args in use

View file

@ -190,7 +190,7 @@ impl Resolver {
db: &impl HirDatabase, db: &impl HirDatabase,
path: &'p Path, path: &'p Path,
) -> Option<ResolveValueResult<'p>> { ) -> Option<ResolveValueResult<'p>> {
if let Some(type_ref) = &path.type_ref { if let PathKind::Type(type_ref) = &path.kind {
return Some(ResolveValueResult::TypeRef(type_ref)); return Some(ResolveValueResult::TypeRef(type_ref));
} }