Commit 9543c3ec

9543c3ec6d60Merged

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

Description

fix(codegen): support @@x op= rhs and array-of-hash-lit typing

Two unrelated issue fixes batched because both happened to land on the same edit cycle.

Fixes #398 — @@x op= rhs (and ||= / &&=) on class variables was rejected as unsupported syntax. The Prism→AST parser had no cases for PM_CLASS_VARIABLE_{OPERATOR,OR,AND}_WRITE_NODE and the codegen had no handlers for the resulting node types. The desugared form @@x = @@x op rhs already worked.

Adds:

  • spinel_parse.c: cases for the three Prism node types.
  • spinel_codegen.rb: collect_cvars_in (registration), compile_stmt (Operator/Or/And arms mirroring GlobalVariableOperator/Or/And), compile_expr (expression-form fallthrough via comma-emit), compile_body_return (last-stmt-of-method return path).

Type widening for nil-init slots (@@x = nil; @@x ||= "str") is left for a follow-up — needs a scan_cvars_widen pass mirroring the ivar widening done in scan_ivars.

Fixes #402 — array literal of hash literals ([{n: 3}, {n: 1}]) was typed as sp_IntArray * (the bottom default in infer_array_elem_type_from_ids), but elements were emitted as sp_SymIntHash * -- sp_IntArray_push(int_array, hash_ptr) fails C-compile.

Adds:

  • A hash-typed-element arm in infer_array_elem_type_from_ids that returns poly_array (no <hash>_ptr_array slot exists).
  • SymIntHash / SymStrHash dispatch arms in compile_poly_method_call's []-with-symbol-key block so arr[i][:n] resolves through poly to the right hash. Gated on @needs_sym_*_hash so we don't pull the helpers in for unrelated programs.

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

Details

Commit
9543c3ec6d60
Parent
2775cd558d53

spinel_codegen.rbmodifiedView file

