Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions gcc/rust/expand/rust-macro-builtins-log-debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,46 @@ 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);

std::vector<std::unique_ptr<AST::TokenTree>> panic_tree;
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)
{
Comment thread
powerboat9 marked this conversation as resolved.
// TODO: insert stringify invocation
(void) 0;
Comment on lines +73 to +74

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's put a warning here? Not a full-on sorry at, but a warning saying this is a stub implementation of assert

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please open an issue for this, I think it shouldn't be too hard to implement even by a newcomer contributor

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we'd run into issues making sure that stringify! actually calls the built-in stringify!, in all cases. Also, I'll open an issue, but I'm not sure I should insert a sorry warning -- it's a preexisting issue that only effects the message an assert! emits upon failure. If you do still want me to put it in, I will.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to have a rust_warning_at but not a rust_sorry as yeah it's not super important and very specific to assert! as you said

}
}

const_TokenPtr close = Token::make (TokenId::RIGHT_PAREN, invoc_locus);
panic_tree.push_back (std::make_unique<AST::Token> (std::move (close)));

Expand All @@ -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> (
Expand Down
3 changes: 3 additions & 0 deletions gcc/testsuite/rust/compile/assert_missing_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Loading