Skip to content

Handle assert with custom message#4581

Merged
CohenArthur merged 1 commit into
Rust-GCC:masterfrom
powerboat9:more-assert
Jun 26, 2026
Merged

Handle assert with custom message#4581
CohenArthur merged 1 commit into
Rust-GCC:masterfrom
powerboat9:more-assert

Conversation

@powerboat9

Copy link
Copy Markdown
Collaborator

Fixes #4578

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>

@CohenArthur CohenArthur left a comment

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.

Looks good!

Comment thread gcc/rust/expand/rust-macro-builtins-log-debug.cc
@CohenArthur

Copy link
Copy Markdown
Member

Can you add a testcase for something of this form?

    // 2. begin <= end
    assert!(
        begin <= end,
        "begin <= end ({} <= {}) when slicing `{}`{}",
        begin,
        end,
        s_trunc,
        ellipsis
    );

this is one of the hindrances I ran into with core

@powerboat9

Copy link
Copy Markdown
Collaborator Author

Can you add a testcase for something of this form?

    // 2. begin <= end
    assert!(
        begin <= end,
        "begin <= end ({} <= {}) when slicing `{}`{}",
        begin,
        end,
        s_trunc,
        ellipsis
    );

this is one of the hindrances I ran into with core

I could, but the part of a test I added should already cover this (and does seem to prevent this bit of core from erroring). Note that the formatting itself isn't tested, just the compilation down to a panic! invocation (I don't think we support panicking yet).

Comment thread gcc/rust/expand/rust-macro-builtins-log-debug.cc
Comment on lines +73 to +74
// TODO: insert stringify invocation
(void) 0;

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

@CohenArthur CohenArthur added the core Issue related to the compilation of the `core` crate label Jun 11, 2026

@CohenArthur CohenArthur left a comment

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 we should merge this now as I need it for core and see how we can improve it in the future

@CohenArthur CohenArthur added this pull request to the merge queue Jun 26, 2026
Merged via the queue into Rust-GCC:master with commit 97cb6d6 Jun 26, 2026
13 checks passed
@powerboat9 powerboat9 deleted the more-assert branch June 27, 2026 18:42
@powerboat9

Copy link
Copy Markdown
Collaborator Author

Opened #4635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issue related to the compilation of the `core` crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement second form of assert!() for core

2 participants