Skip to content

Commit 5fb479f

Browse files
committed
defatalize compile_declarative_macro
1 parent 60d7608 commit 5fb479f

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

src/librustc_expand/mbe/macro_rules.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_ast_pretty::pprust;
1515
use rustc_attr::{self as attr, TransparencyError};
1616
use rustc_data_structures::fx::FxHashMap;
1717
use rustc_data_structures::sync::Lrc;
18-
use rustc_errors::{Applicability, DiagnosticBuilder, FatalError};
18+
use rustc_errors::{Applicability, DiagnosticBuilder};
1919
use rustc_feature::Features;
2020
use rustc_parse::parser::Parser;
2121
use rustc_session::parse::ParseSess;
@@ -180,6 +180,19 @@ impl TTMacroExpander for MacroRulesMacroExpander {
180180
}
181181
}
182182

183+
struct MacroRulesDummyExpander;
184+
185+
impl TTMacroExpander for MacroRulesDummyExpander {
186+
fn expand<'cx>(
187+
&self,
188+
_: &'cx mut ExtCtxt<'_>,
189+
sp: Span,
190+
_: TokenStream,
191+
) -> Box<dyn MacResult + 'cx> {
192+
DummyResult::any(sp)
193+
}
194+
}
195+
183196
fn trace_macros_note(cx_expansions: &mut FxHashMap<Span, Vec<String>>, sp: Span, message: String) {
184197
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
185198
cx_expansions.entry(sp).or_default().push(message);
@@ -364,6 +377,18 @@ pub fn compile_declarative_macro(
364377
def: &ast::Item,
365378
edition: Edition,
366379
) -> SyntaxExtension {
380+
let mk_syn_ext = |expander| {
381+
SyntaxExtension::new(
382+
sess,
383+
SyntaxExtensionKind::LegacyBang(expander),
384+
def.span,
385+
Vec::new(),
386+
edition,
387+
def.ident.name,
388+
&def.attrs,
389+
)
390+
};
391+
367392
let diag = &sess.span_diagnostic;
368393
let lhs_nm = ast::Ident::new(sym::lhs, def.span);
369394
let rhs_nm = ast::Ident::new(sym::rhs, def.span);
@@ -418,13 +443,12 @@ pub fn compile_declarative_macro(
418443
Failure(token, msg) => {
419444
let s = parse_failure_msg(&token);
420445
let sp = token.span.substitute_dummy(def.span);
421-
let mut err = sess.span_diagnostic.struct_span_fatal(sp, &s);
422-
err.span_label(sp, msg);
423-
err.emit();
424-
FatalError.raise();
446+
sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
447+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
425448
}
426-
Error(sp, s) => {
427-
sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
449+
Error(sp, msg) => {
450+
sess.span_diagnostic.struct_span_err(sp.substitute_dummy(def.span), &msg).emit();
451+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
428452
}
429453
};
430454

@@ -496,15 +520,7 @@ pub fn compile_declarative_macro(
496520
valid,
497521
});
498522

499-
SyntaxExtension::new(
500-
sess,
501-
SyntaxExtensionKind::LegacyBang(expander),
502-
def.span,
503-
Vec::new(),
504-
edition,
505-
def.ident.name,
506-
&def.attrs,
507-
)
523+
mk_syn_ext(expander)
508524
}
509525

510526
fn check_lhs_nt_follows(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {}
2+
3+
macro_rules! ambiguity {
4+
($($i:ident)* $j:ident) => {};
5+
}
6+
7+
ambiguity!(error); //~ ERROR local ambiguity
8+
ambiguity!(error); //~ ERROR local ambiguity
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: local ambiguity: multiple parsing options: built-in NTs ident ('i') or ident ('j').
2+
--> $DIR/local-ambiguity-multiple-parsing-options.rs:7:12
3+
|
4+
LL | ambiguity!(error);
5+
| ^^^^^
6+
7+
error: local ambiguity: multiple parsing options: built-in NTs ident ('i') or ident ('j').
8+
--> $DIR/local-ambiguity-multiple-parsing-options.rs:8:12
9+
|
10+
LL | ambiguity!(error);
11+
| ^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)