Commit 4b4bb554

4b4bb55455a0Merged

Yusuke Endoh <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

fix(codegen): @x ||= expr and @x &&= expr

InstanceVariableOrWriteNode and InstanceVariableAndWriteNode had no codegen and no scan-time registration, so the whole statement degraded to a no-op: the slot was never registered, the struct came out without the field, and reads ground out at the int default. Add four small handlers in spinel_codegen.rb:

  • scan_ivars: register the slot when first seen, seed with nil (||= reads @x first), and immediately update with the rhs's infer_ivar_init_type.

  • scan_writer_calls: widen the slot using infer_type on the rhs. infer_ivar_init_type (used at registration) bottoms out at int for a CallNode rhs; infer_type resolves it to the callee's return type, so @str ||= compute_string widens @str from the nil seed to string.

  • compile_stmt: short-circuit emit if (cond) { rhs; @x = rhs; } with the rhs compiled INSIDE the conditional block so any auxiliary emits land in the conditional body — matches Ruby's short-circuit semantics. cond is !ivar for pointer/scalar slots and the explicit nil/false tag-check for poly slots.

  • compile_expr + infer_type: method bodies whose final stmt is @x ||= … reach compile_expr via the return-value path. Add a value-producing form (delegate to compile_stmt for the side effect, surface @x's post-assign value as the expression result) and report the slot's type so the surrounding method's return type matches.

Tests: ivar_or_write_basic, ivar_and_write_basic.

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

Details

Commit
4b4bb55455a0
Parent
4a8ca9dea493

spinel_codegen.rbmodifiedView file

@@ -2380,6 +2380,16 @@
23802380 end
23812381 return "int"
23822382 end
2383+ if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"
2384+ # `(@x ||= expr)` / `(@x &&= expr)` evaluates to @x's slot type
2385+ # (union of the prior value and the rhs, but Spinel widens those
2386+ # via update_ivar_type already, so reading the slot type is the
2387+ # same answer).
2388+ if @current_class_idx >= 0
2389+ return cls_ivar_type(@current_class_idx, @nd_name[nid])
2390+ end
2391+ return "int"
2392+ end
23832393 if t == "InstanceVariableReadNode"
23842394 if @current_class_idx >= 0
23852395 return cls_ivar_type(@current_class_idx, @nd_name[nid])
@@ -8063,6 +8073,36 @@
80638073 add_ivar(ci, iname, "int")
80648074 end
80658075 end
8076+ # `@x ||= expr` / `@x &&= expr`: register the slot when first
8077+ # encountered. The rhs type seeds the ivar; without registration
8078+ # the struct comes out without the slot and any subsequent read
8079+ # ground out at the int default.
8080+ if @nd_type[nid] == "InstanceVariableOrWriteNode" || @nd_type[nid] == "InstanceVariableAndWriteNode"
8081+ iname = @nd_name[nid]
8082+ expr_first = @nd_expression[nid]
8083+ if ivar_exists(ci, iname) == 0 && ivar_exists_in_ancestor(ci, iname) == 1
8084+ vtype = infer_ivar_init_type(expr_first)
8085+ if vtype != "int" && vtype != "nil"
8086+ update_ivar_type(ci, iname, vtype)
8087+ end
8088+ elsif ivar_exists(ci, iname) == 0
8089+ vtype = infer_ivar_init_type(expr_first)
8090+ # ||= reads @x first; pre-register a nil observation so the
8091+ # `nil + T → T?` rule fires when a later writer-scan widens.
8092+ add_ivar(ci, iname, "nil", 0)
8093+ if vtype != "int" && vtype != "nil"
8094+ update_ivar_type(ci, iname, vtype)
8095+ end
8096+ else
8097+ expr = @nd_expression[nid]
8098+ if expr >= 0 && @nd_type[expr] != "NilNode"
8099+ vtype = infer_ivar_init_type(expr)
8100+ if vtype != "int" && vtype != "nil"
8101+ update_ivar_type(ci, iname, vtype)
8102+ end
8103+ end
8104+ end
8105+ end
80668106 # Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`).
80678107 # Without this branch, ivars assigned only via destructuring never get
80688108 # registered and the struct comes out missing them.
@@ -12215,6 +12255,23 @@
1221512255 end
1221612256 end
1221712257 end
12258+ # `@x ||= expr` / `@x &&= expr`: writer-scan the rhs the same as a
12259+ # plain `@x = expr` so the slot widens from its `nil` seed to the
12260+ # rhs's actual type (`infer_type` resolves CallNode return types,
12261+ # unlike `infer_ivar_init_type` used at registration time).
12262+ if @nd_type[nid] == "InstanceVariableOrWriteNode" || @nd_type[nid] == "InstanceVariableAndWriteNode"
12263+ if @current_class_idx >= 0
12264+ iname = @nd_name[nid]
12265+ expr_id = @nd_expression[nid]
12266+ if expr_id >= 0
12267+ at = infer_type(expr_id)
12268+ record_ivar_observation(@current_class_idx, iname, at, expr_id)
12269+ if at != "int" && at != "nil"
12270+ update_ivar_type(@current_class_idx, iname, at)
12271+ end
12272+ end
12273+ end
12274+ end
1221812275 # Multi-write to ivars: `@a, @b = pulse_0, pulse_1`. Mirrors the
1221912276 # single-write branch above so each ivar's type is widened from
1222012277 # the corresponding RHS slot. Without this the multi-write left
@@ -20818,6 +20875,15 @@
2081820875 end
2081920876 return "(" + ivar_lhs(iname_w) + " = " + val + ")"
2082020877 end
20878+ if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"
20879+ # Expression form of `@x ||= …` / `@x &&= …`. Method bodies
20880+ # whose last statement is one of these reach `compile_expr`
20881+ # via the return-value path, so we need a value-producing
20882+ # form. Run the statement-style emit for the side effect, then
20883+ # surface `@x`'s post-assign value as the expression result.
20884+ compile_stmt(nid)
20885+ return ivar_lhs(@nd_name[nid])
20886+ end
2082120887 if t == "LocalVariableWriteNode"
2082220888 # `local = expr` used as an expression (e.g. inside a chained
2082320889 # `@a = local = expr` write, or `if (sprite = arr[i])` in a
@@ -29975,6 +30041,42 @@
2997530041 end
2997630042 return
2997730043 end
30044+ # `@x ||= expr`: `@x = expr` only when @x is currently nil/false.
30045+ # Lower to `if (!@x) { @x = expr; }`. For pointer-typed slots
30046+ # (sp_Foo *, const char *, ...) the C `!ptr` check is exact; for
30047+ # an int slot we treat 0 as falsy (Spinel's int slot has no
30048+ # separate nil/false state). The `&&= expr` form is the dual.
30049+ # Compile the rhs INSIDE the conditional block so any auxiliary
30050+ # emits land in the conditional body — matches Ruby's
30051+ # short-circuit semantics where the rhs (and its side effects)
30052+ # only run when the lhs is falsy.
30053+ if t == "InstanceVariableOrWriteNode" || t == "InstanceVariableAndWriteNode"
30054+ ivor_iname = @nd_name[nid]
30055+ ivor_lhs = ivar_lhs(ivor_iname)
30056+ ivor_expr_id = @nd_expression[nid]
30057+ ivor_ivar_t = ""
30058+ if @current_class_idx >= 0
30059+ ivor_ivar_t = cls_ivar_type(@current_class_idx, ivor_iname)
30060+ end
30061+ ivor_cond = ""
30062+ if base_type(ivor_ivar_t) == "poly"
30063+ ivor_cond = "(" + ivor_lhs + ").tag == SP_TAG_NIL || ((" + ivor_lhs + ").tag == SP_TAG_BOOL && (" + ivor_lhs + ").v.i == 0)"
30064+ else
30065+ ivor_cond = "!(" + ivor_lhs + ")"
30066+ end
30067+ if t == "InstanceVariableAndWriteNode"
30068+ ivor_cond = "!(" + ivor_cond + ")"
30069+ end
30070+ emit(" if (" + ivor_cond + ") {")
30071+ ivor_val = compile_expr(ivor_expr_id)
30072+ ivor_val_t = infer_type(ivor_expr_id)
30073+ if base_type(ivor_ivar_t) == "poly" && ivor_val_t != "" && ivor_val_t != "poly"
30074+ ivor_val = box_value_to_poly(ivor_val_t, ivor_val)
30075+ end
30076+ emit(" " + ivor_lhs + " = " + ivor_val + ";")
30077+ emit(" }")
30078+ return
30079+ end
2997830080 if t == "InstanceVariableOperatorWriteNode"
2997930081 op = @nd_binop[nid]
2998030082 val = compile_expr(@nd_expression[nid])