2020-01-09 10:18:47 +00:00
|
|
|
use rustc_errors::{Applicability, DiagnosticBuilder};
|
2019-02-04 12:49:54 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2020-10-18 20:30:16 +00:00
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::{self as ast, *};
|
2020-01-11 16:02:46 +00:00
|
|
|
use rustc_ast_pretty::pprust;
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::*;
|
2019-10-15 20:48:13 +00:00
|
|
|
use rustc_parse::parser::Parser;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{sym, Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2018-03-07 07:13:15 +00:00
|
|
|
|
|
|
|
pub fn expand_assert<'cx>(
|
2019-02-04 12:49:54 +00:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
2018-03-07 07:13:15 +00:00
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream,
|
2018-07-12 09:58:16 +00:00
|
|
|
) -> Box<dyn MacResult + 'cx> {
|
2018-12-04 19:10:32 +00:00
|
|
|
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
|
|
|
|
Ok(assert) => assert,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
2019-08-13 17:51:54 +00:00
|
|
|
return DummyResult::any(sp);
|
2018-03-07 07:13:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-14 20:17:11 +00:00
|
|
|
// `core::panic` and `std::panic` are different macros, so we use call-site
|
|
|
|
// context to pick up whichever is currently in scope.
|
|
|
|
let sp = cx.with_call_site_ctxt(sp);
|
2020-10-18 20:30:16 +00:00
|
|
|
|
|
|
|
let panic_call = {
|
|
|
|
if let Some(tokens) = custom_message {
|
|
|
|
// Pass the custom message to panic!().
|
|
|
|
cx.expr(
|
|
|
|
sp,
|
|
|
|
ExprKind::MacCall(MacCall {
|
|
|
|
path: Path::from_ident(Ident::new(sym::panic, sp)),
|
|
|
|
args: P(MacArgs::Delimited(
|
|
|
|
DelimSpan::from_single(sp),
|
|
|
|
MacDelimiter::Parenthesis,
|
|
|
|
tokens,
|
|
|
|
)),
|
|
|
|
prior_type_ascription: None,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Pass our own message directly to $crate::panicking::panic(),
|
|
|
|
// because it might contain `{` and `}` that should always be
|
|
|
|
// passed literally.
|
|
|
|
cx.expr_call_global(
|
|
|
|
sp,
|
|
|
|
cx.std_path(&[sym::panicking, sym::panic]),
|
|
|
|
vec![cx.expr_str(
|
|
|
|
DUMMY_SP,
|
|
|
|
Symbol::intern(&format!(
|
|
|
|
"assertion failed: {}",
|
|
|
|
pprust::expr_to_string(&cond_expr).escape_debug()
|
|
|
|
)),
|
|
|
|
)],
|
|
|
|
)
|
|
|
|
}
|
2018-03-07 07:13:15 +00:00
|
|
|
};
|
2020-10-18 20:30:16 +00:00
|
|
|
let if_expr =
|
|
|
|
cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);
|
2018-03-07 07:13:15 +00:00
|
|
|
MacEager::expr(if_expr)
|
|
|
|
}
|
2018-12-04 19:10:32 +00:00
|
|
|
|
|
|
|
struct Assert {
|
|
|
|
cond_expr: P<ast::Expr>,
|
|
|
|
custom_message: Option<TokenStream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_assert<'a>(
|
|
|
|
cx: &mut ExtCtxt<'a>,
|
|
|
|
sp: Span,
|
2019-12-22 22:42:04 +00:00
|
|
|
stream: TokenStream,
|
2018-12-04 19:10:32 +00:00
|
|
|
) -> Result<Assert, DiagnosticBuilder<'a>> {
|
2019-08-31 17:08:06 +00:00
|
|
|
let mut parser = cx.new_parser_from_tts(stream);
|
2018-12-04 19:10:32 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:44:28 +00:00
|
|
|
let cond_expr = parser.parse_expr()?;
|
|
|
|
|
|
|
|
// Some crates use the `assert!` macro in the following form (note extra semicolon):
|
|
|
|
//
|
|
|
|
// assert!(
|
|
|
|
// my_function();
|
|
|
|
// );
|
|
|
|
//
|
2020-02-28 13:10:33 +00:00
|
|
|
// Emit an error about semicolon and suggest removing it.
|
2019-04-24 22:44:28 +00:00
|
|
|
if parser.token == token::Semi {
|
2020-02-28 13:10:33 +00:00
|
|
|
let mut err = cx.struct_span_err(sp, "macro requires an expression as an argument");
|
2019-04-24 22:44:28 +00:00
|
|
|
err.span_suggestion(
|
2019-06-07 10:31:13 +00:00
|
|
|
parser.token.span,
|
2019-04-24 22:44:28 +00:00
|
|
|
"try removing semicolon",
|
|
|
|
String::new(),
|
2019-12-22 22:42:04 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
2019-04-24 22:44:28 +00:00
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
|
|
|
|
parser.bump();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some crates use the `assert!` macro in the following form (note missing comma before
|
|
|
|
// message):
|
|
|
|
//
|
|
|
|
// assert!(true "error message");
|
|
|
|
//
|
2020-02-28 13:10:33 +00:00
|
|
|
// Emit an error and suggest inserting a comma.
|
2019-12-22 22:42:04 +00:00
|
|
|
let custom_message =
|
|
|
|
if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
|
2020-02-28 13:10:33 +00:00
|
|
|
let mut err = cx.struct_span_err(parser.token.span, "unexpected string literal");
|
2020-02-29 11:56:15 +00:00
|
|
|
let comma_span = parser.prev_token.span.shrink_to_hi();
|
2019-12-22 22:42:04 +00:00
|
|
|
err.span_suggestion_short(
|
|
|
|
comma_span,
|
|
|
|
"try adding a comma",
|
|
|
|
", ".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
err.emit();
|
2019-04-24 22:44:28 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
parse_custom_message(&mut parser)
|
|
|
|
} else if parser.eat(&token::Comma) {
|
|
|
|
parse_custom_message(&mut parser)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-04-17 11:01:57 +00:00
|
|
|
|
|
|
|
if parser.token != token::Eof {
|
2020-10-05 21:21:03 +00:00
|
|
|
return parser.unexpected();
|
2019-04-17 11:01:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:44:28 +00:00
|
|
|
Ok(Assert { cond_expr, custom_message })
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:49:03 +00:00
|
|
|
fn parse_custom_message(parser: &mut Parser<'_>) -> Option<TokenStream> {
|
2019-04-24 22:44:28 +00:00
|
|
|
let ts = parser.parse_tokens();
|
2019-12-22 22:42:04 +00:00
|
|
|
if !ts.is_empty() { Some(ts) } else { None }
|
2018-12-04 19:10:32 +00:00
|
|
|
}
|