From: Owen Avery <[email protected]>
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 <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/97cb6d68d3773e1243df9e50adc8eac2d20b81cd
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4581
.../expand/rust-macro-builtins-log-debug.cc | 48 +++++++++++--------
.../rust/compile/assert_missing_panic.rs | 3 ++
2 files changed, 30 insertions(+), 21 deletions(-)
diff --git a/gcc/rust/expand/rust-macro-builtins-log-debug.cc
b/gcc/rust/expand/rust-macro-builtins-log-debug.cc
index cef34eb1e..8c1fd1311 100644
--- a/gcc/rust/expand/rust-macro-builtins-log-debug.cc
+++ b/gcc/rust/expand/rust-macro-builtins-log-debug.cc
@@ -35,33 +35,17 @@ MacroBuiltin::assert_handler (location_t invoc_locus,
Parser<MacroInvocLexer> parser (lex);
auto last_token_id = macro_end_token (tt, parser);
- bool has_error = false;
- auto expanded_expr = try_expand_many_expr (parser, last_token_id,
- invoc.get_expander (), has_error);
- if (expanded_expr.size () < 1)
+ // TODO: less args?
+ auto expr_to_assert = parser.parse_expr ();
+ if (!expr_to_assert.has_value ())
{
rust_error_at (invoc_locus,
"macro requires a boolean expression as an argument");
return AST::Fragment::create_error ();
}
- auto expr_to_assert = std::move (expanded_expr[0]);
- expanded_expr.erase (expanded_expr.begin ());
- if (expanded_expr.size () > 1)
- {
- rust_sorry_at (
- invoc_locus,
- "The second form of assert with a message is not supported yet");
-
- return AST::Fragment::create_error ();
- }
-
- auto pending_invocations = check_for_eager_invocations (expanded_expr);
- if (!pending_invocations.empty ())
- return make_eager_builtin_invocation (BuiltinMacro::Assert, invoc_locus,
- invoc.get_delim_tok_tree (),
- std::move (pending_invocations));
+ // don't need to expand macros -- panic! will handle it
AST::Builder b (invoc_locus);
@@ -69,6 +53,28 @@ MacroBuiltin::assert_handler (location_t invoc_locus,
const_TokenPtr open = Token::make (TokenId::LEFT_PAREN, invoc_locus);
panic_tree.push_back (std::make_unique<AST::Token> (std::move (open)));
+ if (parser.maybe_skip_token (COMMA))
+ {
+ bool needs_stringify = true;
+ while (parser.peek_current_token ()->get_id () != last_token_id
+ && parser.peek_current_token ()->get_id () != END_OF_FILE)
+ {
+ needs_stringify = false;
+ auto token_tree = parser.parse_token_tree ();
+ if (!token_tree.has_value ())
+ {
+ // error already emitted (?)
+ return AST::Fragment::create_error ();
+ }
+ panic_tree.push_back (std::move (token_tree.value ()));
+ }
+ if (needs_stringify)
+ {
+ // TODO: insert stringify invocation
+ (void) 0;
+ }
+ }
+
const_TokenPtr close = Token::make (TokenId::RIGHT_PAREN, invoc_locus);
panic_tree.push_back (std::make_unique<AST::Token> (std::move (close)));
@@ -83,7 +89,7 @@ MacroBuiltin::assert_handler (location_t invoc_locus,
stmts.push_back (std::move (stmt));
auto block = b.block (std::move (stmts));
auto negated_condition = std::unique_ptr<AST::NegationExpr> (
- new AST::NegationExpr (std::move (expr_to_assert),
+ new AST::NegationExpr (std::move (expr_to_assert.value ()),
AST::NegationExpr::ExprType::NOT, {}, invoc_locus));
auto if_expr = std::make_unique<AST::IfExpr> (
diff --git a/gcc/testsuite/rust/compile/assert_missing_panic.rs
b/gcc/testsuite/rust/compile/assert_missing_panic.rs
index 71f87aece..cce0e1031 100644
--- a/gcc/testsuite/rust/compile/assert_missing_panic.rs
+++ b/gcc/testsuite/rust/compile/assert_missing_panic.rs
@@ -10,3 +10,6 @@ macro_rules! assert {
const _: () = assert!(true);
// { dg-error "could not resolve macro invocation .panic." "" { target *-*-* }
.-1 }
+
+const _: () = assert!(true, "oops, {}", 12);
+// { dg-error "could not resolve macro invocation .panic." "" { target *-*-* }
.-1 }
base-commit: 696cb485cfa9969c952d6765889d82ed548ca15c
--
2.54.0