Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #18246 - ChayimFriedman2:fix-18238, r=Veykril
fix: Fix `prettify_macro_expansion()` when the node isn't the whole file Fixes #18238.
bors 2024-10-09
parent 9aa4293 · parent c8540e7 · commit e33e819
-rw-r--r--crates/hir-expand/src/prettify_macro_expansion_.rs5
-rw-r--r--crates/ide/src/hover/tests.rs30
2 files changed, 34 insertions, 1 deletions
diff --git a/crates/hir-expand/src/prettify_macro_expansion_.rs b/crates/hir-expand/src/prettify_macro_expansion_.rs
index d928cafdef..6ff7831fd8 100644
--- a/crates/hir-expand/src/prettify_macro_expansion_.rs
+++ b/crates/hir-expand/src/prettify_macro_expansion_.rs
@@ -15,11 +15,14 @@ pub fn prettify_macro_expansion(
span_map: &ExpansionSpanMap,
target_crate_id: CrateId,
) -> SyntaxNode {
+ // Because `syntax_bridge::prettify_macro_expansion::prettify_macro_expansion()` clones subtree for `syn`,
+ // that means it will be offsetted to the beginning.
+ let span_offset = syn.text_range().start();
let crate_graph = db.crate_graph();
let target_crate = &crate_graph[target_crate_id];
let mut syntax_ctx_id_to_dollar_crate_replacement = FxHashMap::default();
syntax_bridge::prettify_macro_expansion::prettify_macro_expansion(syn, &mut |dollar_crate| {
- let ctx = span_map.span_at(dollar_crate.text_range().start()).ctx;
+ let ctx = span_map.span_at(dollar_crate.text_range().start() + span_offset).ctx;
let replacement =
syntax_ctx_id_to_dollar_crate_replacement.entry(ctx).or_insert_with(|| {
let ctx_data = db.lookup_intern_syntax_context(ctx);
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index e60be577f7..81397b0785 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -8988,3 +8988,33 @@ mod m {
"#]],
);
}
+
+#[test]
+fn regression_18238() {
+ check(
+ r#"
+macro_rules! foo {
+ ($name:ident) => {
+ pub static $name = Foo::new(|| {
+ $crate;
+ });
+ };
+}
+
+foo!(BAR_$0);
+"#,
+ expect![[r#"
+ *BAR_*
+
+ ```rust
+ test
+ ```
+
+ ```rust
+ pub static BAR_: {error} = Foo::new(||{
+ crate;
+ })
+ ```
+ "#]],
+ );
+}