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

@@ -3010,6 +3010,25 @@
30103010 TyKind pt = p ? p->type : TY_UNKNOWN;
30113011 int poly = (pt == TY_POLY);
30123012 if (!poly && !needs_root(pt)) { emit_arg_or_default(c, m, idx, provided, out); return; }
3013+ /* A bare read (local/ivar/const/self/nil/string literal) is already reachable
3014+ from a root where it lives, so it needs no hoisted temp. Hoisting it into
3015+ g_pre is also WRONG when the call sits in a sequence-expression that assigns
3016+ the read variable before the call: the g_pre line is flushed at the
3017+ statement boundary, capturing the value ABOVE that in-sequence assignment
3018+ (`a = {...}; foo(a)` as an operand passed a stale `a`). Emit inline, matching
3019+ the g_argov skip in emit_args_filled. A param default like `{}` (provided<0)
3020+ is a fresh allocation and still hoisted -- the #1445 root case. */
3021+ if (provided >= 0) {
3022+ const char *aty = nt_type(c->nt, provided);
3023+ if (aty && (!strcmp(aty, "LocalVariableReadNode") ||
3024+ !strcmp(aty, "InstanceVariableReadNode") ||
3025+ !strcmp(aty, "ConstantReadNode") ||
3026+ !strcmp(aty, "SelfNode") || !strcmp(aty, "NilNode") ||
3027+ !strcmp(aty, "StringNode"))) {
3028+ emit_arg_or_default(c, m, idx, provided, out);
3029+ return;
3030+ }
3031+ }
30133032 Buf ab; memset(&ab, 0, sizeof ab);
30143033 emit_arg_or_default(c, m, idx, provided, &ab);
30153034 int t = ++g_tmp;