@@ -2825,6 +2825,20 @@
28252825 @needs_rb_value = 1
28262826 return "poly_array"
28272827 end
2828+ # Hash literals as elements (`[{n: 3}, {n: 1}]`): each
2829+ # element is a heap-allocated hash pointer. Spinel has no
2830+ # typed `<hash>_ptr_array` slot, so box via poly_array;
2831+ # sp_box_hash_to_poly is called on each push and the
2832+ # poly-builtin dispatch on `arr[i]` recovers the hash.
2833+ # Without this arm, the array's inferred type fell back to
2834+ # `int_array` (the bottom of this function), and
2835+ # `sp_IntArray_push` was called with a hash pointer —
2836+ # int-from-pointer C-compile error.
2837+ if is_hash_type(et) == 1
2838+ @needs_gc = 1
2839+ @needs_rb_value = 1
2840+ return "poly_array"
2841+ end
28282842 # Check if elements have mixed types
28292843 k = 1
28302844 while k < elems.length
@@ -7095,6 +7109,15 @@
70957109 register_cvar(qname, val_t)
70967110 try_fold_cvar_init(qname, @nd_expression[nid])
70977111 end
7112+ # `@@x op= val` / `@@x ||= val` / `@@x &&= val` — same
7113+ # storage as plain `@@x = ...`. Register the cvar so the
7114+ # static decl pass emits a slot, and seed its type from the
7115+ # rhs (or default int for `||=`/`&&=` reading nil).
7116+ if t == "ClassVariableOperatorWriteNode" || t == "ClassVariableOrWriteNode" || t == "ClassVariableAndWriteNode"
7117+ qname = cvar_qname(class_idx, @nd_name[nid])
7118+ val_t = infer_type(@nd_expression[nid])
7119+ register_cvar(qname, val_t)
7120+ end
70987121 cs = []
70997122 push_child_ids(nid, cs)
71007123 k = 0
@@ -21620,6 +21643,14 @@
2162021643 register_cvar(qname, val_t)
2162121644 return "(cvar_" + qname + " = " + val + ")"
2162221645 end
21646+ if t == "ClassVariableOperatorWriteNode" || t == "ClassVariableOrWriteNode" || t == "ClassVariableAndWriteNode"
21647+ # Expression form (`x = (@@y op= v)` or last stmt of a
21648+ # class method body). Run the statement-style emit for the
21649+ # side effect, then surface the cvar's post-assign value.
21650+ compile_stmt(nid)
21651+ qname_e = cvar_qname(@current_class_idx, @nd_name[nid])
21652+ return "cvar_" + qname_e
21653+ end
2162321654 if t == "InstanceVariableWriteNode"
2162421655 # Same poly-slot boxing as the statement-form emit.
2162521656 # Expression form (`x = (@y = expr)`) is rarer but reaches
@@ -29072,6 +29103,21 @@
2907229103 shc = "sp_SymPolyHash_get((sp_SymPolyHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + ")"
2907329104 shrhs = is_poly_ret == 1 ? shc : "(" + shc + ").v.i"
2907429105 emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_SYM_POLY_HASH) " + result_tmp + " = " + shrhs + ";")
29106+ # SymIntHash arm: heterogeneous-element array of `{n: 3}`-style
29107+ # hashes (issue #402) boxes each as SymIntHash. Returns mrb_int,
29108+ # box to poly when the call site expects poly. Only emit when
29109+ # the program already uses SymIntHash so we don't pull the
29110+ # runtime helpers in for unrelated programs.
29111+ if @needs_sym_int_hash == 1
29112+ sihc = "sp_SymIntHash_get((sp_SymIntHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + ")"
29113+ sihrhs = is_poly_ret == 1 ? "sp_box_int(" + sihc + ")" : sihc
29114+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_SYM_INT_HASH) " + result_tmp + " = " + sihrhs + ";")
29115+ end
29116+ if @needs_sym_str_hash == 1
29117+ sshc = "sp_SymStrHash_get((sp_SymStrHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + ")"
29118+ sshrhs = is_poly_ret == 1 ? "sp_box_str(" + sshc + ")" : sshc
29119+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_SYM_STR_HASH) " + result_tmp + " = " + sshrhs + ";")
29120+ end
2907529121 end
2907629122 # `length` / `size` — every built-in array exposes its own
2907729123 # `_length` helper (sym_array shares IntArray's). PtrArray is
@@ -31100,6 +31146,66 @@
3110031146 emit(" cvar_" + qname + " = " + val + ";")
3110131147 return
3110231148 end
31149+ if t == "ClassVariableOperatorWriteNode"
31150+ # `@@x op= val` -- desugar to `@@x = @@x op val`. Mirrors
31151+ # GlobalVariableOperatorWriteNode and the instance-var
31152+ # variant. Same storage path as ClassVariableWriteNode.
31153+ qname = cvar_qname(@current_class_idx, @nd_name[nid])
31154+ val = compile_expr(@nd_expression[nid])
31155+ val_t = infer_type(@nd_expression[nid])
31156+ register_cvar(qname, val_t)
31157+ cname = "cvar_" + qname
31158+ op = @nd_binop[nid]
31159+ if op == "+"
31160+ emit(" " + cname + " += " + val + ";")
31161+ elsif op == "-"
31162+ emit(" " + cname + " -= " + val + ";")
31163+ elsif op == "*"
31164+ emit(" " + cname + " *= " + val + ";")
31165+ elsif op == "/"
31166+ emit(" " + cname + " = sp_idiv(" + cname + ", " + val + ");")
31167+ elsif op == "%"
31168+ emit(" " + cname + " = sp_imod(" + cname + ", " + val + ");")
31169+ elsif op == "<<"
31170+ emit(" " + cname + " <<= " + val + ";")
31171+ elsif op == ">>"
31172+ emit(" " + cname + " >>= " + val + ";")
31173+ elsif op == "&"
31174+ emit(" " + cname + " &= " + val + ";")
31175+ elsif op == "|"
31176+ emit(" " + cname + " |= " + val + ";")
31177+ elsif op == "^"
31178+ emit(" " + cname + " ^= " + val + ";")
31179+ else
31180+ emit(" " + cname + " = " + cname + " " + op + " " + val + ";")
31181+ end
31182+ return
31183+ end
31184+ if t == "ClassVariableOrWriteNode"
31185+ # `@@x ||= val` -- evaluate rhs only when @@x is falsy.
31186+ # Mirror of GlobalVariableOrWriteNode.
31187+ qname = cvar_qname(@current_class_idx, @nd_name[nid])
31188+ val_t = infer_type(@nd_expression[nid])
31189+ register_cvar(qname, val_t)
31190+ cname = "cvar_" + qname
31191+ emit(" if (!(" + cname + ")) {")
31192+ val = compile_expr(@nd_expression[nid])
31193+ emit(" " + cname + " = " + val + ";")
31194+ emit(" }")
31195+ return
31196+ end
31197+ if t == "ClassVariableAndWriteNode"
31198+ # `@@x &&= val` -- evaluate rhs only when @@x is truthy.
31199+ qname = cvar_qname(@current_class_idx, @nd_name[nid])
31200+ val_t = infer_type(@nd_expression[nid])
31201+ register_cvar(qname, val_t)
31202+ cname = "cvar_" + qname
31203+ emit(" if (" + cname + ") {")
31204+ val = compile_expr(@nd_expression[nid])
31205+ emit(" " + cname + " = " + val + ";")
31206+ emit(" }")
31207+ return
31208+ end
3110331209 if t == "RedoNode"
3110431210 # `redo` -- jump to the top of the current iteration without
3110531211 # advancing the iterator or re-evaluating the loop guard. The
@@ -32762,6 +32868,10 @@
3276232868 bt = base_type(pred_type)
3276332869 obj_cname = bt[4, bt.length - 4]
3276432870 emit(" sp_" + obj_cname + " *" + tmp + " = " + pred_val + ";")
32871+ elsif pred_type == "poly"
32872+ # `case <poly_value> when ...` — keep the receiver tagged so
32873+ # each when-arm can do tag-check + value-compare. Issue #387.
32874+ emit(" sp_RbVal " + tmp + " = " + pred_val + ";")
3276532875 else
3276632876 emit(" mrb_int " + tmp + " = " + pred_val + ";")
3276732877 end
@@ -32867,7 +32977,34 @@
3286732977 result = result + "0"
3286832978 end
3286932979 else
32870- if pred_type == "string"
32980+ if pred_type == "poly"
32981+ # `case <poly> when <lit>` — Issue #387. The pred_tmp is
32982+ # an sp_RbVal; emit a tag-check + value-compare matched
32983+ # to the literal's type. Class consts (`when Foo`) are
32984+ # left for the future Class-object work; here we only
32985+ # cover the value-literal arms (sym/str/int/float/nil).
32986+ ct = @nd_type[cid]
32987+ if ct == "SymbolNode"
32988+ result = result + "(" + tmp + ".tag == SP_TAG_SYM && " + tmp + ".v.i == " + compile_expr(cid) + ")"
32989+ elsif ct == "StringNode"
32990+ result = result + "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + compile_expr(cid) + ") == 0)"
32991+ elsif ct == "IntegerNode"
32992+ result = result + "(" + tmp + ".tag == SP_TAG_INT && " + tmp + ".v.i == " + compile_expr(cid) + ")"
32993+ elsif ct == "FloatNode"
32994+ result = result + "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == " + compile_expr(cid) + ")"
32995+ elsif ct == "NilNode"
32996+ result = result + "(" + tmp + ".tag == SP_TAG_NIL)"
32997+ elsif ct == "TrueNode"
32998+ result = result + "(" + tmp + ".tag == SP_TAG_BOOL && " + tmp + ".v.b)"
32999+ elsif ct == "FalseNode"
33000+ result = result + "(" + tmp + ".tag == SP_TAG_BOOL && !" + tmp + ".v.b)"
33001+ else
33002+ # Fallback: unbox via sp_poly_eq (does the right thing for
33003+ # numeric / string / sym pairs) and box the rhs.
33004+ @needs_rb_value = 1
33005+ result = result + "sp_poly_eq(" + tmp + ", " + box_expr_to_poly(cid) + ")"
33006+ end
33007+ elsif pred_type == "string"
3287133008 # `case <string> when :sym_literal` — CRuby's
3287233009 # `:sym === "str"` is always false (Symbol#=== returns
3287333010 # false for non-Symbol receivers), so emit a literal
@@ -40114,6 +40251,13 @@
4011440251 end
4011540252 return
4011640253 end
40254+ if lt == "ClassVariableWriteNode" || lt == "ClassVariableOperatorWriteNode" || lt == "ClassVariableOrWriteNode" || lt == "ClassVariableAndWriteNode"
40255+ compile_stmt(last)
40256+ if return_type != "void"
40257+ emit(" return cvar_" + cvar_qname(@current_class_idx, @nd_name[last]) + ";")
40258+ end
40259+ return
40260+ end
4011740261 if lt == "GlobalVariableWriteNode"
4011840262 compile_stmt(last)
4011940263 if return_type != "void"