diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-03-17 10:56:00 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-03-24 06:28:55 +0100 |
| commit | ce8880d1d8b10cfaebc44449a37b6ad9fc628cb9 (patch) | |
| tree | 14cb991da08cd155816aa4a2b274c2365133bbb1 | |
| parent | 35cca7421264371dd8962bf52d0fa5cc479e1279 (diff) | |
| download | rust-ce8880d1d8b10cfaebc44449a37b6ad9fc628cb9.tar.gz rust-ce8880d1d8b10cfaebc44449a37b6ad9fc628cb9.zip | |
defatalize AttrProcMacro::expand
| -rw-r--r-- | src/librustc_expand/base.rs | 6 | ||||
| -rw-r--r-- | src/librustc_expand/expand.rs | 7 | ||||
| -rw-r--r-- | src/librustc_expand/proc_macro.rs | 21 |
3 files changed, 16 insertions, 18 deletions
diff --git a/src/librustc_expand/base.rs b/src/librustc_expand/base.rs index 20e1895270a..4947730a4aa 100644 --- a/src/librustc_expand/base.rs +++ b/src/librustc_expand/base.rs @@ -326,7 +326,7 @@ pub trait AttrProcMacro { span: Span, annotation: TokenStream, annotated: TokenStream, - ) -> TokenStream; + ) -> Result<TokenStream, ErrorReported>; } impl<F> AttrProcMacro for F @@ -339,9 +339,9 @@ where _span: Span, annotation: TokenStream, annotated: TokenStream, - ) -> TokenStream { + ) -> Result<TokenStream, ErrorReported> { // FIXME setup implicit context in TLS before calling self. - (*self)(annotation, annotated) + Ok((*self)(annotation, annotated)) } } diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs index 57314824d5a..cbb695a815a 100644 --- a/src/librustc_expand/expand.rs +++ b/src/librustc_expand/expand.rs @@ -712,8 +712,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if let MacArgs::Eq(..) = attr_item.args { self.cx.span_err(span, "key-value macro attributes are not supported"); } - let tok_result = - expander.expand(self.cx, span, attr_item.args.inner_tokens(), tokens); + let inner_tokens = attr_item.args.inner_tokens(); + let tok_result = match expander.expand(self.cx, span, inner_tokens, tokens) { + Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)), + Ok(ts) => ts, + }; self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span) } SyntaxExtensionKind::LegacyAttr(expander) => { diff --git a/src/librustc_expand/proc_macro.rs b/src/librustc_expand/proc_macro.rs index 404f29f1fcf..40b5e63756b 100644 --- a/src/librustc_expand/proc_macro.rs +++ b/src/librustc_expand/proc_macro.rs @@ -45,21 +45,16 @@ impl base::AttrProcMacro for AttrProcMacro { span: Span, annotation: TokenStream, annotated: TokenStream, - ) -> TokenStream { + ) -> Result<TokenStream, ErrorReported> { let server = proc_macro_server::Rustc::new(ecx); - match self.client.run(&EXEC_STRATEGY, server, annotation, annotated) { - Ok(stream) => stream, - Err(e) => { - let msg = "custom attribute panicked"; - let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.as_str() { - err.help(&format!("message: {}", s)); - } - - err.emit(); - FatalError.raise(); + self.client.run(&EXEC_STRATEGY, server, annotation, annotated).map_err(|e| { + let mut err = ecx.struct_span_err(span, "custom attribute panicked"); + if let Some(s) = e.as_str() { + err.help(&format!("message: {}", s)); } - } + err.emit(); + ErrorReported + }) } } |
