Commit b86e0e16

b86e0e16f140Merged

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

Description

codegen: truthy-wrap inlined if/unless predicates on sp_RbVal

The inlined statement path (compile_stmt_with_block) emitted the if/unless predicate via the raw compile_expr_remap result. When a mixed-type a && b predicate materialises as an sp_RbVal temp, the emitted if (temp) / if (!(temp)) is not a scalar and trips a C compile ("wrong type argument to unary exclamation mark"). Add a compile_cond_remap helper mirroring compile_cond_expr's truthy wrapping (sp_poly_truthy for poly, nil-sentinel for int?, NULL check for nullable pointers) and route both inline handlers through it. Also trust and_result_type/or_result_type in compile_cond_expr so a stale non-poly infer_type cache from the un-inlined scope cannot drop the sp_poly_truthy wrap.

Fixes #1015

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
b86e0e16f140
Parent
96c8b057fb19

spinel_codegen.rbmodifiedView file

@@ -12957,6 +12957,18 @@
1295712957 def compile_cond_expr(nid)
1295812958 expr = compile_expr(nid)
1295912959 t = infer_type(nid)
12960+ # compile_expr lowers an And/Or node using and_result_type /
12961+ # or_result_type, which materialises an sp_RbVal temp when the
12962+ # branches' types don't unify. infer_type may return a stale,
12963+ # non-poly cache entry for the same node in an inlined context (the
12964+ # cache was filled against the un-inlined scope), so trust the
12965+ # result-type helpers here too. Otherwise `!(temp)` / a missing
12966+ # sp_poly_truthy wrap on the sp_RbVal trips a C compile.
12967+ if nid >= 0 && @nd_type[nid] == "AndNode"
12968+ t = and_result_type(nid)
12969+ elsif nid >= 0 && @nd_type[nid] == "OrNode"
12970+ t = or_result_type(nid)
12971+ end
1296012972 if t == "poly"
1296112973 return "sp_poly_truthy(" + expr + ")"
1296212974 end
@@ -44037,6 +44049,33 @@
4403744049 0
4403844050 end
4403944051
44052+ # Remap-aware truthy condition for the inlined statement path. The
44053+ # raw `compile_expr_remap` result needs the same truthy-wrapping
44054+ # compile_cond_expr applies: a poly predicate (e.g. a mixed-type
44055+ # `a && b` materialised as sp_RbVal) must go through sp_poly_truthy,
44056+ # an int? through the nil sentinel, a nullable pointer through a
44057+ # NULL check. Without this, `if (sp_RbVal)` / `!(sp_RbVal)` trips a
44058+ # C compile in inlined bodies.
44059+ def compile_cond_remap(pred, map_from, map_to)
44060+ cond = compile_expr_remap(pred, map_from, map_to)
44061+ ct = infer_type(pred)
44062+ if pred >= 0 && @nd_type[pred] == "AndNode"
44063+ ct = and_result_type(pred)
44064+ elsif pred >= 0 && @nd_type[pred] == "OrNode"
44065+ ct = or_result_type(pred)
44066+ end
44067+ if ct == "poly"
44068+ return "sp_poly_truthy(" + cond + ")"
44069+ end
44070+ if is_scalar_nullable_type(ct) == 1
44071+ return "(!sp_int_is_nil(" + cond + "))"
44072+ end
44073+ if is_nullable_type(ct) == 1
44074+ return "(" + cond + " != NULL)"
44075+ end
44076+ cond
44077+ end
44078+
4404044079 def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)
4404144080 if nid < 0
4404244081 return
@@ -44187,7 +44226,7 @@
4418744226 return
4418844227 end
4418944228 if t == "IfNode"
44190- cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
44229+ cond = compile_cond_remap(@nd_predicate[nid], map_from, map_to)
4419144230 emit(" if (" + cond + ") {")
4419244231 @indent = @indent + 1
4419344232 # Mirror compile_if_stmt's is_a? narrow push so the y1-inlined
@@ -44291,7 +44330,7 @@
4429144330 # compile_expr_remap's default, which emitted the cond as a
4429244331 # no-op ternary AND the raise unconditionally as a separate
4429344332 # statement. Inline as `if (!cond) { body } [else { else }]`.
44294- cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
44333+ cond = compile_cond_remap(@nd_predicate[nid], map_from, map_to)
4429544334 emit(" if (!(" + cond + ")) {")
4429644335 @indent = @indent + 1
4429744336 body = @nd_body[nid]