rust/compiler/rustc_builtin_macros/src/assert.rs

134 lines
4.0 KiB
Rust
Raw Normal View History

use rustc_errors::{Applicability, DiagnosticBuilder};
2019-02-04 06:49:54 -06:00
use rustc_ast::ptr::P;
use rustc_ast::token::{self, TokenKind};
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
2020-04-27 12:56:11 -05:00
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust;
use rustc_expand::base::*;
use rustc_parse::parser::Parser;
2020-04-19 06:00:18 -05:00
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
pub fn expand_assert<'cx>(
2019-02-04 06:49:54 -06:00
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn MacResult + 'cx> {
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
Ok(assert) => assert,
Err(mut err) => {
err.emit();
return DummyResult::any(sp);
}
};
2019-09-14 15:17:11 -05: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);
let tokens = custom_message.unwrap_or_else(|| {
TokenStream::from(TokenTree::token(
2019-12-22 16:42:04 -06:00
TokenKind::lit(
token::Str,
Symbol::intern(&format!(
"assertion failed: {}",
pprust::expr_to_string(&cond_expr).escape_debug()
)),
None,
),
DUMMY_SP,
))
});
let args = P(MacArgs::Delimited(DelimSpan::from_single(sp), MacDelimiter::Parenthesis, tokens));
2020-02-29 10:32:20 -06:00
let panic_call = MacCall {
path: Path::from_ident(Ident::new(sym::panic, sp)),
args,
prior_type_ascription: None,
};
let if_expr = cx.expr_if(
sp,
cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)),
2020-02-29 10:32:20 -06:00
cx.expr(sp, ExprKind::MacCall(panic_call)),
None,
);
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,
2019-12-22 16:42:04 -06:00
stream: TokenStream,
) -> Result<Assert, DiagnosticBuilder<'a>> {
let mut parser = cx.new_parser_from_tts(stream);
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 17:44:28 -05:00
let cond_expr = parser.parse_expr()?;
// Some crates use the `assert!` macro in the following form (note extra semicolon):
//
// assert!(
// my_function();
// );
//
// Emit an error about semicolon and suggest removing it.
2019-04-24 17:44:28 -05:00
if parser.token == token::Semi {
let mut err = cx.struct_span_err(sp, "macro requires an expression as an argument");
2019-04-24 17:44:28 -05:00
err.span_suggestion(
parser.token.span,
2019-04-24 17:44:28 -05:00
"try removing semicolon",
String::new(),
2019-12-22 16:42:04 -06:00
Applicability::MaybeIncorrect,
2019-04-24 17:44:28 -05:00
);
err.emit();
parser.bump();
}
// Some crates use the `assert!` macro in the following form (note missing comma before
// message):
//
// assert!(true "error message");
//
// Emit an error and suggest inserting a comma.
2019-12-22 16:42:04 -06:00
let custom_message =
if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
let mut err = cx.struct_span_err(parser.token.span, "unexpected string literal");
let comma_span = parser.prev_token.span.shrink_to_hi();
2019-12-22 16:42:04 -06:00
err.span_suggestion_short(
comma_span,
"try adding a comma",
", ".to_string(),
Applicability::MaybeIncorrect,
);
err.emit();
2019-04-24 17:44:28 -05:00
2019-12-22 16:42:04 -06:00
parse_custom_message(&mut parser)
} else if parser.eat(&token::Comma) {
parse_custom_message(&mut parser)
} else {
None
};
if parser.token != token::Eof {
parser.expect_one_of(&[], &[])?;
unreachable!();
}
2019-04-24 17:44:28 -05:00
Ok(Assert { cond_expr, custom_message })
}
2019-06-21 16:49:03 -05:00
fn parse_custom_message(parser: &mut Parser<'_>) -> Option<TokenStream> {
2019-04-24 17:44:28 -05:00
let ts = parser.parse_tokens();
2019-12-22 16:42:04 -06:00
if !ts.is_empty() { Some(ts) } else { None }
}