]> git.lizzy.rs Git - rust.git/commitdiff
Merge #2581
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Tue, 17 Dec 2019 14:39:07 +0000 (14:39 +0000)
committerGitHub <noreply@github.com>
Tue, 17 Dec 2019 14:39:07 +0000 (14:39 +0000)
2581: Refactor PathKind r=matklad a=matklad

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
crates/ra_assists/src/assists/add_import.rs
crates/ra_hir_def/src/nameres/collector.rs
crates/ra_hir_def/src/nameres/path_resolution.rs
crates/ra_hir_def/src/path.rs
crates/ra_hir_def/src/path/lower.rs
crates/ra_hir_def/src/path/lower/lower_use.rs

index f81b4184a06b426f89f4abc8301fee6d84b0e7dc..ceffee9b87605f1c78d72218786acbeb4ea1f6d5 100644 (file)
@@ -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()));
index 912a073eac2dcff19b950037318981c7f4fdc582..8bbf7ffa27de502afc9d5aae4a84143b2bd70154 100644 (file)
@@ -890,7 +890,7 @@ fn collect_macro(&mut self, mac: &raw::MacroData) {
         // 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 {
index 4a249e7e727866d634e289a741a2a3338b11e049..a3bfc15421854ab2c111e2b7bb0d0c4a7cd7f0df 100644 (file)
@@ -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 @@ pub(super) fn resolve_path_fp_with_macro(
             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 @@ pub(super) fn resolve_path_fp_with_macro(
                 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 @@ pub(super) fn resolve_path_fp_with_macro(
                     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);
index 20d6d98ea036512720449a9321aed140dd50e9f6..3b26e8337fbfd38f2c5068d209ea7a5f26b2cd99 100644 (file)
@@ -56,7 +56,7 @@ pub fn is_ident(&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.
@@ -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,
index a2e99519802cf646d8363c336340902989dbd25b..c71b52d8988eb047488b4d1f11bb847bd8ef7b4e 100644 (file)
@@ -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;
             }
         }
index ea3fdb56cc2f3e6166e6ecdeadd091efbbffa8bc..062c02063e50c18dc8df305840a008694a5e28c2 100644 (file)
@@ -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