Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions corpus/830_for_splat_targets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# A for loop with a splat target collects what each gives as ruby's for
# does: several values as they are, one value as an Array when it converts
# to one, none as []. A splat of an index, an attribute or an ivar, and
# several targets, take the same values apart.
o = Object.new
def o.each
yield 1, 2
yield [3, 4]
yield 5
yield
yield [[6]]
yield nil
end

r = []
for *a in o
r << a
end
p r, a

r = []
for x, *y in o
r << [x, y]
end
p r

S = Struct.new(:x)
s = S.new
h = {}
r = []
for *h[:z] in o
r << h[:z]
end
p r

r = []
for *s.x in o
r << s.x
end
p r

r = []
for *@iv in o
r << @iv
end
p r

r = []
for (d, e) in o
r << [d, e]
end
p r

class Pair
def to_ary = [7, 8]
end
r = []
for *a in [Pair.new, [Pair.new]]
r << a.map(&:class)
end
p r

for *n in []
end
p n

def in_method
for *m in [[1, 2], [3]]
end
[m, local_variables]
end
p in_method
30 changes: 30 additions & 0 deletions loop/2026-11-26-stage10-eval-for-splat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# A for loop's splat target on the evaluator

#300's review found it: on the evaluator, `for *a in o` with `o.each` yielding `1, 2` bound `[1]`, and a bare `yield` bound `[nil]`. Ruby binds `[1, 2]` and `[]`; the VM already did.

## Why

The parser writes a for loop as `each` with a block. A splat target alone was given one parameter, `|__for|`, and the assignment `*a = __for`: a block's one parameter keeps the first of several values and is nil for none.

Ruby's for with a splat target (pm_compile_for_node_index) takes what each gave as a rest parameter. When that is one value which `Array.try_convert` turns into an Array, it uses that Array. Then it assigns. Neither `|x|` nor `|*x|` alone does both: `yield [3, 4]` gives `[3, 4]`, `yield 1, 2` gives `[1, 2]`.

Beside it, a splat on a complex target, `for *h[:z] in o` or `for *s.x in o`, was a SyntaxError on both executions: the for loop's own target reader did not know `*`. In ruby both take the same values as `for *a`.

## The change

- `for *a`, and a splat on an ivar, take `|*__forv|` and, before the assignment, ruby's rule (`for_rest_args`): if `__forv.length == 1` and `::Array.try_convert(__forv[0])` is an Array, that Array. The calls are made, as ruby's sequence makes them.
- The for loop's target reader takes a `*` in front of a target (`parse_for_xtargets`), as a multiple assignment's does. One splat target alone (`for *h[:z]`) goes the same way as `for *a`.

## Left as it was

Several complex targets keep main's way on the evaluator, and a splat among them stays a SyntaxError on both:

- `for s.x, b in [[1, 2]]`: ruby 4.0 sends the writer to one of the values (`2.x = 1`). The VM runs ruby's sequence and does the same; the evaluator binds `[[1, 2], nil]`, as on main. Matching ruby's way on the evaluator is a separate item.
- `for h[:k], *c in o`: ruby raises NoMethodError (`[]=` sent to a Symbol, the same way). Taking it on the evaluator in another way would make the two executions differ where they now agree.

## Witnesses

Corpus 830: `for *a` over several values, one Array, one value, none, an Array of an Array and nil, with the variable after the loop;
`for x, *y` and `for (d, e)` beside it; a splat on an index, an attribute and an ivar; an object whose `to_ary` answers, alone and in an Array; a loop that does not run; and the locals of a method with such a loop. It matches ruby on both executions; on main the evaluator differs on the first and both refuse the splat targets.

