Add tuple unpacking syntax#23368
Open
tgehr wants to merge 35 commits into
Open
Conversation
…#7) * Fix parsing tuple notation without `-preview=tuples` After #5, without `-preview=tuples`, the parser would cause an assert error when finding tokens that satisfy `isTupleNotation`. * Fix parsing malformed auto declaration starting with `(` Change preview check to an assert after last commit. Handle a tuple declaration that doesn't have `=` after the last `)`. The `peekPastParen(&token).value == TOK.assign` check is not needed because the `parseInitializer` parameter is true so `check(TOK.assign)` does the job and provides a good error message if missing. Add test for malformed auto and non-auto tuple declarations. * Remove unnecessary preview check
Add error for `out` parameters. Ignore `auto ref` after parameter error to avoid unpack error.
Avoid lowering to `auto Type x =`, which causes a redundant `auto` error.
This uses the opening `(` position rather than the first component's.
Require semi-colon after TuplePattern = Initializer.
Although we could allow a list of multiple tuple pattern/init pairs,
let's simplify the grammar to:
```
VarDeclarations:
...
TuplePattern = Initializer;
TuplePattern:
`(` TuplePatternComponents `)`
```
Note AutoDeclaration will still support commas, e.g. `auto (x,) = tup, i
= 1;`.
Grammar changes since approved DIP: AutoDeclaration allows interleaved tuple and non-tuple declarators. VarDeclarations now only takes one TuplePattern. Also change Initializer to AssignExpression (`= void` is not allowed).
2a00ac3 to
d4fbee2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements DIP 1053, "Tuple Unpacking Syntax".
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1053.md
Many thanks to @MetaLang and @ntrel for their essential help with pushing this over the finish line.
The PR includes a changelog entry and a spec update.
Note that the features in this PR are still gated behind the
-preview=tuplesflag. This is because of:tgehr#18 (comment) (comment by @ntrel )
I.e., enabling the parser changes causes error messages in some of the existing
fail_compilationtests to change. If preferred, I think we can get the test suite to pass even when enabling unpacking syntax unconditionally, but it will require updating error messages in some of the tests. A more conservative approach would be to release with the flag first, to avoid disruption in case there is an undetected bug and it unexpectedly does break something in the wild. As a middle ground, we could also release with the flag on by default but still support-revert=tuples. (Which would still require updating the tests.)I think assuming that the intention is to support either
-preview=tuplesor-revert=tuplesin any release, this PR can be merged as-is, otherwise it might make sense to remove the flag first. (Though I would be happy to finally see this upstreamed in either case.)