Skip to content

destructure record rest elements#8317

Open
tsnobip wants to merge 9 commits intomasterfrom
fix-8311
Open

destructure record rest elements#8317
tsnobip wants to merge 9 commits intomasterfrom
fix-8311

Conversation

@tsnobip
Copy link
Copy Markdown
Member

@tsnobip tsnobip commented Mar 28, 2026

fixes #8311

@tsnobip tsnobip changed the title destructure record rest elements - fixes #8311 destructure record rest elements Mar 28, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 28, 2026

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8317

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8317

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8317

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8317

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8317

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8317

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8317

commit: 2245765

@tsnobip tsnobip marked this pull request as ready for review March 31, 2026 10:51
construct ~loc ~attrs (map_loc sub l) (map_opt (sub.pat sub) p)
| Ppat_variant (l, p) -> variant ~loc ~attrs l (map_opt (sub.pat sub) p)
| Ppat_record (lpl, cf) ->
| Ppat_record (lpl, cf, _rest) ->
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about what should be done here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question: it goes to ast0 and then back during ppx, so any use of a ppx would discard this at the moment, and change the code

normally, one tries to encode additional ast information in special annotations, so they can be recovered on the way back (from_0)

@cknitt
Copy link
Copy Markdown
Member

cknitt commented Mar 31, 2026

@codex review

@cknitt cknitt requested a review from cristianoc March 31, 2026 11:29
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 224576540e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2747 to +2748
match pat.pat_desc with
| Tpat_record (_, _, Some rest) ->
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Bind rest variables in nested record patterns

inject_record_rest_binding only handles cases where the top-level pattern is Tpat_record (_, _, Some rest). When a record-rest appears inside another pattern shape (for example a tuple, constructor, or-or pattern), no Precord_spread_new binding is emitted even though typing has already introduced rest as a bound identifier. This causes incorrect code generation for valid nested destructuring forms because the rest variable is referenced without being initialized from the matched record.

Useful? React with 👍 / 👎.

Comment on lines +186 to 189
| Ppat_record (pl, _, _rest) ->
List.iter
(fun {lid = lbl; x = p} ->
add bv lbl;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track dependencies from record-rest annotations

The dependency walker now matches Ppat_record (pl, _, _rest) but never visits _rest. As a result, module/type paths used only in record-rest annotations (e.g. ...M.t as rest) are omitted from dependency collection, so changing those modules may not trigger recompilation or proper dependency invalidation.

Useful? React with 👍 / 👎.

Comment on lines +1552 to +1553
| PatRest rest_pat -> (fields, flag, Some rest_pat)
| PatUnderscore -> (fields, flag, rest))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject duplicate record-rest clauses during parsing

parse_record_pattern stores rest as Some rest_pat every time it sees PatRest and does not check whether a previous rest was already parsed. This lets patterns with multiple rest clauses be accepted while silently discarding one clause, which is ambiguous and can mask user mistakes instead of producing a syntax error.

Useful? React with 👍 / 👎.

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.

Support destructuring/pattern matching of record rest elements

3 participants