CHECKED
50 changes: 44 additions & 6 deletions m_parse.mere
Original file line number Diff line number Diff line change
Expand Up @@ -10610,10 +10610,23 @@ and parse_for = fn toks ->
let (e0, r3) = parse_for_expr r2 in
let (e, body, r4) = parse_for_iter (for_bare_do r2) e0 r3 in
(match targets with
| Cons (t, Nil) ->
| Cons (t, Nil) when (match t with ESplat _ -> false | _ -> true) ->
(SExpr (EMethodB (e, "each", Nil, Cons ("__forv", Nil), Cons (for_xassign t (EVar "__forv"), body))), r4)
// (one splat -- `for *h[:k] in` -- collects what each gave, as `for *a`
// does: for_rest_args)
| Cons (ESplat _, Nil) ->
(SExpr (EMethodB (e, "each", Nil, Cons ("*__forv", Nil),
list_append_stmt (for_rest_args ()) (Cons (SMultiAssignX (targets, Cons (ESplat (EVar "__forv"), Nil)), body)))), r4)
// (several: as before. ruby 4.0 sends the writer of a call or index
// target among several to one of the VALUES -- `for o.x, y in [[1,
// 2]]` calls 2.x = 1 -- which the VM, running ruby's sequence, does
// too, and this does not; and a splat among them is refused here, as it
// was, rather than taken in a way the VM's would not)
| _ ->
(SExpr (EMethodB (e, "each", Nil, Cons ("*__forv", Nil), Cons (SMultiAssignX (targets, Cons (ESplat (EVar "__forv"), Nil)), body))), r4))
(if list_has_splat_x targets then
(let _ = (if map_has prim_missing "i" then () else map_set prim_missing "i" (cstr_h "mere-ruby: bad assignment target")) in fail "mere-ruby: bad assignment target")
else
(SExpr (EMethodB (e, "each", Nil, Cons ("*__forv", Nil), Cons (SMultiAssignX (targets, Cons (ESplat (EVar "__forv"), Nil)), body))), r4)))
else
let (vars, r2) = parse_for_vars toks Nil in
let (e0, r3) = parse_for_expr r2 in
Expand All @@ -10639,9 +10652,14 @@ and parse_for = fn toks ->
// is the assignment `*r = [1,2]`, so r is [1,2]. for_assign would have
// made a local literally NAMED "*r" and left r undefined -- and a real
// `|*r|` block param would have wrapped the element one layer too deep.
// (...and it collects what each gave as ruby's for does: the values
// given, or the one value as an Array when it converts to one --
// for_rest_args. Bound through one `|__for|` it lost all but the first
// of several values, and `yield` alone gave [nil].)
(if str_len v > 1 && str_eq (char_at v 0) "*" then
(SExpr (EMethodB (e, "each", Nil, Cons ("__for", Nil),
Cons (SMultiAssign (Cons (v, Nil), Cons (EVar "__for", Nil)), body))), r4)
(SExpr (EMethodB (e, "each", Nil, Cons ("*__forv", Nil),
list_append_stmt (for_rest_args ())
(Cons (SMultiAssign (Cons (v, Nil), Cons (EVar "__forv", Nil)), body)))), r4)
else if str_eq v "*" then
// `for * in xs` binds nothing at all; the loop still runs.
(SExpr (EMethodB (e, "each", Nil, Cons ("__for", Nil), body)), r4)
Expand Down Expand Up @@ -10697,9 +10715,29 @@ and for_targets_complex = fn toks ->
else for_targets_complex r
| Cons (_, r) -> for_targets_complex r

// parse comma-separated complex for-targets, terminated by `in`.
// What a for loop's splat target takes (`for *a`, `for *h[:k]`): the
// values each gave (the block's `*__forv`), or, when that is one value
// that converts to an Array, that Array -- the sequence ruby compiles for
// them (pm_compile_for_node_index): length, [], Array.try_convert, called.
and for_rest_args = fn (u: unit) -> (Cons (SIf (Cons ((EBin ("==", EMethod (EVar "__forv", "length", Nil), EInt 1),
Cons (SAssign ("__fort", EMethod (EConst "::Array", "try_convert",
Cons (EMethod (EVar "__forv", "[]", Cons (EInt 0, Nil)), Nil))),
Cons (SIf (Cons ((EVar "__fort", Cons (SAssign ("__forv", EVar "__fort"), Nil)), Nil), Nil), Nil))), Nil),
Nil),
(Nil : Stmt list)))

and list_has_splat_x = fn (ts: Expr list) ->
match ts with
| Nil -> false
| Cons (ESplat _, _) -> true
| Cons (_, t) -> list_has_splat_x t

// parse comma-separated complex for-targets, terminated by `in` (a `*` in
// front of one is its splat, as in a multiple assignment's)
and parse_for_xtargets = fn toks -> fn (acc: Expr list) ->
let (t, r) = parse_x_target1 toks in
let (t, r) = (match toks with
| Cons (TP star, r0) when str_eq star "*" -> (let (i, r1) = parse_x_target1 r0 in (ESplat i, r1))
| _ -> parse_x_target1 toks) in
match r with
| Cons (TP c, r2) -> if str_eq c "," then parse_for_xtargets r2 (Cons (t, acc)) else (let _ = (if map_has prim_missing "i" then () else map_set prim_missing "i" (mkstr ("mere-ruby: expected in after for" ++ parsing_file ()))) in fail ("mere-ruby: expected in after for" ++ parsing_file ()))
| Cons (TId kw, r2) -> if str_eq kw "in" then (rev_app (Cons (t, acc)) Nil, r2) else (let _ = (if map_has prim_missing "i" then () else map_set prim_missing "i" (mkstr ("mere-ruby: expected in after for" ++ parsing_file ()))) in fail ("mere-ruby: expected in after for" ++ parsing_file ()))
Expand Down
Loading