1 use rustc_errors::{Applicability, DiagnosticBuilder};
5 use rustc_ast::tokenstream::{DelimSpan, TokenStream};
6 use rustc_ast::{self as ast, *};
7 use rustc_ast_pretty::pprust;
8 use rustc_expand::base::*;
9 use rustc_parse::parser::Parser;
10 use rustc_span::symbol::{sym, Ident, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
13 pub fn expand_assert<'cx>(
14 cx: &'cx mut ExtCtxt<'_>,
17 ) -> Box<dyn MacResult + 'cx> {
18 let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
22 return DummyResult::any(sp);
26 // `core::panic` and `std::panic` are different macros, so we use call-site
27 // context to pick up whichever is currently in scope.
28 let sp = cx.with_call_site_ctxt(sp);
30 let panic_call = if let Some(tokens) = custom_message {
31 // Pass the custom message to panic!().
34 ExprKind::MacCall(MacCall {
35 path: Path::from_ident(Ident::new(sym::panic, sp)),
36 args: P(MacArgs::Delimited(
37 DelimSpan::from_single(sp),
38 MacDelimiter::Parenthesis,
41 prior_type_ascription: None,
45 // Pass our own message directly to $crate::panicking::panic(),
46 // because it might contain `{` and `}` that should always be
50 cx.std_path(&[sym::panicking, sym::panic]),
53 Symbol::intern(&format!(
54 "assertion failed: {}",
55 pprust::expr_to_string(&cond_expr).escape_debug()
61 cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);
62 MacEager::expr(if_expr)
66 cond_expr: P<ast::Expr>,
67 custom_message: Option<TokenStream>,
74 ) -> Result<Assert, DiagnosticBuilder<'a>> {
75 let mut parser = cx.new_parser_from_tts(stream);
77 if parser.token == token::Eof {
78 let mut err = cx.struct_span_err(sp, "macro requires a boolean expression as an argument");
79 err.span_label(sp, "boolean expression required");
83 let cond_expr = parser.parse_expr()?;
85 // Some crates use the `assert!` macro in the following form (note extra semicolon):
91 // Emit an error about semicolon and suggest removing it.
92 if parser.token == token::Semi {
93 let mut err = cx.struct_span_err(sp, "macro requires an expression as an argument");
96 "try removing semicolon",
98 Applicability::MaybeIncorrect,
105 // Some crates use the `assert!` macro in the following form (note missing comma before
108 // assert!(true "error message");
110 // Emit an error and suggest inserting a comma.
112 if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
113 let mut err = cx.struct_span_err(parser.token.span, "unexpected string literal");
114 let comma_span = parser.prev_token.span.shrink_to_hi();
115 err.span_suggestion_short(
117 "try adding a comma",
119 Applicability::MaybeIncorrect,
123 parse_custom_message(&mut parser)
124 } else if parser.eat(&token::Comma) {
125 parse_custom_message(&mut parser)
130 if parser.token != token::Eof {
131 return parser.unexpected();
134 Ok(Assert { cond_expr, custom_message })
137 fn parse_custom_message(parser: &mut Parser<'_>) -> Option<TokenStream> {
138 let ts = parser.parse_tokens();
139 if !ts.is_empty() { Some(ts) } else { None }