Skip to content

Commit 66a81d3

Browse files
authored
Merge pull request rust-lang#18418 from ChayimFriedman2/explicitly-disable
feat: Split `macro-error` diagnostic so users can ignore only parts of it
2 parents 59dd642 + 071bd3c commit 66a81d3

File tree

5 files changed

+77
-34
lines changed

5 files changed

+77
-34
lines changed

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
122122

123123
let mut expn_text = String::new();
124124
if let Some(err) = exp.err {
125-
format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).0);
125+
format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).message);
126126
}
127127
let (parse, token_map) = exp.value;
128128
if expect_errors {

src/tools/rust-analyzer/crates/hir-expand/src/lib.rs

+57-24
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,73 @@ pub enum ExpandErrorKind {
165165
}
166166

167167
impl ExpandError {
168-
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> (String, bool) {
168+
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
169169
self.inner.0.render_to_string(db)
170170
}
171171
}
172172

173+
pub struct RenderedExpandError {
174+
pub message: String,
175+
pub error: bool,
176+
pub kind: &'static str,
177+
}
178+
179+
impl RenderedExpandError {
180+
const GENERAL_KIND: &str = "macro-error";
181+
}
182+
173183
impl ExpandErrorKind {
174-
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> (String, bool) {
184+
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
175185
match self {
176-
ExpandErrorKind::ProcMacroAttrExpansionDisabled => {
177-
("procedural attribute macro expansion is disabled".to_owned(), false)
178-
}
179-
ExpandErrorKind::MacroDisabled => {
180-
("proc-macro is explicitly disabled".to_owned(), false)
181-
}
186+
ExpandErrorKind::ProcMacroAttrExpansionDisabled => RenderedExpandError {
187+
message: "procedural attribute macro expansion is disabled".to_owned(),
188+
error: false,
189+
kind: "proc-macros-disabled",
190+
},
191+
ExpandErrorKind::MacroDisabled => RenderedExpandError {
192+
message: "proc-macro is explicitly disabled".to_owned(),
193+
error: false,
194+
kind: "proc-macro-disabled",
195+
},
182196
&ExpandErrorKind::MissingProcMacroExpander(def_crate) => {
183197
match db.proc_macros().get_error_for_crate(def_crate) {
184-
Some((e, hard_err)) => (e.to_owned(), hard_err),
185-
None => (
186-
format!(
187-
"internal error: proc-macro map is missing error entry for crate {def_crate:?}"
188-
),
189-
true,
190-
),
198+
Some((e, hard_err)) => RenderedExpandError {
199+
message: e.to_owned(),
200+
error: hard_err,
201+
kind: RenderedExpandError::GENERAL_KIND,
202+
},
203+
None => RenderedExpandError {
204+
message: format!("internal error: proc-macro map is missing error entry for crate {def_crate:?}"),
205+
error: true,
206+
kind: RenderedExpandError::GENERAL_KIND,
207+
},
191208
}
192209
}
193-
ExpandErrorKind::MacroDefinition => {
194-
("macro definition has parse errors".to_owned(), true)
195-
}
196-
ExpandErrorKind::Mbe(e) => (e.to_string(), true),
197-
ExpandErrorKind::RecursionOverflow => {
198-
("overflow expanding the original macro".to_owned(), true)
199-
}
200-
ExpandErrorKind::Other(e) => ((**e).to_owned(), true),
201-
ExpandErrorKind::ProcMacroPanic(e) => (format!("proc-macro panicked: {e}"), true),
210+
ExpandErrorKind::MacroDefinition => RenderedExpandError {
211+
message: "macro definition has parse errors".to_owned(),
212+
error: true,
213+
kind: RenderedExpandError::GENERAL_KIND,
214+
},
215+
ExpandErrorKind::Mbe(e) => RenderedExpandError {
216+
message: e.to_string(),
217+
error: true,
218+
kind: RenderedExpandError::GENERAL_KIND,
219+
},
220+
ExpandErrorKind::RecursionOverflow => RenderedExpandError {
221+
message: "overflow expanding the original macro".to_owned(),
222+
error: true,
223+
kind: RenderedExpandError::GENERAL_KIND,
224+
},
225+
ExpandErrorKind::Other(e) => RenderedExpandError {
226+
message: (**e).to_owned(),
227+
error: true,
228+
kind: RenderedExpandError::GENERAL_KIND,
229+
},
230+
ExpandErrorKind::ProcMacroPanic(e) => RenderedExpandError {
231+
message: format!("proc-macro panicked: {e}"),
232+
error: true,
233+
kind: RenderedExpandError::GENERAL_KIND,
234+
},
202235
}
203236
}
204237
}

src/tools/rust-analyzer/crates/hir/src/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pub struct MacroError {
165165
pub precise_location: Option<TextRange>,
166166
pub message: String,
167167
pub error: bool,
168+
pub kind: &'static str,
168169
}
169170

170171
#[derive(Debug, Clone, Eq, PartialEq)]

src/tools/rust-analyzer/crates/hir/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ use hir_def::{
5858
TypeOrConstParamId, TypeParamId, UnionId,
5959
};
6060
use hir_expand::{
61-
attrs::collect_attrs, proc_macro::ProcMacroKind, AstId, MacroCallKind, ValueResult,
61+
attrs::collect_attrs, proc_macro::ProcMacroKind, AstId, MacroCallKind, RenderedExpandError,
62+
ValueResult,
6263
};
6364
use hir_ty::{
6465
all_super_traits, autoderef, check_orphan_rules,
@@ -838,7 +839,7 @@ fn macro_call_diagnostics(
838839
let file_id = loc.kind.file_id();
839840
let node =
840841
InFile::new(file_id, db.ast_id_map(file_id).get_erased(loc.kind.erased_ast_id()));
841-
let (message, error) = err.render_to_string(db.upcast());
842+
let RenderedExpandError { message, error, kind } = err.render_to_string(db.upcast());
842843
let precise_location = if err.span().anchor.file_id == file_id {
843844
Some(
844845
err.span().range
@@ -850,7 +851,7 @@ fn macro_call_diagnostics(
850851
} else {
851852
None
852853
};
853-
acc.push(MacroError { node, precise_location, message, error }.into());
854+
acc.push(MacroError { node, precise_location, message, error, kind }.into());
854855
}
855856

856857
if !parse_errors.is_empty() {
@@ -916,13 +917,14 @@ fn emit_def_diagnostic_(
916917

917918
DefDiagnosticKind::MacroError { ast, path, err } => {
918919
let item = ast.to_ptr(db.upcast());
919-
let (message, error) = err.render_to_string(db.upcast());
920+
let RenderedExpandError { message, error, kind } = err.render_to_string(db.upcast());
920921
acc.push(
921922
MacroError {
922923
node: InFile::new(ast.file_id, item.syntax_node_ptr()),
923924
precise_location: None,
924925
message: format!("{}: {message}", path.display(db.upcast(), edition)),
925926
error,
927+
kind,
926928
}
927929
.into(),
928930
)
@@ -1811,7 +1813,8 @@ impl DefWithBody {
18111813
InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
18121814
}
18131815
BodyDiagnostic::MacroError { node, err } => {
1814-
let (message, error) = err.render_to_string(db.upcast());
1816+
let RenderedExpandError { message, error, kind } =
1817+
err.render_to_string(db.upcast());
18151818

18161819
let precise_location = if err.span().anchor.file_id == node.file_id {
18171820
Some(
@@ -1829,6 +1832,7 @@ impl DefWithBody {
18291832
precise_location,
18301833
message,
18311834
error,
1835+
kind,
18321836
}
18331837
.into()
18341838
}

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/macro_error.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
33
// Diagnostic: macro-error
44
//
55
// This diagnostic is shown for macro expansion errors.
6+
7+
// Diagnostic: proc-macros-disabled
8+
//
9+
// This diagnostic is shown for proc macros where proc macros have been disabled.
10+
11+
// Diagnostic: proc-macro-disabled
12+
//
13+
// This diagnostic is shown for proc macros that has been specifically disabled via `rust-analyzer.procMacro.ignored`.
614
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
715
// Use more accurate position if available.
816
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
917
Diagnostic::new(
10-
DiagnosticCode::Ra(
11-
"macro-error",
12-
if d.error { Severity::Error } else { Severity::WeakWarning },
13-
),
18+
DiagnosticCode::Ra(d.kind, if d.error { Severity::Error } else { Severity::WeakWarning }),
1419
d.message.clone(),
1520
display_range,
1621
)

0 commit comments

Comments
 (0)