about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/crates/ide-diagnostics
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-07-29 14:07:33 +0000
committerbors <bors@rust-lang.org>2024-07-29 14:07:33 +0000
commit722f79d374cdf97fdeaf14aaa3e8dde10db07140 (patch)
treec178adf20881030ce0a8cebaad2bf0903ebda643 /src/tools/rust-analyzer/crates/ide-diagnostics
parent51130d8222c3228a9576e3e114a0016510a9da0f (diff)
parent5ac8b79ca8087fa86185ba2c472ee5e0ceae6e5c (diff)
downloadrust-722f79d374cdf97fdeaf14aaa3e8dde10db07140.tar.gz
rust-722f79d374cdf97fdeaf14aaa3e8dde10db07140.zip
Auto merge of #17707 - Veykril:proc-macro-err-cleanup, r=Veykril
feat: Use spans for builtin and declarative macro expansion errors

This should generally improve some error reporting for macro expansion errors. Especially for `compile_error!` within proc-macros
Diffstat (limited to 'src/tools/rust-analyzer/crates/ide-diagnostics')
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs24
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs45
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs2
3 files changed, 16 insertions, 55 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs
index 2cd6a71c001..e59b63f288d 100644
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs
+++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs
@@ -7,7 +7,10 @@ pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) ->
     // Use more accurate position if available.
     let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
     Diagnostic::new(
-        DiagnosticCode::Ra("macro-error", Severity::Error),
+        DiagnosticCode::Ra(
+            "macro-error",
+            if d.error { Severity::Error } else { Severity::WeakWarning },
+        ),
         d.message.clone(),
         display_range,
     )
@@ -45,7 +48,7 @@ macro_rules! include { () => {} }
 macro_rules! compile_error { () => {} }
 
   include!("doesntexist");
-//^^^^^^^ error: failed to load file `doesntexist`
+         //^^^^^^^^^^^^^ error: failed to load file `doesntexist`
 
   compile_error!("compile_error macro works");
 //^^^^^^^^^^^^^ error: compile_error macro works
@@ -125,7 +128,7 @@ macro_rules! env { () => {} }
 macro_rules! concat { () => {} }
 
   include!(concat!(env!("OUT_DIR"), "/out.rs"));
-//^^^^^^^ error: `OUT_DIR` not set, enable "build scripts" to fix
+                      //^^^^^^^^^ error: `OUT_DIR` not set, enable "build scripts" to fix
 "#,
         );
     }
@@ -160,20 +163,25 @@ macro_rules! include {}
 
 #[rustc_builtin_macro]
 macro_rules! compile_error {}
+#[rustc_builtin_macro]
+macro_rules! concat {}
 
 fn main() {
     // Test a handful of built-in (eager) macros:
 
     include!(invalid);
-  //^^^^^^^ error: could not convert tokens
+           //^^^^^^^ error: expected string literal
     include!("does not exist");
-  //^^^^^^^ error: failed to load file `does not exist`
+           //^^^^^^^^^^^^^^^^ error: failed to load file `does not exist`
+
+    include!(concat!("does ", "not ", "exist"));
+                  //^^^^^^^^^^^^^^^^^^^^^^^^^^ error: failed to load file `does not exist`
 
     env!(invalid);
-  //^^^ error: could not convert tokens
+       //^^^^^^^ error: expected string literal
 
     env!("OUT_DIR");
-  //^^^ error: `OUT_DIR` not set, enable "build scripts" to fix
+       //^^^^^^^^^ error: `OUT_DIR` not set, enable "build scripts" to fix
 
     compile_error!("compile_error works");
   //^^^^^^^^^^^^^ error: compile_error works
@@ -198,7 +206,7 @@ fn f() {
     m!();
 
     m!(hi);
-  //^ error: leftover tokens
+    //^ error: leftover tokens
 }
       "#,
         );
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
deleted file mode 100644
index 7ea50c496fb..00000000000
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
+++ /dev/null
@@ -1,45 +0,0 @@
-use hir::db::DefDatabase;
-
-use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
-
-// Diagnostic: unresolved-proc-macro
-//
-// This diagnostic is shown when a procedural macro can not be found. This usually means that
-// procedural macro support is simply disabled (and hence is only a weak hint instead of an error),
-// but can also indicate project setup problems.
-//
-// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
-// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
-// enable support for procedural macros (see `rust-analyzer.procMacro.attributes.enable`).
-pub(crate) fn unresolved_proc_macro(
-    ctx: &DiagnosticsContext<'_>,
-    d: &hir::UnresolvedProcMacro,
-    proc_macros_enabled: bool,
-    proc_attr_macros_enabled: bool,
-) -> Diagnostic {
-    // Use more accurate position if available.
-    let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
-
-    let config_enabled = match d.kind {
-        hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
-        _ => proc_macros_enabled,
-    };
-
-    let not_expanded_message = match &d.macro_name {
-        Some(name) => format!("proc macro `{name}` not expanded"),
-        None => "proc macro not expanded".to_owned(),
-    };
-    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("internal error")
-    } else {
-        match d.kind {
-            hir::MacroKind::Attr if proc_macros_enabled => "attribute macro expansion is disabled",
-            _ => "proc-macro expansion is disabled",
-        }
-    };
-    let message = format!("{not_expanded_message}: {message}");
-
-    Diagnostic::new(DiagnosticCode::Ra("unresolved-proc-macro", severity), message, display_range)
-}
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs
index 9523cc81b8f..263ab747559 100644
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs
@@ -62,7 +62,6 @@ mod handlers {
     pub(crate) mod unresolved_macro_call;
     pub(crate) mod unresolved_method;
     pub(crate) mod unresolved_module;
-    pub(crate) mod unresolved_proc_macro;
     pub(crate) mod unused_variables;
 
     // The handlers below are unusual, the implement the diagnostics as well.
@@ -405,7 +404,6 @@ pub fn diagnostics(
             AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
             AnyDiagnostic::UnresolvedMethodCall(d) => handlers::unresolved_method::unresolved_method(&ctx, &d),
             AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
-            AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled),
             AnyDiagnostic::UnusedMut(d) => match handlers::mutability_errors::unused_mut(&ctx, &d) {
                 Some(it) => it,
                 None => continue,