]> git.lizzy.rs Git - rust.git/commitdiff
Fix resolution of `crate` paths from within blocks
authorJonas Schievink <jonasschievink@gmail.com>
Tue, 2 Feb 2021 17:02:12 +0000 (18:02 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Tue, 2 Feb 2021 17:02:12 +0000 (18:02 +0100)
They resolve to the crate root, not the DefMap's root module (which
can be a block)

crates/hir_def/src/nameres.rs
crates/hir_def/src/nameres/path_resolution.rs

index 5efc2fe470a8ac8bb77f634913aaee77d926ece5..0a15fc4704076c6bf346c87dcb359d01d66a0e65 100644 (file)
@@ -275,6 +275,11 @@ pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId {
         ModuleId { krate: self.krate, local_id, block }
     }
 
+    pub(crate) fn crate_root(&self) -> ModuleId {
+        let (root_map, _) = self.ancestor_maps(self.root).last().unwrap();
+        root_map.module_id(root_map.root)
+    }
+
     pub(crate) fn resolve_path(
         &self,
         db: &dyn DefDatabase,
index 2d1477160e46d0ab9c7bf1584f578796991ec9bb..ecf75c777cff129e1fe943e4d9351fd187713706 100644 (file)
@@ -152,7 +152,7 @@ pub(super) fn resolve_path_fp_with_macro_single(
             PathKind::DollarCrate(krate) => {
                 if krate == self.krate {
                     mark::hit!(macro_dollar_crate_self);
-                    PerNs::types(self.module_id(self.root).into(), Visibility::Public)
+                    PerNs::types(self.crate_root().into(), Visibility::Public)
                 } else {
                     let def_map = db.crate_def_map(krate);
                     let module = def_map.module_id(def_map.root);
@@ -160,7 +160,7 @@ pub(super) fn resolve_path_fp_with_macro_single(
                     PerNs::types(module.into(), Visibility::Public)
                 }
             }
-            PathKind::Crate => PerNs::types(self.module_id(self.root).into(), Visibility::Public),
+            PathKind::Crate => PerNs::types(self.crate_root().into(), Visibility::Public),
             // plain import or absolute path in 2015: crate-relative with
             // fallback to extern prelude (with the simplification in
             // rust-lang/rust#57745)