Skip to content

Commit ad5304a

Browse files
committed
defatalize compile_declarative_macro
1 parent 8dddaed commit ad5304a

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_parse::Directory;
@@ -181,6 +181,19 @@ impl TTMacroExpander for MacroRulesMacroExpander {
181181
}
182182
}
183183

184+
struct MacroRulesDummyExpander;
185+
186+
impl TTMacroExpander for MacroRulesDummyExpander {
187+
fn expand<'cx>(
188+
&self,
189+
_: &'cx mut ExtCtxt<'_>,
190+
sp: Span,
191+
_: TokenStream,
192+
) -> Box<dyn MacResult + 'cx> {
193+
DummyResult::any(sp)
194+
}
195+
}
196+
184197
fn trace_macros_note(cx_expansions: &mut FxHashMap<Span, Vec<String>>, sp: Span, message: String) {
185198
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
186199
cx_expansions.entry(sp).or_default().push(message);
@@ -369,6 +382,18 @@ pub fn compile_declarative_macro(
369382
def: &ast::Item,
370383
edition: Edition,
371384
) -> SyntaxExtension {
385+
let mk_syn_ext = |expander| {
386+
SyntaxExtension::new(
387+
sess,
388+
SyntaxExtensionKind::LegacyBang(expander),
389+
def.span,
390+
Vec::new(),
391+
edition,
392+
def.ident.name,
393+
&def.attrs,
394+
)
395+
};
396+
372397
let diag = &sess.span_diagnostic;
373398
let lhs_nm = ast::Ident::new(sym::lhs, def.span);
374399
let rhs_nm = ast::Ident::new(sym::rhs, def.span);
@@ -423,13 +448,12 @@ pub fn compile_declarative_macro(
423448
Failure(token, msg) => {
424449
let s = parse_failure_msg(&token);
425450
let sp = token.span.substitute_dummy(def.span);
426-
let mut err = sess.span_diagnostic.struct_span_fatal(sp, &s);
427-
err.span_label(sp, msg);
428-
err.emit();
429-
FatalError.raise();
451+
sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
452+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
430453
}
431-
Error(sp, s) => {
432-
sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
454+
Error(sp, msg) => {
455+
sess.span_diagnostic.struct_span_err(sp.substitute_dummy(def.span), &msg).emit();
456+
return mk_syn_ext(Box::new(MacroRulesDummyExpander));
433457
}
434458
};
435459

@@ -501,15 +525,7 @@ pub fn compile_declarative_macro(
501525
valid,
502526
});
503527

504-
SyntaxExtension::new(
505-
sess,
506-
SyntaxExtensionKind::LegacyBang(expander),
507-
def.span,
508-
Vec::new(),
509-
edition,
510-
def.ident.name,
511-
&def.attrs,
512-
)
528+
mk_syn_ext(expander)
513529
}
514530

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