Skip to content

Commit bb6790b

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 defining `LiteralPattern` as a `LiteralExpression` optionally prefixed by the minus sign. This better matches how the `rustc` AST models this.
1 parent 048d75a commit bb6790b

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

src/patterns.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,12 @@ r[patterns.literal]
139139

140140
r[patterns.literal.syntax]
141141
```grammar,patterns
142-
LiteralPattern ->
143-
`true` | `false`
144-
| CHAR_LITERAL
145-
| BYTE_LITERAL
146-
| STRING_LITERAL
147-
| RAW_STRING_LITERAL
148-
| BYTE_STRING_LITERAL
149-
| RAW_BYTE_STRING_LITERAL
150-
| C_STRING_LITERAL
151-
| RAW_C_STRING_LITERAL
152-
| `-`? INTEGER_LITERAL
153-
| `-`? FLOAT_LITERAL
142+
LiteralPattern -> `-`? LiteralExpression
154143
```
155144

156145
r[patterns.literal.intro]
157146
_Literal patterns_ match exactly the same value as what is created by the literal.
158-
Since negative numbers are not [literals], literal patterns also accept an optional minus sign before the literal, which acts like the negation operator.
147+
Since negative numbers are not [literals], literals in patterns may be prefixed by an optional minus sign, which acts like the negation operator.
159148

160149
> [!WARNING]
161150
> C string and raw C string literals are accepted in literal patterns, but `&CStr` doesn't implement structural equality (`#[derive(Eq, PartialEq)]`) and therefore any such `match` on a `&CStr` will be rejected with a type error.

0 commit comments

Comments
 (0)