-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
never_patterns: Parse match arms with no body #118527
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
(only the last commit changes to the size of AST and/or HIR nodes) |
This comment has been minimized.
This comment has been minimized.
b730dcc
to
7f93db3
Compare
Ok giving up on that last commit, I'll find a better solution later |
This comment has been minimized.
This comment has been minimized.
Because a macro invocation can expand to a never pattern, we can't rule out a `arm!(),` arm at parse time. Instead we detect that case at expansion time, if the macro tries to output a pattern followed by `=>`.
Parsing now accepts a match arm without a body, so we must make sure to only accept that if the pattern is a never pattern.
7f93db3
to
431cc4a
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
@compiler-errors do you think this (or perhaps the associated tracking issue/rfc) would be worth t-style discussion? |
@calebcartwright: I will @bors r+ rollup=never |
☀️ Test successful - checks-actions |
Finished benchmarking commit (2b399b5): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDNext Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 675.635s -> 674.909s (-0.11%) |
Is this change FCP'd or nightly-only? It sounds like this is adding new syntax to the language -- is there a lang sign-off on that somewhere? (I'm surprised this landed without that). |
Perf regression looks to be genuine but likely not worth investigating, limited to single primary incr-unchanged scenario. |
It's not adding syntax to stable rust. What's different is that now the parser accepts match arms with no body and they're rejected later, instead of them being rejected by the parser directly. |
The difference is that match *ptr {
None => { foo(); }
#[cfg(FALSE)]
Some(!),
} will compile on stable. Any unstable syntax needs to be feature-gated at parser level using |
Oh shoot yeah I didn't consider that. I knew there would be a subtlety. To be precise, match *ptr {
None => { foo(); }
#[cfg(FALSE)]
Some(!),
} does not compile on stable because However this now compiles on stable: match Some(0) {
None => { foo(); }
#[cfg(FALSE)]
Some(_)
} which is much worse. |
I don't want to gate a token here though, I want to give a user a normal parse error. Is there a way in the parser to know which feature gates are active? This may not even make sense if we aren't done parsing though... |
Fix is up: #118868 |
…terns-parsing, r=petrochenkov Correctly gate the parsing of match arms without body rust-lang#118527 accidentally allowed the following to parse on stable: ```rust match Some(0) { None => { foo(); } #[cfg(FALSE)] Some(_) } ``` This fixes that oversight. The way I choose which error to emit is the best I could think of, I'm open if you know a better way. r? `@petrochenkov` since you're the one who noticed
Rollup merge of rust-lang#118868 - Nadrieril:correctly-gate-never_patterns-parsing, r=petrochenkov Correctly gate the parsing of match arms without body rust-lang#118527 accidentally allowed the following to parse on stable: ```rust match Some(0) { None => { foo(); } #[cfg(FALSE)] Some(_) } ``` This fixes that oversight. The way I choose which error to emit is the best I could think of, I'm open if you know a better way. r? `@petrochenkov` since you're the one who noticed
…mpiler-errors never_patterns: Parse match arms with no body Never patterns are meant to signal unreachable cases, and thus don't take bodies: ```rust let ptr: *const Option<!> = ...; match *ptr { None => { foo(); } Some(!), } ``` This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser). ~~The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.~~ EDIT: I removed that commit r? `@compiler-errors`
…nkov Don't suggest writing a bodyless arm if the pattern can never be a never pattern rust-lang#118527 enabled arms to be bodyless for never patterns ; this PR removes the `,` and `}` suggestions for patterns that could never be never patterns.
Rollup merge of rust-lang#119380 - ShE3py:match-never-pat, r=petrochenkov Don't suggest writing a bodyless arm if the pattern can never be a never pattern rust-lang#118527 enabled arms to be bodyless for never patterns ; this PR removes the `,` and `}` suggestions for patterns that could never be never patterns.
…cjgillot Don't drop a hir node after lowering Fixes rust-lang#119271. It seems that all hir nodes that get allocated an id must be placed within the hir on pain of ICEs. In rust-lang#118527 I dropped guards on never patterns since they're not useful, which caused the ICE.
Don't drop a hir node after lowering Fixes rust-lang/rust#119271. It seems that all hir nodes that get allocated an id must be placed within the hir on pain of ICEs. In rust-lang/rust#118527 I dropped guards on never patterns since they're not useful, which caused the ICE.
…mpiler-errors never_patterns: Parse match arms with no body Never patterns are meant to signal unreachable cases, and thus don't take bodies: ```rust let ptr: *const Option<!> = ...; match *ptr { None => { foo(); } Some(!), } ``` This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser). ~~The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.~~ EDIT: I removed that commit r? `@compiler-errors`
Never patterns are meant to signal unreachable cases, and thus don't take bodies:
This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser).
The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.EDIT: I removed that commitr? @compiler-errors