Merge pull request #18418 from ChayimFriedman2/explicitly-disable

feat: Split `macro-error` diagnostic so users can ignore only parts of it
This commit is contained in:
Lukas Wirth 2024-10-27 09:37:56 +00:00 committed by GitHub
commit 79b86f25b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 77 additions and 34 deletions

View file

@ -122,7 +122,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
let mut expn_text = String::new(); let mut expn_text = String::new();
if let Some(err) = exp.err { if let Some(err) = exp.err {
format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).0); format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).message);
} }
let (parse, token_map) = exp.value; let (parse, token_map) = exp.value;
if expect_errors { if expect_errors {

View file

@ -165,40 +165,73 @@ pub enum ExpandErrorKind {
} }
impl ExpandError { impl ExpandError {
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> (String, bool) { pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
self.inner.0.render_to_string(db) self.inner.0.render_to_string(db)
} }
} }
pub struct RenderedExpandError {
pub message: String,
pub error: bool,
pub kind: &'static str,
}
impl RenderedExpandError {
const GENERAL_KIND: &str = "macro-error";
}
impl ExpandErrorKind { impl ExpandErrorKind {
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> (String, bool) { pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
match self { match self {
ExpandErrorKind::ProcMacroAttrExpansionDisabled => { ExpandErrorKind::ProcMacroAttrExpansionDisabled => RenderedExpandError {
("procedural attribute macro expansion is disabled".to_owned(), false) message: "procedural attribute macro expansion is disabled".to_owned(),
} error: false,
ExpandErrorKind::MacroDisabled => { kind: "proc-macros-disabled",
("proc-macro is explicitly disabled".to_owned(), false) },
} ExpandErrorKind::MacroDisabled => RenderedExpandError {
message: "proc-macro is explicitly disabled".to_owned(),
error: false,
kind: "proc-macro-disabled",
},
&ExpandErrorKind::MissingProcMacroExpander(def_crate) => { &ExpandErrorKind::MissingProcMacroExpander(def_crate) => {
match db.proc_macros().get_error_for_crate(def_crate) { match db.proc_macros().get_error_for_crate(def_crate) {
Some((e, hard_err)) => (e.to_owned(), hard_err), Some((e, hard_err)) => RenderedExpandError {
None => ( message: e.to_owned(),
format!( error: hard_err,
"internal error: proc-macro map is missing error entry for crate {def_crate:?}" kind: RenderedExpandError::GENERAL_KIND,
), },
true, None => RenderedExpandError {
), message: format!("internal error: proc-macro map is missing error entry for crate {def_crate:?}"),
error: true,
kind: RenderedExpandError::GENERAL_KIND,
},
} }
} }
ExpandErrorKind::MacroDefinition => { ExpandErrorKind::MacroDefinition => RenderedExpandError {
("macro definition has parse errors".to_owned(), true) message: "macro definition has parse errors".to_owned(),
} error: true,
ExpandErrorKind::Mbe(e) => (e.to_string(), true), kind: RenderedExpandError::GENERAL_KIND,
ExpandErrorKind::RecursionOverflow => { },
("overflow expanding the original macro".to_owned(), true) ExpandErrorKind::Mbe(e) => RenderedExpandError {
} message: e.to_string(),
ExpandErrorKind::Other(e) => ((**e).to_owned(), true), error: true,
ExpandErrorKind::ProcMacroPanic(e) => (format!("proc-macro panicked: {e}"), true), kind: RenderedExpandError::GENERAL_KIND,
},
ExpandErrorKind::RecursionOverflow => RenderedExpandError {
message: "overflow expanding the original macro".to_owned(),
error: true,
kind: RenderedExpandError::GENERAL_KIND,
},
ExpandErrorKind::Other(e) => RenderedExpandError {
message: (**e).to_owned(),
error: true,
kind: RenderedExpandError::GENERAL_KIND,
},
ExpandErrorKind::ProcMacroPanic(e) => RenderedExpandError {
message: format!("proc-macro panicked: {e}"),
error: true,
kind: RenderedExpandError::GENERAL_KIND,
},
} }
} }
} }

View file

@ -165,6 +165,7 @@ pub struct MacroError {
pub precise_location: Option<TextRange>, pub precise_location: Option<TextRange>,
pub message: String, pub message: String,
pub error: bool, pub error: bool,
pub kind: &'static str,
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]

