about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonas.schievink@ferrous-systems.com>2022-02-25 18:38:51 +0100
committerJonas Schievink <jonas.schievink@ferrous-systems.com>2022-02-25 18:38:51 +0100
commita247fffdf618cc39ce83d181d4c58da1ec8803a2 (patch)
tree81ec5a9d2f7bf5adf7e5731050665753a8dbea5e
parentab896e38e124676c4d0532b74f4d0d714b33b2f9 (diff)
downloadrust-a247fffdf618cc39ce83d181d4c58da1ec8803a2.tar.gz
rust-a247fffdf618cc39ce83d181d4c58da1ec8803a2.zip
Resolve `$crate` in `HirDisplay` of `Path`
-rw-r--r--crates/hir_ty/src/display.rs13
-rw-r--r--crates/ide/src/hover/tests.rs30
2 files changed, 42 insertions, 1 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 0e75ddeabcd..2020834fbc4 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -1189,7 +1189,18 @@ impl HirDisplay for Path {
                     write!(f, "super")?;
                 }
             }
-            (_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?,
+            (_, PathKind::DollarCrate(id)) => {
+                // Resolve `$crate` to the crate's display name.
+                // FIXME: should use the dependency name instead if available, but that depends on
+                // the crate invoking `HirDisplay`
+                let crate_graph = f.db.crate_graph();
+                let name = crate_graph[*id]
+                    .display_name
+                    .as_ref()
+                    .map(|name| name.canonical_name())
+                    .unwrap_or("$crate");
+                write!(f, "{name}")?
+            }
         }
 
         for (seg_idx, segment) in self.segments().iter().enumerate() {
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index a232ebd4fb7..df0ca941c99 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -4583,3 +4583,33 @@ pub struct Foo;
         "##]],
     );
 }
+
+#[test]
+fn hover_dollar_crate() {
+    // $crate should be resolved to the right crate name.
+
+    check(
+        r#"
+//- /main.rs crate:main deps:dep
+dep::m!(KONST$0);
+//- /dep.rs crate:dep
+#[macro_export]
+macro_rules! m {
+    ( $name:ident ) => { const $name: $crate::Type = $crate::Type; };
+}
+
+pub struct Type;
+"#,
+        expect![[r#"
+            *KONST*
+
+            ```rust
+            main
+            ```
+
+            ```rust
+            const KONST: dep::Type = $crate::Type
+            ```
+        "#]],
+    );
+}