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
0 end # Compile a node for use as a C scalar condition. Value-type objects # are passed by value (a struct), and C rejects them as scalars in # `if (...)` etc. In Ruby every non-nil/non-false object is truthy, # so wrap the expression in a comma operator that evaluates it for # side effects then yields 1. def compile_cond_expr(nid) expr = compile_expr(nid) if nid >= 0 && is_value_type_obj(infer_type(nid)) == 1 return "((" + expr + "), 1)" end expr end # ---- Expression compiler ---- def compile_expr(nid) if nid < 0 end def compile_if_expr(nid) cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) then_val = "0" body = @nd_body[nid] if body >= 0 end def compile_unless_expr(nid) cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) then_val = "0" body = @nd_body[nid] if body >= 0 end def compile_if_stmt(nid) cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) emit(" if (" + cond + ") {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) end def compile_unless_stmt(nid) cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) emit(" if (!(" + cond + ")) {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) @hoisted_strlen_var = len_tmp end end cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) emit(" while (" + cond + ") {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) def compile_until_stmt(nid) old = @in_loop @in_loop = 1 cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) emit(" while (!(" + cond + ")) {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) end def compile_if_return(nid, rt) cond = compile_expr(@nd_predicate[nid]) cond = compile_cond_expr(@nd_predicate[nid]) emit(" if (" + cond + ") {") @indent = @indent + 1 body = @nd_body[nid]test/bm_object_truthy.rbaddedView file
Loading diff…