Skip to content

Commit a9a34e8

Browse files
Stop compilation if macro expansion failed
1 parent 20aa182 commit a9a34e8

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

compiler/rustc_expand/src/base.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@ pub struct ExtCtxt<'a> {
12271227
pub(super) expanded_inert_attrs: MarkedAttrs,
12281228
/// `-Zmacro-stats` data.
12291229
pub macro_stats: FxHashMap<(Symbol, MacroKind), MacroStat>,
1230+
pub nb_macro_errors: usize,
12301231
}
12311232

12321233
impl<'a> ExtCtxt<'a> {
@@ -1257,6 +1258,7 @@ impl<'a> ExtCtxt<'a> {
12571258
expanded_inert_attrs: MarkedAttrs::new(),
12581259
buffered_early_lint: vec![],
12591260
macro_stats: Default::default(),
1261+
nb_macro_errors: 0,
12601262
}
12611263
}
12621264

@@ -1318,6 +1320,12 @@ impl<'a> ExtCtxt<'a> {
13181320
self.current_expansion.id.expansion_cause()
13191321
}
13201322

1323+
/// This method increases the internal macro errors count and then call `trace_macros_diag`.
1324+
pub fn macro_error_and_trace_macros_diag(&mut self) {
1325+
self.nb_macro_errors += 1;
1326+
self.trace_macros_diag();
1327+
}
1328+
13211329
pub fn trace_macros_diag(&mut self) {
13221330
for (span, notes) in self.expansions.iter() {
13231331
let mut db = self.dcx().create_note(errors::TraceMacro { span: *span });

compiler/rustc_expand/src/expand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
693693
crate_name: self.cx.ecfg.crate_name,
694694
});
695695

696-
self.cx.trace_macros_diag();
696+
self.cx.macro_error_and_trace_macros_diag();
697697
guar
698698
}
699699

@@ -707,7 +707,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
707707
) -> ErrorGuaranteed {
708708
let guar =
709709
self.cx.dcx().emit_err(WrongFragmentKind { span, kind: kind.name(), name: &mac.path });
710-
self.cx.trace_macros_diag();
710+
self.cx.macro_error_and_trace_macros_diag();
711711
guar
712712
}
713713

@@ -1048,7 +1048,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10481048
}
10491049
annotate_err_with_kind(&mut err, kind, span);
10501050
let guar = err.emit();
1051-
self.cx.trace_macros_diag();
1051+
self.cx.macro_error_and_trace_macros_diag();
10521052
kind.dummy(span, guar)
10531053
}
10541054
}

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ enum EofMatcherPositions {
299299
}
300300

301301
/// Represents the possible results of an attempted parse.
302+
#[derive(Debug)]
302303
pub(crate) enum ParseResult<T, F> {
303304
/// Parsed successfully.
304305
Success(T),

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn expand_macro<'cx>(
278278
// Retry and emit a better error.
279279
let (span, guar) =
280280
diagnostics::failed_to_match_macro(cx.psess(), sp, def_span, name, arg, rules);
281-
cx.trace_macros_diag();
281+
cx.macro_error_and_trace_macros_diag();
282282
DummyResult::any(span, guar)
283283
}
284284
}

compiler/rustc_interface/src/passes.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,15 @@ fn configure_and_expand(
206206
let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
207207
ecx.num_standard_library_imports = num_standard_library_imports;
208208
// Expand macros now!
209-
let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));
209+
let (krate, nb_macro_errors) = sess.time("expand_crate", || {
210+
let mut expander = ecx.monotonic_expander();
211+
let krate = expander.expand_crate(krate);
212+
(krate, ecx.nb_macro_errors)
213+
});
214+
215+
if nb_macro_errors > 0 {
216+
sess.dcx().abort_if_errors();
217+
}
210218

211219
// The rest is error reporting and stats
212220

0 commit comments

Comments
 (0)