]> git.lizzy.rs Git - rust.git/commitdiff
Fix resolution of `self` module within blocks
authorJonas Schievink <jonasschievink@gmail.com>
Fri, 5 Feb 2021 18:24:03 +0000 (19:24 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Fri, 5 Feb 2021 18:24:03 +0000 (19:24 +0100)
crates/hir_def/src/body/tests/block.rs
crates/hir_def/src/nameres/path_resolution.rs

index 062560a70dfb4b2fbda763c7fa7f9ed9020ca859..e688c0989de7e463eb6f4fe4438db7ead5a4ae07 100644 (file)
@@ -26,9 +26,10 @@ fn inner() {}
 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
         "#]],
     );
index 036e389b01d4d16b67f13b3ec83303aea002c347..fdcdc23ae73f6970b66d285080816349cea3a6bf 100644 (file)
@@ -227,7 +227,15 @@ pub(super) fn resolve_path_fp_with_macro_single(
                     }
                 }
 
-                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