Stop compilation if macro expansion failed

This commit is contained in:
Guillaume Gomez 2025-07-24 12:21:41 +02:00
parent a955f1cd09
commit 96340f6714
5 changed files with 17 additions and 4 deletions

View file

@ -1224,6 +1224,7 @@ pub struct ExtCtxt<'a> {
pub(super) expanded_inert_attrs: MarkedAttrs,
/// `-Zmacro-stats` data.
pub macro_stats: FxHashMap<(Symbol, MacroKind), MacroStat>,
pub nb_macro_errors: usize,
}
impl<'a> ExtCtxt<'a> {
@ -1254,6 +1255,7 @@ impl<'a> ExtCtxt<'a> {
expanded_inert_attrs: MarkedAttrs::new(),
buffered_early_lint: vec![],
macro_stats: Default::default(),
nb_macro_errors: 0,
}
}
@ -1315,6 +1317,12 @@ impl<'a> ExtCtxt<'a> {
self.current_expansion.id.expansion_cause()
}
/// This method increases the internal macro errors count and then call `trace_macros_diag`.
pub fn macro_error_and_trace_macros_diag(&mut self) {
self.nb_macro_errors += 1;
self.trace_macros_diag();
}
pub fn trace_macros_diag(&mut self) {
for (span, notes) in self.expansions.iter() {
let mut db = self.dcx().create_note(errors::TraceMacro { span: *span });

View file

@ -693,7 +693,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
crate_name: self.cx.ecfg.crate_name,
});
self.cx.trace_macros_diag();
self.cx.macro_error_and_trace_macros_diag();
guar
}
@ -707,7 +707,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
) -> ErrorGuaranteed {
let guar =
self.cx.dcx().emit_err(WrongFragmentKind { span, kind: kind.name(), name: &mac.path });
self.cx.trace_macros_diag();
self.cx.macro_error_and_trace_macros_diag();
guar
}
@ -1048,7 +1048,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
annotate_err_with_kind(&mut err, kind, span);
let guar = err.emit();
self.cx.trace_macros_diag();
self.cx.macro_error_and_trace_macros_diag();
kind.dummy(span, guar)
}
}

View file

@ -299,6 +299,7 @@ enum EofMatcherPositions {
}
/// Represents the possible results of an attempted parse.
#[derive(Debug)]
pub(crate) enum ParseResult<T, F> {
/// Parsed successfully.
Success(T),

View file

@ -280,7 +280,7 @@ fn expand_macro<'cx>(
// Retry and emit a better error.
let (span, guar) =
diagnostics::failed_to_match_macro(cx.psess(), sp, def_span, name, arg, rules);
cx.trace_macros_diag();
cx.macro_error_and_trace_macros_diag();
DummyResult::any(span, guar)
}
}

View file

@ -208,6 +208,10 @@ fn configure_and_expand(
// Expand macros now!
let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
if ecx.nb_macro_errors > 0 {
sess.dcx().abort_if_errors();
}
// The rest is error reporting and stats
sess.psess.buffered_lints.with_lock(|buffered_lints: &mut Vec<BufferedEarlyLint>| {