defatalize ProcMacroDerive::expand

Also remove ExtCtxt::struct_span_fatal.
This commit is contained in:
Mazdak Farrokhzad 2020-03-17 11:30:53 +01:00
parent ce8880d1d8
commit c1ef1b3bca
6 changed files with 44 additions and 33 deletions

View File

@ -1020,9 +1020,6 @@ pub fn struct_span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> Diagnost
pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'a> { pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'a> {
self.parse_sess.span_diagnostic.struct_span_err(sp, msg) self.parse_sess.span_diagnostic.struct_span_err(sp, msg)
} }
pub fn struct_span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> DiagnosticBuilder<'a> {
self.parse_sess.span_diagnostic.struct_span_fatal(sp, msg)
}
/// Emit `msg` attached to `sp`, and stop compilation immediately. /// Emit `msg` attached to `sp`, and stop compilation immediately.
/// ///

View File

@ -5,7 +5,7 @@
use rustc_ast::token; use rustc_ast::token;
use rustc_ast::tokenstream::{self, TokenStream}; use rustc_ast::tokenstream::{self, TokenStream};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, ErrorReported, FatalError}; use rustc_errors::{Applicability, ErrorReported};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
@ -86,8 +86,7 @@ fn expand(
| Annotatable::Expr(_) => { | Annotatable::Expr(_) => {
ecx.span_err( ecx.span_err(
span, span,
"proc-macro derives may only be \ "proc-macro derives may only be applied to a struct, enum, or union",
applied to a struct, enum, or union",
); );
return ExpandResult::Ready(Vec::new()); return ExpandResult::Ready(Vec::new());
} }
@ -97,8 +96,7 @@ fn expand(
_ => { _ => {
ecx.span_err( ecx.span_err(
span, span,
"proc-macro derives may only be \ "proc-macro derives may only be applied to a struct, enum, or union",
applied to a struct, enum, or union",
); );
return ExpandResult::Ready(Vec::new()); return ExpandResult::Ready(Vec::new());
} }
@ -111,20 +109,16 @@ fn expand(
let stream = match self.client.run(&EXEC_STRATEGY, server, input) { let stream = match self.client.run(&EXEC_STRATEGY, server, input) {
Ok(stream) => stream, Ok(stream) => stream,
Err(e) => { Err(e) => {
let msg = "proc-macro derive panicked"; let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
let mut err = ecx.struct_span_fatal(span, msg);
if let Some(s) = e.as_str() { if let Some(s) = e.as_str() {
err.help(&format!("message: {}", s)); err.help(&format!("message: {}", s));
} }
err.emit(); err.emit();
FatalError.raise(); return ExpandResult::Ready(vec![]);
} }
}; };
let error_count_before = ecx.parse_sess.span_diagnostic.err_count(); let error_count_before = ecx.parse_sess.span_diagnostic.err_count();
let msg = "proc-macro derive produced unparseable tokens";
let mut parser = let mut parser =
rustc_parse::stream_to_parser(ecx.parse_sess, stream, Some("proc-macro derive")); rustc_parse::stream_to_parser(ecx.parse_sess, stream, Some("proc-macro derive"));
let mut items = vec![]; let mut items = vec![];
@ -134,18 +128,15 @@ fn expand(
Ok(None) => break, Ok(None) => break,
Ok(Some(item)) => items.push(Annotatable::Item(item)), Ok(Some(item)) => items.push(Annotatable::Item(item)),
Err(mut err) => { Err(mut err) => {
// FIXME: handle this better err.emit();
err.cancel(); break;
ecx.struct_span_fatal(span, msg).emit();
FatalError.raise();
} }
} }
} }
// fail if there have been errors emitted // fail if there have been errors emitted
if ecx.parse_sess.span_diagnostic.err_count() > error_count_before { if ecx.parse_sess.span_diagnostic.err_count() > error_count_before {
ecx.struct_span_fatal(span, msg).emit(); ecx.struct_span_err(span, "proc-macro derive produced unparseable tokens").emit();
FatalError.raise();
} }
ExpandResult::Ready(items) ExpandResult::Ready(items)

View File

@ -3,11 +3,9 @@
#[macro_use] #[macro_use]
extern crate derive_bad; extern crate derive_bad;
#[derive( #[derive(A)]
A //~^ ERROR proc-macro derive produced unparseable tokens
)]
//~^^ ERROR proc-macro derive produced unparseable tokens
//~| ERROR expected `:`, found `}` //~| ERROR expected `:`, found `}`
struct A; struct A; //~ ERROR the name `A` is defined multiple times
fn main() {} fn main() {}

View File

@ -1,16 +1,28 @@
error: expected `:`, found `}` error: expected `:`, found `}`
--> $DIR/derive-bad.rs:7:5 --> $DIR/derive-bad.rs:6:10
| |
LL | A LL | #[derive(A)]
| ^ expected `:` | ^ expected `:`
| |
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: proc-macro derive produced unparseable tokens error: proc-macro derive produced unparseable tokens
--> $DIR/derive-bad.rs:7:5 --> $DIR/derive-bad.rs:6:10
| |
LL | A LL | #[derive(A)]
| ^ | ^
error: aborting due to 2 previous errors error[E0428]: the name `A` is defined multiple times
--> $DIR/derive-bad.rs:9:1
|
LL | #[derive(A)]
| - previous definition of the type `A` here
...
LL | struct A;
| ^^^^^^^^^ `A` redefined here
|
= note: `A` must be defined only once in the type namespace of this module
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0428`.

View File

@ -5,6 +5,7 @@
#[derive(Identity, Panic)] //~ ERROR proc-macro derive panicked #[derive(Identity, Panic)] //~ ERROR proc-macro derive panicked
struct Baz { struct Baz {
//~^ ERROR the name `Baz` is defined multiple times
a: i32, a: i32,
b: i32, b: i32,
} }

View File

@ -6,5 +6,17 @@ LL | #[derive(Identity, Panic)]
| |
= help: message: panic-derive = help: message: panic-derive
error: aborting due to previous error error[E0428]: the name `Baz` is defined multiple times
--> $DIR/issue-36935.rs:7:1
|
LL | struct Baz {
| ^^^^^^^^^^
| |
| `Baz` redefined here
| previous definition of the type `Baz` here
|
= note: `Baz` must be defined only once in the type namespace of this module
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0428`.