View file

@ -58,7 +58,8 @@ use hir_def::{
TypeOrConstParamId, TypeParamId, UnionId, TypeOrConstParamId, TypeParamId, UnionId,
}; };
use hir_expand::{ use hir_expand::{
attrs::collect_attrs, proc_macro::ProcMacroKind, AstId, MacroCallKind, ValueResult, attrs::collect_attrs, proc_macro::ProcMacroKind, AstId, MacroCallKind, RenderedExpandError,
ValueResult,
}; };
use hir_ty::{ use hir_ty::{
all_super_traits, autoderef, check_orphan_rules, all_super_traits, autoderef, check_orphan_rules,
@ -838,7 +839,7 @@ fn macro_call_diagnostics(
let file_id = loc.kind.file_id(); let file_id = loc.kind.file_id();
let node = let node =
InFile::new(file_id, db.ast_id_map(file_id).get_erased(loc.kind.erased_ast_id())); InFile::new(file_id, db.ast_id_map(file_id).get_erased(loc.kind.erased_ast_id()));
let (message, error) = err.render_to_string(db.upcast()); let RenderedExpandError { message, error, kind } = err.render_to_string(db.upcast());
let precise_location = if err.span().anchor.file_id == file_id { let precise_location = if err.span().anchor.file_id == file_id {
Some( Some(
err.span().range err.span().range
@ -850,7 +851,7 @@ fn macro_call_diagnostics(
} else { } else {
None None
}; };
acc.push(MacroError { node, precise_location, message, error }.into()); acc.push(MacroError { node, precise_location, message, error, kind }.into());
} }
if !parse_errors.is_empty() { if !parse_errors.is_empty() {
@ -916,13 +917,14 @@ fn emit_def_diagnostic_(
DefDiagnosticKind::MacroError { ast, path, err } => { DefDiagnosticKind::MacroError { ast, path, err } => {
let item = ast.to_ptr(db.upcast()); let item = ast.to_ptr(db.upcast());
let (message, error) = err.render_to_string(db.upcast()); let RenderedExpandError { message, error, kind } = err.render_to_string(db.upcast());
acc.push( acc.push(
MacroError { MacroError {
node: InFile::new(ast.file_id, item.syntax_node_ptr()), node: InFile::new(ast.file_id, item.syntax_node_ptr()),
precise_location: None, precise_location: None,
message: format!("{}: {message}", path.display(db.upcast(), edition)), message: format!("{}: {message}", path.display(db.upcast(), edition)),
error, error,
kind,
} }
.into(), .into(),
) )
@ -1811,7 +1813,8 @@ impl DefWithBody {
InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into() InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
} }
BodyDiagnostic::MacroError { node, err } => { BodyDiagnostic::MacroError { node, err } => {
let (message, error) = err.render_to_string(db.upcast()); let RenderedExpandError { message, error, kind } =
err.render_to_string(db.upcast());
let precise_location = if err.span().anchor.file_id == node.file_id { let precise_location = if err.span().anchor.file_id == node.file_id {
Some( Some(
@ -1829,6 +1832,7 @@ impl DefWithBody {
precise_location, precise_location,
message, message,
error, error,
kind,
} }
.into() .into()
} }

View file

@ -3,14 +3,19 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
// Diagnostic: macro-error // Diagnostic: macro-error
// //
// This diagnostic is shown for macro expansion errors. // This diagnostic is shown for macro expansion errors.
// Diagnostic: proc-macros-disabled
//
// This diagnostic is shown for proc macros where proc macros have been disabled.
// Diagnostic: proc-macro-disabled
//
// This diagnostic is shown for proc macros that has been specifically disabled via `rust-analyzer.procMacro.ignored`.
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic { pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
// Use more accurate position if available. // Use more accurate position if available.
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location); let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
Diagnostic::new( Diagnostic::new(
DiagnosticCode::Ra( DiagnosticCode::Ra(d.kind, if d.error { Severity::Error } else { Severity::WeakWarning }),
"macro-error",
if d.error { Severity::Error } else { Severity::WeakWarning },
),
d.message.clone(), d.message.clone(),
display_range, display_range,
) )