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();
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;
if expect_errors {

View file

@ -165,40 +165,73 @@ pub enum ExpandErrorKind {
}
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)
}
}
pub struct RenderedExpandError {
pub message: String,
pub error: bool,
pub kind: &'static str,
}
impl RenderedExpandError {
const GENERAL_KIND: &str = "macro-error";
}
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 {
ExpandErrorKind::ProcMacroAttrExpansionDisabled => {
("procedural attribute macro expansion is disabled".to_owned(), false)
}
ExpandErrorKind::MacroDisabled => {
("proc-macro is explicitly disabled".to_owned(), false)
}
ExpandErrorKind::ProcMacroAttrExpansionDisabled => RenderedExpandError {
message: "procedural attribute macro expansion is disabled".to_owned(),
error: false,
kind: "proc-macros-disabled",
},
ExpandErrorKind::MacroDisabled => RenderedExpandError {
message: "proc-macro is explicitly disabled".to_owned(),
error: false,
kind: "proc-macro-disabled",
},
&ExpandErrorKind::MissingProcMacroExpander(def_crate) => {
match db.proc_macros().get_error_for_crate(def_crate) {
Some((e, hard_err)) => (e.to_owned(), hard_err),
None => (
format!(
"internal error: proc-macro map is missing error entry for crate {def_crate:?}"
),
true,
),
Some((e, hard_err)) => RenderedExpandError {
message: e.to_owned(),
error: hard_err,
kind: RenderedExpandError::GENERAL_KIND,
},
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 => {
("macro definition has parse errors".to_owned(), true)
}
ExpandErrorKind::Mbe(e) => (e.to_string(), true),
ExpandErrorKind::RecursionOverflow => {
("overflow expanding the original macro".to_owned(), true)
}
ExpandErrorKind::Other(e) => ((**e).to_owned(), true),
ExpandErrorKind::ProcMacroPanic(e) => (format!("proc-macro panicked: {e}"), true),
ExpandErrorKind::MacroDefinition => RenderedExpandError {
message: "macro definition has parse errors".to_owned(),
error: true,
kind: RenderedExpandError::GENERAL_KIND,
},
ExpandErrorKind::Mbe(e) => RenderedExpandError {
message: e.to_string(),
error: 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 message: String,
pub error: bool,
pub kind: &'static str,
}
#[derive(Debug, Clone, Eq, PartialEq)]

View file

@ -58,7 +58,8 @@ use hir_def::{
TypeOrConstParamId, TypeParamId, UnionId,
};
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::{
all_super_traits, autoderef, check_orphan_rules,
@ -838,7 +839,7 @@ fn macro_call_diagnostics(
let file_id = loc.kind.file_id();
let node =
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 {
Some(
err.span().range
@ -850,7 +851,7 @@ fn macro_call_diagnostics(
} else {
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() {
@ -916,13 +917,14 @@ fn emit_def_diagnostic_(
DefDiagnosticKind::MacroError { ast, path, err } => {
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(
MacroError {
node: InFile::new(ast.file_id, item.syntax_node_ptr()),
precise_location: None,
message: format!("{}: {message}", path.display(db.upcast(), edition)),
error,
kind,
}
.into(),
)
@ -1811,7 +1813,8 @@ impl DefWithBody {
InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
}
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 {
Some(
@ -1829,6 +1832,7 @@ impl DefWithBody {
precise_location,
message,
error,
kind,
}
.into()
}

View file

@ -3,14 +3,19 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
// Diagnostic: macro-error
//
// 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 {
// Use more accurate position if available.
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
Diagnostic::new(
DiagnosticCode::Ra(
"macro-error",
if d.error { Severity::Error } else { Severity::WeakWarning },
),
DiagnosticCode::Ra(d.kind, if d.error { Severity::Error } else { Severity::WeakWarning }),
d.message.clone(),
display_range,
)