Commit 5718ac5f

5718ac5f8f6fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Static is_a? guards: drop dead branches in specialized clones

obj.is_a?(Class) (kind_of?/instance_of?) with a concrete object receiver is a compile-time constant, so emit_if now folds such a guard and emits only the live branch. A specialized class-method clone whose param has a known class (Comment.from_row's row is a CommentRow) would otherwise compile the dead row.is_a?(ArticleRow) arm and hit an unresolvable row.title / instance.title=.

Fixes cls_method_specialization_boxer.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
5718ac5f8f6f
Parent
0af422deae50

src/codegen.cmodifiedView file

@@ -14786,12 +14786,68 @@
1478614786 emit_expr(c, id, b);
1478714787 }
1478814788
14789+/* `obj.is_a?(Class)` (or kind_of?/instance_of?) with a concrete object receiver
14790+ is a compile-time constant: 1 (always) / 0 (never) / -1 (unknown). Lets a
14791+ specialized clone whose param has a known class drop statically-dead branches
14792+ (e.g. `instance.title=` in an arm guarded by `row.is_a?(ArticleRow)` when row
14793+ is a CommentRow), which would otherwise emit an unresolvable call. */
14794+static int static_isa_cond(Compiler *c, int pred) {
14795+ const NodeTable *nt = c->nt;
14796+ if (pred < 0 || !nt_type(nt, pred) || strcmp(nt_type(nt, pred), "CallNode")) return -1;
14797+ const char *nm = nt_str(nt, pred, "name");
14798+ if (!nm || (strcmp(nm, "is_a?") && strcmp(nm, "kind_of?") && strcmp(nm, "instance_of?"))) return -1;
14799+ int recv = nt_ref(nt, pred, "receiver");
14800+ if (recv < 0) return -1;
14801+ TyKind rt = comp_ntype(c, recv);
14802+ if (!ty_is_object(rt)) return -1;
14803+ int args = nt_ref(nt, pred, "arguments");
14804+ int ac = 0; const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &ac) : NULL;
14805+ if (ac != 1 || !av || !nt_type(nt, av[0]) || strcmp(nt_type(nt, av[0]), "ConstantReadNode")) return -1;
14806+ int target = comp_class_index(c, nt_str(nt, av[0], "name"));
14807+ if (target < 0) return -1;
14808+ int rcls = ty_object_class(rt);
14809+ if (rcls == target) return 1;
14810+ if (strcmp(nm, "instance_of?") && is_descendant(c, rcls, target)) return 1;
14811+ return 0;
14812+}
14813+
1478914814 static void emit_if(Compiler *c, int id, Buf *b, int indent, int is_unless, int tail) {
1479014815 const NodeTable *nt = c->nt;
1479114816 int pred = nt_ref(nt, id, "predicate");
1479214817 int then_b = nt_ref(nt, id, "statements");
1479314818 int sub = nt_ref(nt, id, is_unless ? "else_clause" : "subsequent");
1479414819
14820+ /* Statically-decidable guard: drop the dead branch entirely. */
14821+ {
14822+ int sc = static_isa_cond(c, pred);
14823+ int eff = (sc < 0) ? -1 : (is_unless ? !sc : sc);
14824+ if (eff == 1) {
14825+ /* condition always true: emit only the then-branch */
14826+ emit_indent(b, indent); buf_puts(b, "{\n");
14827+ if (tail) emit_stmts_tail(c, then_b, b, indent + 1);
14828+ else emit_stmts(c, then_b, b, indent + 1);
14829+ emit_indent(b, indent); buf_puts(b, "}\n");
14830+ return;
14831+ }
14832+ if (eff == 0) {
14833+ /* condition always false: emit only the subsequent (else / elsif) */
14834+ if (sub >= 0) {
14835+ const char *sty = nt_type(nt, sub);
14836+ if (sty && !strcmp(sty, "ElseNode")) {
14837+ emit_indent(b, indent); buf_puts(b, "{\n");
14838+ int s = nt_ref(nt, sub, "statements");
14839+ if (tail) emit_stmts_tail(c, s, b, indent + 1);
14840+ else emit_stmts(c, s, b, indent + 1);
14841+ emit_indent(b, indent); buf_puts(b, "}\n");
14842+ }
14843+ else if (sty && !strcmp(sty, "IfNode")) {
14844+ emit_if(c, sub, b, indent, 0, tail);
14845+ }
14846+ }
14847+ return;
14848+ }
14849+ }
14850+
1479514851 emit_indent(b, indent);
1479614852 buf_puts(b, "if (");
1479714853 if (is_unless) buf_puts(b, "!(");