Skip to content

Commit 8987532

Browse files
committed
Fix grammar for LiteralPattern regarding -
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 #[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 removing the `-`s from within `LiteralPattern` and instead allowing `LiteralPattern` to be prefixed by an optional `-`. This better matches how the `rustc` AST models this as compared with the alternative of including the optional `-`s pervasively in the `LiteralPattern` production. # Please enter the commit message for your changes. Lines starting # with '#' will be kept; you may remove them yourself if you want to. # An empty message aborts the commit. # # Date: Wed May 14 12:39:49 2025 +0000 # # On branch TC/fix-literal-pattern # Changes to be committed: # modified: src/patterns.md # # Untracked files: # linkcheck.sh # src/TODO.md #
1 parent 048d75a commit 8987532

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/patterns.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PatternNoTopAlt ->
1010
| RangePattern
1111
1212
PatternWithoutRange ->
13-
LiteralPattern
13+
`-`? LiteralPattern
1414
| IdentifierPattern
1515
| WildcardPattern
1616
| RestPattern
@@ -149,8 +149,8 @@ LiteralPattern ->
149149
| RAW_BYTE_STRING_LITERAL
150150
| C_STRING_LITERAL
151151
| RAW_C_STRING_LITERAL
152-
| `-`? INTEGER_LITERAL
153-
| `-`? FLOAT_LITERAL
152+
| INTEGER_LITERAL
153+
| FLOAT_LITERAL
154154
```
155155

156156
r[patterns.literal.intro]

0 commit comments

Comments
 (0)