make panictry!
private to libsyntax
This commit completely removes usage of the `panictry!` macro from outside libsyntax. The macro causes parse errors to be fatal, so using it in libsyntax_ext caused parse failures *within* a syntax extension to be fatal, which is probably not intended. Furthermore, this commit adds spans to diagnostics emitted by empty extensions if they were missing, à la #56491.
This commit is contained in:
parent
cae164753f
commit
0a6fb84738
22 changed files with 451 additions and 134 deletions
|
@ -1,9 +1,11 @@
|
|||
use syntax::ast::*;
|
||||
use errors::DiagnosticBuilder;
|
||||
use syntax::ast::{self, *};
|
||||
use syntax::source_map::Spanned;
|
||||
use syntax::ext::base::*;
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::parse::token;
|
||||
use syntax::print::pprust;
|
||||
use syntax::ptr::P;
|
||||
use syntax::symbol::Symbol;
|
||||
use syntax::tokenstream::{TokenStream, TokenTree};
|
||||
use syntax_pos::{Span, DUMMY_SP};
|
||||
|
@ -13,33 +15,18 @@ pub fn expand_assert<'cx>(
|
|||
sp: Span,
|
||||
tts: &[TokenTree],
|
||||
) -> Box<dyn MacResult + 'cx> {
|
||||
let mut parser = cx.new_parser_from_tts(tts);
|
||||
|
||||
if parser.token == token::Eof {
|
||||
cx.struct_span_err(sp, "macro requires a boolean expression as an argument")
|
||||
.span_label(sp, "boolean expression required")
|
||||
.emit();
|
||||
return DummyResult::expr(sp);
|
||||
}
|
||||
|
||||
let cond_expr = panictry!(parser.parse_expr());
|
||||
let custom_msg_args = if parser.eat(&token::Comma) {
|
||||
let ts = parser.parse_tokens();
|
||||
if !ts.is_empty() {
|
||||
Some(ts)
|
||||
} else {
|
||||
None
|
||||
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
|
||||
Ok(assert) => assert,
|
||||
Err(mut err) => {
|
||||
err.emit();
|
||||
return DummyResult::expr(sp);
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let sp = sp.apply_mark(cx.current_expansion.mark);
|
||||
let panic_call = Mac_ {
|
||||
path: Path::from_ident(Ident::new(Symbol::intern("panic"), sp)),
|
||||
tts: if let Some(ts) = custom_msg_args {
|
||||
ts.into()
|
||||
} else {
|
||||
tts: custom_message.unwrap_or_else(|| {
|
||||
TokenStream::from(TokenTree::Token(
|
||||
DUMMY_SP,
|
||||
token::Literal(
|
||||
|
@ -49,8 +36,8 @@ pub fn expand_assert<'cx>(
|
|||
))),
|
||||
None,
|
||||
),
|
||||
)).into()
|
||||
},
|
||||
))
|
||||
}).into(),
|
||||
delim: MacDelimiter::Parenthesis,
|
||||
};
|
||||
let if_expr = cx.expr_if(
|
||||
|
@ -67,3 +54,36 @@ pub fn expand_assert<'cx>(
|
|||
);
|
||||
MacEager::expr(if_expr)
|
||||
}
|
||||
|
||||
struct Assert {
|
||||
cond_expr: P<ast::Expr>,
|
||||
custom_message: Option<TokenStream>,
|
||||
}
|
||||
|
||||
fn parse_assert<'a>(
|
||||
cx: &mut ExtCtxt<'a>,
|
||||
sp: Span,
|
||||
tts: &[TokenTree]
|
||||
) -> Result<Assert, DiagnosticBuilder<'a>> {
|
||||
let mut parser = cx.new_parser_from_tts(tts);
|
||||
|
||||
if parser.token == token::Eof {
|
||||
let mut err = cx.struct_span_err(sp, "macro requires a boolean expression as an argument");
|
||||
err.span_label(sp, "boolean expression required");
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
Ok(Assert {
|
||||
cond_expr: parser.parse_expr()?,
|
||||
custom_message: if parser.eat(&token::Comma) {
|
||||
let ts = parser.parse_tokens();
|
||||
if !ts.is_empty() {
|
||||
Some(ts)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue