about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2021-02-05 19:24:03 +0100
committerJonas Schievink <jonasschievink@gmail.com>2021-02-05 19:24:03 +0100
commit997bd97b77e0cacf7eb8e466071e416492cc24b3 (patch)
tree903383bc4aec9ff57974289a1dce5bcbc7c37fb1
parent80ab753d7e0bf59b81df317d6ddda43cb919ec83 (diff)
Fix resolution of `self` module within blocks
-rw-r--r--crates/hir_def/src/body/tests/block.rs14
-rw-r--r--crates/hir_def/src/nameres/path_resolution.rs10
2 files changed, 17 insertions, 7 deletions
diff --git a/crates/hir_def/src/body/tests/block.rs b/crates/hir_def/src/body/tests/block.rs
index 062560a70df..e688c0989de 100644
--- a/crates/hir_def/src/body/tests/block.rs
+++ b/crates/hir_def/src/body/tests/block.rs
@@ -26,9 +26,10 @@ fn outer() {
 fn use_from_crate() {
     check_at(
         r#"
-struct Struct;
+struct Struct {}
 fn outer() {
-    use Struct;
+    fn Struct() {}
+    use Struct as PlainStruct;
     use crate::Struct as CrateStruct;
     use self::Struct as SelfStruct;
     $0
@@ -36,12 +37,13 @@ fn outer() {
 "#,
         expect![[r#"
             block scope
-            CrateStruct: t v
-            SelfStruct: t v
-            Struct: t v
+            CrateStruct: t
+            PlainStruct: t v
+            SelfStruct: t
+            Struct: v
 
             crate
-            Struct: t v
+            Struct: t
             outer: v
         "#]],
     );
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs
index 036e389b01d..fdcdc23ae73 100644
--- a/crates/hir_def/src/nameres/path_resolution.rs
+++ b/crates/hir_def/src/nameres/path_resolution.rs
@@ -227,7 +227,15 @@ impl DefMap {
                     }
                 }
 
-                PerNs::types(self.module_id(module).into(), Visibility::Public)
+                // Resolve `self` to the containing crate-rooted module if we're a block
+                self.with_ancestor_maps(db, module, &mut |def_map, module| {
+                    if def_map.block.is_some() {
+                        None // keep ascending
+                    } else {
+                        Some(PerNs::types(def_map.module_id(module).into(), Visibility::Public))
+                    }
+                })
+                .expect("block DefMap not rooted in crate DefMap")
             }
             PathKind::Abs => {
                 // 2018-style absolute path -- only extern prelude