From d043689735edc12388e22f49b2319362f3b003fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 17:04:20 +0100 Subject: Touch up docs --- crates/ra_hir_def/src/path.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 3b26e8337..c37364f57 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -100,13 +100,14 @@ pub enum GenericArg { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum PathKind { Plain, + /// `self::` is `Super(0)` Super(u8), Crate, - // Absolute path + /// Absolute path (::foo) Abs, - // Type based path like `::foo` + /// Type based path like `::foo` Type(Box), - // `$crate` from macro expansion + /// `$crate` from macro expansion DollarCrate(CrateId), } -- cgit v1.2.3 From 88c6109897417e7ab815ea1711f49545bff94601 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 17:06:52 +0100 Subject: Move PathKindUp --- crates/ra_hir_def/src/path.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index c37364f57..e38d924fa 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -18,6 +18,20 @@ pub struct ModPath { pub segments: Vec, } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum PathKind { + Plain, + /// `self::` is `Super(0)` + Super(u8), + Crate, + /// Absolute path (::foo) + Abs, + /// Type based path like `::foo` + Type(Box), + /// `$crate` from macro expansion + DollarCrate(CrateId), +} + impl ModPath { pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option { lower::lower_path(path, hygiene).map(|it| it.mod_path) @@ -97,20 +111,6 @@ pub enum GenericArg { // or lifetime... } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum PathKind { - Plain, - /// `self::` is `Super(0)` - Super(u8), - Crate, - /// Absolute path (::foo) - Abs, - /// Type based path like `::foo` - Type(Box), - /// `$crate` from macro expansion - DollarCrate(CrateId), -} - impl Path { /// Converts an `ast::Path` to `Path`. Works with use trees. /// DEPRECATED: It does not handle `$crate` from macro call. -- cgit v1.2.3 From 04715cbe1caf92e55d393a352a12454ba958845e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 17:41:33 +0100 Subject: Forbid ::foo syntax in mod paths --- crates/ra_hir_def/src/body.rs | 12 ++++++++---- crates/ra_hir_def/src/nameres/path_resolution.rs | 5 ----- crates/ra_hir_def/src/path.rs | 12 +++++++++--- crates/ra_hir_def/src/path/lower.rs | 6 ++++-- 4 files changed, 21 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 7787cb87f..d4cab0561 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -15,7 +15,7 @@ use crate::{ db::DefDatabase, expr::{Expr, ExprId, Pat, PatId}, nameres::{BuiltinShadowMode, CrateDefMap}, - path::Path, + path::{ModPath, Path}, src::HasSource, DefWithBodyId, HasModule, Lookup, ModuleId, }; @@ -44,7 +44,7 @@ impl Expander { db.ast_id_map(self.current_file_id).ast_id(¯o_call), ); - if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) { + if let Some(path) = macro_call.path().and_then(|path| self.parse_mod_path(path)) { if let Some(def) = self.resolve_path_as_macro(db, &path) { let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id)); let file_id = call_id.as_file(); @@ -81,9 +81,13 @@ impl Expander { Path::from_src(path, &self.hygiene) } - fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &Path) -> Option { + fn parse_mod_path(&mut self, path: ast::Path) -> Option { + ModPath::from_src(path, &self.hygiene) + } + + fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &ModPath) -> Option { self.crate_def_map - .resolve_path(db, self.module.local_id, path.mod_path(), BuiltinShadowMode::Other) + .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other) .0 .take_macros() } diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 1dbc4f371..2dd779b66 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -145,11 +145,6 @@ impl CrateDefMap { return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude } } - PathKind::Type(_) => { - // This is handled in `infer::infer_path_expr` - // The result returned here does not matter - return ResolvePathResult::empty(ReachedFixedPoint::Yes); - } }; for (i, segment) in segments { diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index e38d924fa..1e2da6b48 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -26,8 +26,6 @@ pub enum PathKind { Crate, /// Absolute path (::foo) Abs, - /// Type based path like `::foo` - Type(Box), /// `$crate` from macro expansion DollarCrate(CrateId), } @@ -84,6 +82,8 @@ impl ModPath { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { + /// Type based path like `::foo` + type_anchor: Option>, mod_path: ModPath, /// Invariant: the same len as self.path.segments generic_args: Vec>>, @@ -126,7 +126,7 @@ impl Path { /// Converts an `ast::NameRef` into a single-identifier `Path`. pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path { - Path { mod_path: name_ref.as_name().into(), generic_args: vec![None] } + Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } } /// `true` if this path is just a standalone `self` @@ -138,6 +138,10 @@ impl Path { &self.mod_path.kind } + pub fn type_anchor(&self) -> Option<&TypeRef> { + self.type_anchor.as_ref().map(|it| &**it) + } + pub fn segments(&self) -> PathSegments<'_> { PathSegments { segments: self.mod_path.segments.as_slice(), @@ -154,6 +158,7 @@ impl Path { return None; } let res = Path { + type_anchor: self.type_anchor.clone(), mod_path: ModPath { kind: self.mod_path.kind.clone(), segments: self.mod_path.segments[..self.mod_path.segments.len() - 1].to_vec(), @@ -226,6 +231,7 @@ impl GenericArgs { impl From for Path { fn from(name: Name) -> Path { Path { + type_anchor: None, mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), generic_args: vec![None], } diff --git a/crates/ra_hir_def/src/path/lower.rs b/crates/ra_hir_def/src/path/lower.rs index c71b52d89..62aafd508 100644 --- a/crates/ra_hir_def/src/path/lower.rs +++ b/crates/ra_hir_def/src/path/lower.rs @@ -22,6 +22,7 @@ pub(super) use lower_use::lower_use_tree; /// It correctly handles `$crate` based path from macro call. pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option { let mut kind = PathKind::Plain; + let mut type_anchor = None; let mut segments = Vec::new(); let mut generic_args = Vec::new(); loop { @@ -63,7 +64,8 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option match trait_ref { // ::foo None => { - kind = PathKind::Type(Box::new(self_type)); + type_anchor = Some(Box::new(self_type)); + kind = PathKind::Plain; } // >::Foo desugars to Trait::Foo Some(trait_ref) => { @@ -111,7 +113,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option segments.reverse(); generic_args.reverse(); let mod_path = ModPath { kind, segments }; - return Some(Path { mod_path, generic_args }); + return Some(Path { type_anchor, mod_path, generic_args }); fn qualifier(path: &ast::Path) -> Option { if let Some(q) = path.qualifier() { -- cgit v1.2.3 From d33fc26e05c573f536e02afbffc354fa4f4a35f3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 17:42:49 +0100 Subject: Touch up docs --- crates/ra_hir_def/src/path.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 1e2da6b48..3fb0955d1 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -82,7 +82,8 @@ impl ModPath { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { - /// Type based path like `::foo` + /// Type based path like `::foo`. + /// Note that paths like `::foo` are desugard to `Trait::::foo`. type_anchor: Option>, mod_path: ModPath, /// Invariant: the same len as self.path.segments -- cgit v1.2.3 From afdeacf3c126b3e19bd1e50a912fee2b3f2d4aa9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Dec 2019 17:52:52 +0100 Subject: Remove dead code --- crates/ra_hir_def/src/path.rs | 5 ----- 1 file changed, 5 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 3fb0955d1..7302cf0f1 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -130,11 +130,6 @@ impl Path { Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } } - /// `true` if this path is just a standalone `self` - pub fn is_self(&self) -> bool { - self.mod_path.is_self() - } - pub fn kind(&self) -> &PathKind { &self.mod_path.kind } -- cgit v1.2.3