about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-08 13:56:58 +0000
committerbors <bors@rust-lang.org>2024-02-08 13:56:58 +0000
commitddf26113fc799298a426a08bff24fe5f3f5dc40d (patch)
tree2262e3c4eef293d99cf2f07ed444e84278e3bed1
parent6655960186632f0f267e58a783c0172faaf77c85 (diff)
parent545382db2532801bd6fb05532d71114f8fb509fd (diff)
downloadrust-ddf26113fc799298a426a08bff24fe5f3f5dc40d.tar.gz
rust-ddf26113fc799298a426a08bff24fe5f3f5dc40d.zip
Auto merge of #16462 - Veykril:proc-error, r=Veykril
Better error message for when proc-macros have not yet been built

Closes https://github.com/rust-lang/rust-analyzer/issues/16331
-rw-r--r--crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs2
-rw-r--r--crates/ide-diagnostics/src/handlers/missing_fields.rs27
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs2
-rw-r--r--crates/rust-analyzer/src/reload.rs8
4 files changed, 21 insertions, 18 deletions
diff --git a/crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs b/crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs
index 51be8960b8c..712842372b6 100644
--- a/crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs
+++ b/crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs
@@ -359,7 +359,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
         &'a self,
         ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>,
         ty: &'a Self::Ty,
-    ) -> impl Iterator<Item = Self::Ty> + ExactSizeIterator + Captures<'a> {
+    ) -> impl ExactSizeIterator<Item = Self::Ty> + Captures<'a> {
         let single = |ty| smallvec![ty];
         let tys: SmallVec<[_; 2]> = match ctor {
             Struct | Variant(_) | UnionField => match ty.kind(Interner) {
diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs
index 37ac912f064..3bc043c8fc6 100644
--- a/crates/ide-diagnostics/src/handlers/missing_fields.rs
+++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs
@@ -59,9 +59,15 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
 
     let current_module =
         ctx.sema.scope(d.field_list_parent.to_node(&root).syntax()).map(|it| it.module());
+    let range = InFile::new(d.file, d.field_list_parent.text_range())
+        .original_node_file_range_rooted(ctx.sema.db);
 
-    let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
+    let build_text_edit = |new_syntax: &SyntaxNode, old_syntax| {
         let edit = {
+            let old_range = ctx.sema.original_range_opt(old_syntax)?;
+            if old_range.file_id != range.file_id {
+                return None;
+            }
             let mut builder = TextEdit::builder();
             if d.file.is_macro() {
                 // we can't map the diff up into the macro input unfortunately, as the macro loses all
@@ -69,8 +75,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
                 // This has the downside that the cursor will be moved in macros by doing it without a diff
                 // but that is a trade off we can make.
                 // FIXME: this also currently discards a lot of whitespace in the input... we really need a formatter here
-                let range = ctx.sema.original_range_opt(old_syntax)?;
-                builder.replace(range.range, new_syntax.to_string());
+                builder.replace(old_range.range, new_syntax.to_string());
             } else {
                 algo::diff(old_syntax, new_syntax).into_text_edit(&mut builder);
             }
@@ -79,8 +84,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
         Some(vec![fix(
             "fill_missing_fields",
             "Fill struct fields",
-            SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
-            ctx.sema.original_range(parent_syntax).range,
+            SourceChange::from_text_edit(range.file_id, edit),
+            range.range,
         )])
     };
 
@@ -143,11 +148,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
                 );
                 new_field_list.add_field(field.clone_for_update());
             }
-            build_text_edit(
-                field_list_parent.syntax(),
-                new_field_list.syntax(),
-                old_field_list.syntax(),
-            )
+            build_text_edit(new_field_list.syntax(), old_field_list.syntax())
         }
         Either::Right(field_list_parent) => {
             let missing_fields = ctx.sema.record_pattern_missing_fields(field_list_parent);
@@ -160,11 +161,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
                 ));
                 new_field_list.add_field(field.clone_for_update());
             }
-            build_text_edit(
-                field_list_parent.syntax(),
-                new_field_list.syntax(),
-                old_field_list.syntax(),
-            )
+            build_text_edit(new_field_list.syntax(), old_field_list.syntax())
         }
     }
 }
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs b/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
index 015a3d6b2ce..340f77feea8 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
@@ -32,7 +32,7 @@ pub(crate) fn unresolved_proc_macro(
     let severity = if config_enabled { Severity::Error } else { Severity::WeakWarning };
     let def_map = ctx.sema.db.crate_def_map(d.krate);
     let message = if config_enabled {
-        def_map.proc_macro_loading_error().unwrap_or("proc macro not found in the built dylib")
+        def_map.proc_macro_loading_error().unwrap_or("internal error")
     } else {
         match d.kind {
             hir::MacroKind::Attr if proc_macros_enabled => "attribute macro expansion is disabled",
diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs
index 2a0478d62f5..dcf6089ca7b 100644
--- a/crates/rust-analyzer/src/reload.rs
+++ b/crates/rust-analyzer/src/reload.rs
@@ -528,10 +528,16 @@ impl GlobalState {
             (crate_graph, proc_macros, crate_graph_file_dependencies)
         };
 
+        let mut change = Change::new();
         if self.config.expand_proc_macros() {
+            change.set_proc_macros(
+                crate_graph
+                    .iter()
+                    .map(|id| (id, Err("Proc-macros have not been built yet".to_owned())))
+                    .collect(),
+            );
             self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
         }
-        let mut change = Change::new();
         change.set_crate_graph(crate_graph);
         self.analysis_host.apply_change(change);
         self.crate_graph_file_dependencies = crate_graph_file_dependencies;