Skip to content

Commit 97cb6d6

Browse files
powerboat9CohenArthur
authored andcommitted
Handle assert with custom message
The built-in macro "assert" has a second form, with a custom message used on assertion failure. This patch allows the second form to be compiled. gcc/rust/ChangeLog: * expand/rust-macro-builtins-log-debug.cc (MacroBuiltin::assert_handler): Forward any custom message on to the built-in panic macro. gcc/testsuite/ChangeLog: * rust/compile/assert_missing_panic.rs: Add usage of the second form of assert. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
1 parent 696cb48 commit 97cb6d6

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

gcc/rust/expand/rust-macro-builtins-log-debug.cc

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,46 @@ MacroBuiltin::assert_handler (location_t invoc_locus,
3535
Parser<MacroInvocLexer> parser (lex);
3636

3737
auto last_token_id = macro_end_token (tt, parser);
38-
bool has_error = false;
3938

40-
auto expanded_expr = try_expand_many_expr (parser, last_token_id,
41-
invoc.get_expander (), has_error);
42-
if (expanded_expr.size () < 1)
39+
// TODO: less args?
40+
auto expr_to_assert = parser.parse_expr ();
41+
if (!expr_to_assert.has_value ())
4342
{
4443
rust_error_at (invoc_locus,
4544
"macro requires a boolean expression as an argument");
4645
return AST::Fragment::create_error ();
4746
}
48-
auto expr_to_assert = std::move (expanded_expr[0]);
49-
expanded_expr.erase (expanded_expr.begin ());
5047

51-
if (expanded_expr.size () > 1)
52-
{
53-
rust_sorry_at (
54-
invoc_locus,
55-
"The second form of assert with a message is not supported yet");
56-
57-
return AST::Fragment::create_error ();
58-
}
59-
60-
auto pending_invocations = check_for_eager_invocations (expanded_expr);
61-
if (!pending_invocations.empty ())
62-
return make_eager_builtin_invocation (BuiltinMacro::Assert, invoc_locus,
63-
invoc.get_delim_tok_tree (),
64-
std::move (pending_invocations));
48+
// don't need to expand macros -- panic! will handle it
6549

6650
AST::Builder b (invoc_locus);
6751

6852
std::vector<std::unique_ptr<AST::TokenTree>> panic_tree;
6953
const_TokenPtr open = Token::make (TokenId::LEFT_PAREN, invoc_locus);
7054
panic_tree.push_back (std::make_unique<AST::Token> (std::move (open)));
7155

56+
if (parser.maybe_skip_token (COMMA))
57+
{
58+
bool needs_stringify = true;
59+
while (parser.peek_current_token ()->get_id () != last_token_id
60+
&& parser.peek_current_token ()->get_id () != END_OF_FILE)
61+
{
62+
needs_stringify = false;
63+
auto token_tree = parser.parse_token_tree ();
64+
if (!token_tree.has_value ())
65+
{
66+
// error already emitted (?)
67+
return AST::Fragment::create_error ();
68+
}
69+
panic_tree.push_back (std::move (token_tree.value ()));
70+
}
71+
if (needs_stringify)
72+
{
73+
// TODO: insert stringify invocation
74+
(void) 0;
75+
}
76+
}
77+
7278
const_TokenPtr close = Token::make (TokenId::RIGHT_PAREN, invoc_locus);
7379
panic_tree.push_back (std::make_unique<AST::Token> (std::move (close)));
7480

@@ -83,7 +89,7 @@ MacroBuiltin::assert_handler (location_t invoc_locus,
8389
stmts.push_back (std::move (stmt));
8490
auto block = b.block (std::move (stmts));
8591
auto negated_condition = std::unique_ptr<AST::NegationExpr> (
86-
new AST::NegationExpr (std::move (expr_to_assert),
92+
new AST::NegationExpr (std::move (expr_to_assert.value ()),
8793
AST::NegationExpr::ExprType::NOT, {}, invoc_locus));
8894

8995
auto if_expr = std::make_unique<AST::IfExpr> (

gcc/testsuite/rust/compile/assert_missing_panic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ macro_rules! assert {
1010

1111
const _: () = assert!(true);
1212
// { dg-error "could not resolve macro invocation .panic." "" { target *-*-* } .-1 }
13+
14+
const _: () = assert!(true, "oops, {}", 12);
15+
// { dg-error "could not resolve macro invocation .panic." "" { target *-*-* } .-1 }

0 commit comments

Comments
 (0)