-
Notifications
You must be signed in to change notification settings - Fork 532
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
Conversation
bb6790b
to
723c407
Compare
_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. |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
723c407
to
1fc4b67
Compare
Update books ## rust-lang/book 4 commits in d33916341d480caede1d0ae57cbeae23aab23e88..230c68bc1e08f5f3228384a28cc228c81dfbd10d 2025-05-19 14:25:14 UTC to 2025-05-08 21:28:56 UTC - Chapter 6 from tech review (rust-lang/book#4370) - Chapter 5 from tech review (rust-lang/book#4359) - Chapter 4 from tech review (rust-lang/book#4358) - Chapter 3 from tech review (rust-lang/book#4353) ## rust-lang/reference 12 commits in 387392674d74656f7cb437c05a96f0c52ea8e601..acd0231ebc74849f6a8907b5e646ce86721aad76 2025-05-19 15:41:22 UTC to 2025-05-06 21:36:01 UTC - Add doc for avx512 target features (rust-lang/reference#1778) - Parse grammar without regexes (rust-lang/reference#1827) - Parse optionals and repeats without regexes (rust-lang/reference#1826) - Fix grammar for `RangePatternBound` regarding literals (rust-lang/reference#1825) - Fix grammar for `LiteralPattern` regarding `-` (rust-lang/reference#1824) - Doc: Add the LoongArch stabilized target features (rust-lang/reference#1707) - Fix naked em-dash (rust-lang/reference#1820) - Add missing attribute for statement macros (rust-lang/reference#1819) - Make linked rules are clicked, highlight the color (rust-lang/reference#1817) - Use the reference grammar for inline assembly (rust-lang/reference#1807) - Fix typo in introduction (rust-lang/reference#1810) - Add an example admonition (rust-lang/reference#1812) ## rust-lang/rust-by-example 2 commits in 8a8918c698534547fa8a1a693cb3e7277f0bfb2f..c9d151f9147c4808c77f0375ba3fa5d54443cb9e 2025-05-13 17:49:05 UTC to 2025-05-13 17:48:43 UTC - fix(docs): standardize on `no_run` attribute for documentation examples (rust-lang/rust-by-example#1929) - Fix typo in Japanese translation (rust-lang/rust-by-example#1928)
Rollup merge of rust-lang#141259 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 4 commits in d33916341d480caede1d0ae57cbeae23aab23e88..230c68bc1e08f5f3228384a28cc228c81dfbd10d 2025-05-19 14:25:14 UTC to 2025-05-08 21:28:56 UTC - Chapter 6 from tech review (rust-lang/book#4370) - Chapter 5 from tech review (rust-lang/book#4359) - Chapter 4 from tech review (rust-lang/book#4358) - Chapter 3 from tech review (rust-lang/book#4353) ## rust-lang/reference 12 commits in 387392674d74656f7cb437c05a96f0c52ea8e601..acd0231ebc74849f6a8907b5e646ce86721aad76 2025-05-19 15:41:22 UTC to 2025-05-06 21:36:01 UTC - Add doc for avx512 target features (rust-lang/reference#1778) - Parse grammar without regexes (rust-lang/reference#1827) - Parse optionals and repeats without regexes (rust-lang/reference#1826) - Fix grammar for `RangePatternBound` regarding literals (rust-lang/reference#1825) - Fix grammar for `LiteralPattern` regarding `-` (rust-lang/reference#1824) - Doc: Add the LoongArch stabilized target features (rust-lang/reference#1707) - Fix naked em-dash (rust-lang/reference#1820) - Add missing attribute for statement macros (rust-lang/reference#1819) - Make linked rules are clicked, highlight the color (rust-lang/reference#1817) - Use the reference grammar for inline assembly (rust-lang/reference#1807) - Fix typo in introduction (rust-lang/reference#1810) - Add an example admonition (rust-lang/reference#1812) ## rust-lang/rust-by-example 2 commits in 8a8918c698534547fa8a1a693cb3e7277f0bfb2f..c9d151f9147c4808c77f0375ba3fa5d54443cb9e 2025-05-13 17:49:05 UTC to 2025-05-13 17:48:43 UTC - fix(docs): standardize on `no_run` attribute for documentation examples (rust-lang/rust-by-example#1929) - Fix typo in Japanese translation (rust-lang/rust-by-example#1928)
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.:In the compiler, this happens in
Parser::parse_literal_maybe_minus
andToken::can_begin_literal_maybe_minus
.Let's fix this by defining
LiteralPattern
as aLiteralExpression
optionally prefixed by the minus sign. This better matches how therustc
AST models this.cc @ehuss