Skip to content

Commit 932e63b

Browse files
committed
Auto merge of rust-lang#13239 - mdx97:mathew/fix-add-reference-for-macros, r=Veykril
Fix add reference action on macros. Before we were using the range of the corresponding expression node in the macro expanded file, which is obviously incorrect as we are setting the text in the original source. For some reason, the test I added is failing and I haven't found a way to fix it. Does anyone know why `check_fix` wouldn't work with macros? Getting this error: ```text thread 'handlers::type_mismatch::tests::test_add_reference_to_macro_call' panicked at 'no diagnostics', crates/ide-diagnostics/src/handlers/type_mismatch.rs:317:9 ``` closes rust-lang#13219
2 parents b6e3f41 + a65ca20 commit 932e63b

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

crates/ide-diagnostics/src/handlers/type_mismatch.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ fn add_reference(
5959
d: &hir::TypeMismatch,
6060
acc: &mut Vec<Assist>,
6161
) -> Option<()> {
62-
let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
63-
let expr_node = d.expr.value.to_node(&root);
64-
6562
let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range;
6663

6764
let (_, mutability) = d.expected.as_reference()?;
@@ -72,7 +69,7 @@ fn add_reference(
7269

7370
let ampersands = format!("&{}", mutability.as_keyword_for_ref());
7471

75-
let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands);
72+
let edit = TextEdit::insert(range.start(), ampersands);
7673
let source_change =
7774
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
7875
acc.push(fix("add_reference_here", "Add reference here", source_change, range));
@@ -314,6 +311,34 @@ fn main() {
314311
);
315312
}
316313

314+
#[test]
315+
fn test_add_reference_to_macro_call() {
316+
check_fix(
317+
r#"
318+
macro_rules! thousand {
319+
() => {
320+
1000_u64
321+
};
322+
}
323+
fn test(foo: &u64) {}
324+
fn main() {
325+
test($0thousand!());
326+
}
327+
"#,
328+
r#"
329+
macro_rules! thousand {
330+
() => {
331+
1000_u64
332+
};
333+
}
334+
fn test(foo: &u64) {}
335+
fn main() {
336+
test(&thousand!());
337+
}
338+
"#,
339+
);
340+
}
341+
317342
#[test]
318343
fn test_add_mutable_reference_to_let_stmt() {
319344
check_fix(

0 commit comments

Comments
 (0)