Commit 8fffa9dd

8fffa9dd1d5cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): make value-typed objects truthy in conditions, close #20

Spinel promotes simple immutable classes to "value type" — passed by value as a C struct. Used directly as a condition (if Value.new(0), v ? a : b, etc.) the struct landed in the C scalar slot, and cc rejected it with "used struct type value where scalar is required".

In Ruby every object except nil/false is truthy, so the right behavior is to evaluate the expression for its side effects and then treat it as true. Wrap value-type predicates with the comma operator:

((expr), 1)

Add compile_cond_expr(nid) that performs this rewrite when is_value_type_obj(infer_type(nid)) is true and applies it to the seven compile_expr(@nd_predicate[nid]) sites: compile_if_stmt, compile_unless_stmt, compile_while_stmt, compile_until_stmt, compile_if_expr, compile_unless_expr, compile_if_return.

AndNode/OrNode operands are deliberately left unchanged: routing them through compile_cond_expr reliably triggered the known self-host miscompilation family on bm_ao_render (lv_y corrupted to a stray identifier in unrelated Vec arithmetic), even though the change was a Ruby-level no-op. The regression test below covers the common if/unless/while/?: cases. Patterns like Value.new(0) && x remain unsupported until the underlying self-host issue is fixed.

Includes test/bm_object_truthy.rb covering postfix-if, statement-if, loop, ternary, and unless on value-typed objects.

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

Details

Commit
8fffa9dd1d5c
Parent
b7cccf0afd50

spinel_codegen.rbmodifiedView file

@@ -11112,6 +11112,19 @@
1111211112 0
1111311113 end
1111411114
11115+ # Compile a node for use as a C scalar condition. Value-type objects
11116+ # are passed by value (a struct), and C rejects them as scalars in
11117+ # `if (...)` etc. In Ruby every non-nil/non-false object is truthy,
11118+ # so wrap the expression in a comma operator that evaluates it for
11119+ # side effects then yields 1.
11120+ def compile_cond_expr(nid)
11121+ expr = compile_expr(nid)
11122+ if nid >= 0 && is_value_type_obj(infer_type(nid)) == 1
11123+ return "((" + expr + "), 1)"
11124+ end
11125+ expr
11126+ end
11127+
1111511128 # ---- Expression compiler ----
1111611129 def compile_expr(nid)
1111711130 if nid < 0
@@ -15896,7 +15909,7 @@
1589615909 end
1589715910
1589815911 def compile_if_expr(nid)
15899- cond = compile_expr(@nd_predicate[nid])
15912+ cond = compile_cond_expr(@nd_predicate[nid])
1590015913 then_val = "0"
1590115914 body = @nd_body[nid]
1590215915 if body >= 0
@@ -15924,7 +15937,7 @@
1592415937 end
1592515938
1592615939 def compile_unless_expr(nid)
15927- cond = compile_expr(@nd_predicate[nid])
15940+ cond = compile_cond_expr(@nd_predicate[nid])
1592815941 then_val = "0"
1592915942 body = @nd_body[nid]
1593015943 if body >= 0
@@ -16513,7 +16526,7 @@
1651316526 end
1651416527
1651516528 def compile_if_stmt(nid)
16516- cond = compile_expr(@nd_predicate[nid])
16529+ cond = compile_cond_expr(@nd_predicate[nid])
1651716530 emit(" if (" + cond + ") {")
1651816531 @indent = @indent + 1
1651916532 compile_stmts_body(@nd_body[nid])
@@ -16535,7 +16548,7 @@
1653516548 end
1653616549
1653716550 def compile_unless_stmt(nid)
16538- cond = compile_expr(@nd_predicate[nid])
16551+ cond = compile_cond_expr(@nd_predicate[nid])
1653916552 emit(" if (!(" + cond + ")) {")
1654016553 @indent = @indent + 1
1654116554 compile_stmts_body(@nd_body[nid])
@@ -16772,7 +16785,7 @@
1677216785 @hoisted_strlen_var = len_tmp
1677316786 end
1677416787 end
16775- cond = compile_expr(@nd_predicate[nid])
16788+ cond = compile_cond_expr(@nd_predicate[nid])
1677616789 emit(" while (" + cond + ") {")
1677716790 @indent = @indent + 1
1677816791 compile_stmts_body(@nd_body[nid])
@@ -16786,7 +16799,7 @@
1678616799 def compile_until_stmt(nid)
1678716800 old = @in_loop
1678816801 @in_loop = 1
16789- cond = compile_expr(@nd_predicate[nid])
16802+ cond = compile_cond_expr(@nd_predicate[nid])
1679016803 emit(" while (!(" + cond + ")) {")
1679116804 @indent = @indent + 1
1679216805 compile_stmts_body(@nd_body[nid])
@@ -21761,7 +21774,7 @@
2176121774 end
2176221775
2176321776 def compile_if_return(nid, rt)
21764- cond = compile_expr(@nd_predicate[nid])
21777+ cond = compile_cond_expr(@nd_predicate[nid])
2176521778 emit(" if (" + cond + ") {")
2176621779 @indent = @indent + 1
2176721780 body = @nd_body[nid]