about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-17 11:23:05 +0000
committerbors <bors@rust-lang.org>2022-09-17 11:23:05 +0000
commit932e63b5d4358de4775649f99a71430ae4318db1 (patch)
treeb8ab9723f7d06d5f741abcc100a1f21518a27394
parentb6e3f41c2a8dc2559e3cd22fddadc505556aa1fa (diff)
parenta65ca20210b4cf82c32f11249208f990af3fb816 (diff)
downloadrust-932e63b5d4358de4775649f99a71430ae4318db1.tar.gz
rust-932e63b5d4358de4775649f99a71430ae4318db1.zip
Auto merge of #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 #13219
-rw-r--r--crates/ide-diagnostics/src/handlers/type_mismatch.rs33
1 files changed, 29 insertions, 4 deletions
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
index 6bf90e645b4..62c69f90baa 100644
--- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs
+++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
@@ -59,9 +59,6 @@ fn add_reference(
     d: &hir::TypeMismatch,
     acc: &mut Vec<Assist>,
 ) -> Option<()> {
-    let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
-    let expr_node = d.expr.value.to_node(&root);
-
     let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range;
 
     let (_, mutability) = d.expected.as_reference()?;
@@ -72,7 +69,7 @@ fn add_reference(
 
     let ampersands = format!("&{}", mutability.as_keyword_for_ref());
 
-    let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands);
+    let edit = TextEdit::insert(range.start(), ampersands);
     let source_change =
         SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
     acc.push(fix("add_reference_here", "Add reference here", source_change, range));
@@ -315,6 +312,34 @@ fn main() {
     }
 
     #[test]
+    fn test_add_reference_to_macro_call() {
+        check_fix(
+            r#"
+macro_rules! thousand {
+    () => {
+        1000_u64
+    };
+}
+fn test(foo: &u64) {}
+fn main() {
+    test($0thousand!());
+}
+            "#,
+            r#"
+macro_rules! thousand {
+    () => {
+        1000_u64
+    };
+}
+fn test(foo: &u64) {}
+fn main() {
+    test(&thousand!());
+}
+            "#,
+        );
+    }
+
+    #[test]
     fn test_add_mutable_reference_to_let_stmt() {
         check_fix(
             r#"