1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use syntax::codemap::Spanned;
13 use syntax::ext::base::*;
14 use syntax::ext::build::AstBuilder;
15 use syntax::parse::token;
16 use syntax::print::pprust;
17 use syntax::symbol::Symbol;
18 use syntax::tokenstream::{TokenStream, TokenTree};
19 use syntax_pos::{Span, DUMMY_SP};
21 pub fn expand_assert<'cx>(
25 ) -> Box<MacResult + 'cx> {
26 let mut parser = cx.new_parser_from_tts(tts);
27 let cond_expr = panictry!(parser.parse_expr());
28 let custom_msg_args = if parser.eat(&token::Comma) {
29 let ts = parser.parse_tokens();
39 let sp = sp.apply_mark(cx.current_expansion.mark);
40 let panic_call = Mac_ {
41 path: Path::from_ident(Ident::new(Symbol::intern("panic"), sp)),
42 tts: if let Some(ts) = custom_msg_args {
45 TokenStream::from(TokenTree::Token(
48 token::Lit::Str_(Name::intern(&format!(
49 "assertion failed: {}",
50 pprust::expr_to_string(&cond_expr).escape_debug()
57 let if_expr = cx.expr_if(
59 cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)),
62 ExprKind::Mac(Spanned {
69 MacEager::expr(if_expr)