Skip to content

Add tuple unpacking syntax#23368

Open
tgehr wants to merge 35 commits into
dlang:masterfrom
tgehr:unpacking
Open

Add tuple unpacking syntax#23368
tgehr wants to merge 35 commits into
dlang:masterfrom
tgehr:unpacking

Conversation

@tgehr

@tgehr tgehr commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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=tuples flag. This is because of:
tgehr#18 (comment) (comment by @ntrel )

BTW if we remove -preview=tuples some existing tests will need fixing. I think I made the fixes in one PR before closing that and deciding to go with the flag instead. I'd say it might be worth merging it with the flag, and then make a separate PR to remove the flag, possibly after a release to get wider testing. Or we could see what upstream maintainers think.

I.e., enabling the parser changes causes error messages in some of the existing fail_compilation tests 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=tuples or -revert=tuples in 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.)

tgehr and others added 30 commits July 6, 2026 02:29
…#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).
@tgehr tgehr requested a review from ibuclaw as a code owner July 6, 2026 01:15
Comment thread compiler/src/dmd/globals.d
@tgehr tgehr force-pushed the unpacking branch 2 times, most recently from 2a00ac3 to d4fbee2 Compare July 6, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants