Skip to content

Accept a nested target as the first for loop index#4156

Merged
kddnewton merged 1 commit into
ruby:mainfrom
shugo:fix/for-nested-mlhs
Jul 6, 2026
Merged

Accept a nested target as the first for loop index#4156
kddnewton merged 1 commit into
ruby:mainfrom
shugo:fix/for-nested-mlhs

Conversation

@shugo

@shugo shugo commented Jul 4, 2026

Copy link
Copy Markdown
Member

A for loop index may be a multiple-assignment target whose first element is itself a parenthesized (nested) target, e.g.

for (a, b), c in 1..10
end

CRuby's parse.y accepts this, and a plain multiple assignment with the same target ((a, b), c = ...) is accepted, but Prism rejected it with "unexpected write target".

When parse_parentheses finishes the first (a, b), it validates the multi-target it produced. In a for index it only allowed the target to be followed by in, so a following , (which continues the index target list) fell through to the "not a statement level" error. Allow a comma there as well.

A `for` loop index may be a multiple-assignment target whose first
element is itself a parenthesized (nested) target, e.g.

    for (a, b), c in 1..10
    end

CRuby's parse.y accepts this, and a plain multiple assignment with the
same target (`(a, b), c = ...`) is accepted, but Prism rejected it with
"unexpected write target".

When parse_parentheses finishes the first `(a, b)`, it validates the
multi-target it produced. In a `for` index it only allowed the target
to be followed by `in`, so a following `,` (which continues the index
target list) fell through to the "not a statement level" error. Allow
a comma there as well.
@shugo shugo force-pushed the fix/for-nested-mlhs branch from a62dfaa to 1b96662 Compare July 6, 2026 02:13
@kddnewton kddnewton merged commit 1f42963 into ruby:main Jul 6, 2026
69 checks passed
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.

2 participants