Skip to content

Commit 1bfa248

Browse files
committed
defatalize compile_declarative_macro
1 parent df9cec2 commit 1bfa248

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);
@@ -362,6 +375,18 @@ pub fn compile_declarative_macro(
362375
def: &ast::Item,
363376
edition: Edition,
364377
) -> SyntaxExtension {
378+
let mk_syn_ext = |expander| {
379+
SyntaxExtension::new(
380+
sess,
381+
SyntaxExtensionKind::LegacyBang(expander),
382+
def.span,
383+
Vec::new(),
384+
edition,
385+
def.ident.name,
386+
&def.attrs,
387+
)
388+
};
389+
365390
let diag = &sess.span_diagnostic;
366391
let lhs_nm = ast::Ident::new(sym::lhs, def.span);
367392
let rhs_nm = ast::Ident::new(sym::rhs, def.span);
@@ -416,13 +441,12 @@ pub fn compile_declarative_macro(
416441
Failure(token, msg) => {
417442
let s = parse_failure_msg(&token);
418443
let sp = token.span.substitute_dummy(def.span);
419-
let mut err = sess.span_diagnostic.struct_span_fatal(sp, &s);
420-
err.span_label(sp, msg);
421-
err.emit();
422-
FatalError.raise();
444+
sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
445+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
423446
}
424-
Error(sp, s) => {
425-
sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
447+
Error(sp, msg) => {
448+
sess.span_diagnostic.struct_span_err(sp.substitute_dummy(def.span), &msg).emit();
449+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
426450
}
427451
};
428452

@@ -494,15 +518,7 @@ pub fn compile_declarative_macro(
494518
valid,
495519
});
496520

497-
SyntaxExtension::new(
498-
sess,
499-
SyntaxExtensionKind::LegacyBang(expander),
500-
def.span,
501-
Vec::new(),
502-
edition,
503-
def.ident.name,
504-
&def.attrs,
505-
)
521+
mk_syn_ext(expander)
506522
}
507523

508524
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)