Skip to content

Fix grammar for LiteralPattern regarding - #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged

Conversation

traviscross
Copy link
Contributor

@traviscross traviscross commented May 15, 2025

We had documented that only numeric literals in patterns can be prefixed by - (minus), but the Rust parser happily accepts a minus ahead of all literals in patterns. E.g.:

#[cfg(any())]
match () {
    -true | -false => (),
    -'x' => (),
    -b'x' => (),
    -"x" => (),
    -r"x" => (),
    -br"x" => (),
    -c"x" => (),
    -cr"x" => (),
    -1 => (),
    -1.1 => (),
}

In the compiler, this happens in Parser::parse_literal_maybe_minus and Token::can_begin_literal_maybe_minus.

Let's fix this by defining LiteralPattern as a LiteralExpression optionally prefixed by the minus sign. This better matches how the rustc AST models this.

cc @ehuss

@traviscross traviscross force-pushed the TC/fix-literal-pattern branch 6 times, most recently from bb6790b to 723c407 Compare May 15, 2025 07:26
Comment on lines -157 to +146
_Literal patterns_ match exactly the same value as what is created by the literal.
Since negative numbers are not [literals], literal patterns also accept an optional minus sign before the literal, which acts like the negation operator.
_Literal patterns_ match exactly the same value as what is created by the literal. Since negative numbers are not [literals], literals in patterns may be prefixed by an optional minus sign, which acts like the negation operator.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reworded this a bit, as it seemed to be saying two conflicting things. On the one hand, "literal patterns" match exactly what's parsed as a literal. But on the other, it said that "literal patterns" accept a minus sign, which isn't part of the literal. It seems better to just say that literals in patterns may be prefixed by an optional minus sign.

@traviscross traviscross marked this pull request as ready for review May 15, 2025 07:29
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label May 15, 2025
Copy link
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

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

Thanks!

We had documented that only numeric literals in patterns can be
prefixed by `-` (minus), but the Rust parser happily accepts a minus
ahead of all literals in patterns.  E.g.:

```rust
match () {
    -true | -false => (),
    -'x' => (),
    -b'x' => (),
    -"x" => (),
    -r"x" => (),
    -br"x" => (),
    -c"x" => (),
    -cr"x" => (),
    -1 => (),
    -1.1 => (),
}
```

In the compiler, this happens in `Parser::parse_literal_maybe_minus`
and `Token::can_begin_literal_maybe_minus`.

Let's fix this by defining `LiteralPattern` as a `LiteralExpression`
optionally prefixed by the minus sign.  This better matches how the
`rustc` AST models this.
@ehuss ehuss force-pushed the TC/fix-literal-pattern branch from 723c407 to 1fc4b67 Compare May 15, 2025 12:56
@ehuss ehuss enabled auto-merge May 15, 2025 12:56
@ehuss ehuss added this pull request to the merge queue May 15, 2025
Merged via the queue into master with commit bf89144 May 15, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: The marked PR is awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants