[gcc r17-2234] gccrs: Handle assert with custom message

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:59ed43f4ec0f49473dfd4ea34a9adaf8c5e7faf6

commit r17-2234-g59ed43f4ec0f49473dfd4ea34a9adaf8c5e7faf6
Author: Owen Avery <[email protected]>
Date:   Sun Jun 7 19:23:37 2026 -0400

    gccrs: 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 <[email protected]>

Diff:
---
 gcc/rust/expand/rust-macro-builtins-log-debug.cc   | 48 ++++++++++++----------
 gcc/testsuite/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 cef34eb1e033..8c1fd1311a8c 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 71f87aece385..cce0e1031630 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 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.