Skip to content

v0.13.1 — an END block records no splat, instead of claiming one at index 0

Choose a tag to compare

@tannevaled tannevaled released this 09 Oct 19:14
9f3311f

Patch, as gorelease -base v0.13.0 reports: no exported API changed.

One line, and a Go zero value that means something

END { } desugars to at_exit { }, and the Block it built left SplatIndex
at Go's zero value. Zero is a perfectly good index, and this field's "no
splat" value is -1:

SplatIndex int // index of the top-level *splat param in Params, or -1

So the block announced a *splat parameter at position 0 with no parameters to
hold it.

Nothing here crashed, which is why it lasted

A parser does not run what it builds, so the whole cost landed in a consumer.
go-embedded-ruby's compiler believed the claim and emitted a rest-binding that
read past an empty environment:

$ echo 'END { puts "E" }' > p.rb && rbgo p.rb
panic: runtime error: slice bounds out of range [:1] with capacity 0
$ echo $?
2

An uncontained Go panic from ordinary Ruby — END has been in the language
since 1.0 — which for an embedder takes the host process down rather than
returning an error. Verified end to end with a replace directive: with this
one line, END, main + END, and BEGIN + main + END all match
MRI 4.0.7 byte for byte.

The guard, which is the part worth having

Two of the three &ast.Block literals in parser.go already set SplatIndex —
the two that build their value from parsed parameters. The one that forgot
is the one with no parameters to parse, and that is the shape any future
desugaring will have.

So the new test is an invariant, not a case: it parses a corpus and asserts,
over every Block in every tree, that SplatIndex is -1 or a valid index into
Params. It walks by reflection rather than a type switch, so a node added later
is covered without anyone remembering to extend the walker — not remembering is
the failure it exists to catch — and it fails when the walk finds no Block at
all, since a walk that read nothing looks exactly like a tree with no defects.

Both tests were checked in the reverse direction: reverting the one-line fix
turns both red.

Upgrading

go get github.com/go-ruby-parser/parser@v0.13.1

No API change. Any consumer that compiles a Block's parameters should take it:
the defect is invisible in the AST and only appears when something lowers it.