Skip to content

fix: fix assertion failed panic when input is # - #22873

Open
edragain2nd wants to merge 4 commits into
rust-lang:masterfrom
edragain2nd:fix_assertion
Open

fix: fix assertion failed panic when input is ##22873
edragain2nd wants to merge 4 commits into
rust-lang:masterfrom
edragain2nd:fix_assertion

Conversation

@edragain2nd

@edragain2nd edragain2nd commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

fixes #22807
it is because that expressions::expr(p) first consumes # as the start of an attribute.
Since there is no following expression, it returns None.
But the outer marker is abandoned, leading to an invalid top-level parse shape which cause the panic.
So I think it is better to complete a Err Node rather than abondoning.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 21, 2026
Comment thread crates/parser/src/grammar.rs Outdated
pub(crate) fn expr(p: &mut Parser<'_>) {
let m = p.start();
expressions::expr(p);
let starts_with_attr = p.at(T![#]);

@ChayimFriedman2 ChayimFriedman2 Jul 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This function is fine and should not be changed. The change is also wrong - consider e.g. #[attr] 1. The change should probably be in expression::expr(), but I haven't thought where exactly.

View changes since the review

@edragain2nd

Copy link
Copy Markdown
Contributor Author

@rustbot ready

r: Restrictions,
bp: u8,
) -> Option<(CompletedMarker, BlockLike)> {
let mut starts_with_attr = false;

@ChayimFriedman2 ChayimFriedman2 Jul 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems you continue to misunderstand. There's nothing wrong with an expr that starts with #. Therefore it's not what matters and not what we should look at. I'm not sure if just changing m.abandon(p) to m.complete(ERROR) will cause problems, but it's definitely in a better shape.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I remove the fix code and replace m.abandon(p) to m.complete(ERROR) which failed two tests:macro_expansion_tests::builtin_fn_macro::regression_15002
(result: builtin #format_args ("{}", x =; expect:builtin #format_args ("{}", x = );),
macro_expansion_tests::mbe::regression::eager_regression_154032
(result:builtin #format_args ("{}", &[0 2; expect:builtin #format_args ("{}", &[0 2]);)
so in some cases we still need m.abandon(p), maybe change the condition to check whether outer_attrs(p) actually consumes an attribute?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No. The way I proposed is the beginning of a good way, the way of checking # (including checking outer_attrs()) is a dead end. The next thing to check is why the tests fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There are two reasons why tests failed.

  1. when the error recovering is succeeded, m.complete(ERROR) would change the intended recovery shape and cause following tokens to be handled incorrectly by the outer parser layer.
    (test case: macro_expansion_tests::builtin_fn_macro::regression_15002)
  2. when marker is empty, m.complete(ERROR) would add a ERROR node which form a nest ERROR node.
    (test case: tests::parse_err)
    I think both the cases should use m.abandon(p) instead of m.complete(ERROR)

add helper function is_empty()
add new condition check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: rowan: assertion left == right failed

3 participants