Recover from tuple patterns with inline element types - #160279
Recover from tuple patterns with inline element types#160279ArneshBanerjee wants to merge 2 commits into
Conversation
Writing the type of each element inside a tuple pattern like `let (a: bool, b: u8) = ...` isn't valid, but the current error doesn't really tell you that. It just says "expected one of `)`, `,`, `@`, `if`, or `|`, found `:`" and leaves you to work it out. A let pattern can be followed by a type, so we can suggest the fix: move the types out into a tuple type after the pattern, so `(a: bool, b: u8)` becomes `(a, b): (bool, u8)`. Elements without a type just get `_`. Parsing then keeps going with the types dropped, so you only get the one error.
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @folkertdev (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
r? fmease |
Rework the recovery to happen in a single parse instead of parsing the tuple pattern, resetting on failure, and parsing again. The element closure now eats an optional `: <ty>` directly, so there's no parser snapshot on the happy path. Only treat the colon as a type annotation when it is followed by whitespace, so `(m:C,)` keeps the existing "maybe write a path separator here" suggestion for `m::C` and only `(m: C,)` gets the tuple-type suggestion.
|
Those are good points, thanks. I've reworked it to do everything in one pass so that there's no snapshot on the happy path anymore. The element closure just eats an optional For the path separator case, I only treat the colon as an annotation when there's whitespace after it. Happy to change the wording if you'd prefer. |
If you write the element types inside a tuple pattern:
the error you get doesn't really explain the problem:
A let pattern can have a type after it, so now we point out what to do instead:
If an element doesn't have a type written, it just gets
_, so(a: bool, b)turns into
(a, b): (bool, _).I kept this to let bindings only. That's the case where the pattern doesn't need
a type of its own, so once the types move into the suggestion the rest of the
statement parses fine and you get a single error instead of a pile of follow-up
ones. Match arms, nested patterns and function parameters are left alone.
Closes #149246