Commit fa7999fe
fa7999fed514Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: don't hoist a bare-read call argument above an in-sequence assignment
A call argument whose parameter is pointer-backed (or poly) is rooted by
emit_arg_rooted into a statement-prelude (g_pre) temp so it survives the
rest of the call. For a bare read (render(a) where a is a local) that
hoist is both unnecessary and wrong: the value is already reachable from
the local's own GC root, and the g_pre line is flushed at the enclosing
statement boundary. When the call sits inside a sequence-expression that
assigns the read variable before the call -- the canonical ERB/mustache
template shape "" + (a = {...}; a["name"] = v; render(a)) -- the prelude
temp captures a ABOVE the in-sequence a = {...}, so the callee gets a
stale (empty) hash and every lookup blanks.
Skip the hoist for a bare read (local/ivar/const/self/nil/string literal)
and emit it inline, matching the existing g_argov skip in emit_args_filled.
A bare read needs no extra root, and emitting it at the call site reads the
variable's value at call time (correct Ruby left-to-right semantics). A
param default like {} (provided < 0) is a fresh allocation and is still
hoisted -- the constructor-arg root case is unaffected.
Reported and minimized by OriPekelman, who traced the blanked template value to this argument-temp hoist (not a GC rooting gap, as first suspected).
Fixes #1478
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- fa7999fed514
- Parent
- 5b6a47688f64
src/codegen_fold.cmodifiedView file
TyKind pt = p ? p->type : TY_UNKNOWN; int poly = (pt == TY_POLY); if (!poly && !needs_root(pt)) { emit_arg_or_default(c, m, idx, provided, out); return; } /* A bare read (local/ivar/const/self/nil/string literal) is already reachable from a root where it lives, so it needs no hoisted temp. Hoisting it into g_pre is also WRONG when the call sits in a sequence-expression that assigns the read variable before the call: the g_pre line is flushed at the statement boundary, capturing the value ABOVE that in-sequence assignment (`a = {...}; foo(a)` as an operand passed a stale `a`). Emit inline, matching the g_argov skip in emit_args_filled. A param default like `{}` (provided<0) is a fresh allocation and still hoisted -- the #1445 root case. */ if (provided >= 0) { const char *aty = nt_type(c->nt, provided); if (aty && (!strcmp(aty, "LocalVariableReadNode") || !strcmp(aty, "InstanceVariableReadNode") || !strcmp(aty, "ConstantReadNode") || !strcmp(aty, "SelfNode") || !strcmp(aty, "NilNode") || !strcmp(aty, "StringNode"))) { emit_arg_or_default(c, m, idx, provided, out); return; } } Buf ab; memset(&ab, 0, sizeof ab); emit_arg_or_default(c, m, idx, provided, &ab); int t = ++g_tmp;test/arg_assigned_in_sequence.rbaddedView file
Loading diff…
test/arg_assigned_in_sequence.rb.expectedaddedView file
Loading diff…