Skip to content

Commit ce8880d

Browse files
committed
defatalize AttrProcMacro::expand
1 parent 35cca74 commit ce8880d

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/librustc_expand/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub trait AttrProcMacro {
326326
span: Span,
327327
annotation: TokenStream,
328328
annotated: TokenStream,
329-
) -> TokenStream;
329+
) -> Result<TokenStream, ErrorReported>;
330330
}
331331

332332
impl<F> AttrProcMacro for F
@@ -339,9 +339,9 @@ where
339339
_span: Span,
340340
annotation: TokenStream,
341341
annotated: TokenStream,
342-
) -> TokenStream {
342+
) -> Result<TokenStream, ErrorReported> {
343343
// FIXME setup implicit context in TLS before calling self.
344-
(*self)(annotation, annotated)
344+
Ok((*self)(annotation, annotated))
345345
}
346346
}
347347

src/librustc_expand/expand.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
712712
if let MacArgs::Eq(..) = attr_item.args {
713713
self.cx.span_err(span, "key-value macro attributes are not supported");
714714
}
715-
let tok_result =
716-
expander.expand(self.cx, span, attr_item.args.inner_tokens(), tokens);
715+
let inner_tokens = attr_item.args.inner_tokens();
716+
let tok_result = match expander.expand(self.cx, span, inner_tokens, tokens) {
717+
Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
718+
Ok(ts) => ts,
719+
};
717720
self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
718721
}
719722
SyntaxExtensionKind::LegacyAttr(expander) => {

src/librustc_expand/proc_macro.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ impl base::AttrProcMacro for AttrProcMacro {
4545
span: Span,
4646
annotation: TokenStream,
4747
annotated: TokenStream,
48-
) -> TokenStream {
48+
) -> Result<TokenStream, ErrorReported> {
4949
let server = proc_macro_server::Rustc::new(ecx);
50-
match self.client.run(&EXEC_STRATEGY, server, annotation, annotated) {
51-
Ok(stream) => stream,
52-
Err(e) => {
53-
let msg = "custom attribute panicked";
54-
let mut err = ecx.struct_span_fatal(span, msg);
55-
if let Some(s) = e.as_str() {
56-
err.help(&format!("message: {}", s));
57-
}
58-
59-
err.emit();
60-
FatalError.raise();
50+
self.client.run(&EXEC_STRATEGY, server, annotation, annotated).map_err(|e| {
51+
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
52+
if let Some(s) = e.as_str() {
53+
err.help(&format!("message: {}", s));
6154
}
62-
}
55+
err.emit();
56+
ErrorReported
57+
})
6358
}
6459
}
6560

0 commit comments

Comments
 (0)