Commit cbbcef47

cbbcef4706efMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~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
cbbcef4706ef
Parent
db950a409a08

spinel_analyze.rbmodifiedView file

@@ -2303,6 +2303,20 @@
23032303 @needs_rb_value = 1
23042304 return "poly_array"
23052305 end
2306+ # Hash literals as elements (`[{n: 3}, {n: 1}]`): each
2307+ # element is a heap-allocated hash pointer. Spinel has no
2308+ # typed `<hash>_ptr_array` slot, so box via poly_array;
2309+ # sp_box_hash_to_poly is called on each push and the
2310+ # poly-builtin dispatch on `arr[i]` recovers the hash.
2311+ # Without this arm, the array's inferred type fell back to
2312+ # `int_array` (the bottom of this function), and
2313+ # `sp_IntArray_push` was called with a hash pointer —
2314+ # int-from-pointer C-compile error.
2315+ if is_hash_type(et) == 1
2316+ @needs_gc = 1
2317+ @needs_rb_value = 1
2318+ return "poly_array"
2319+ end
23062320 # Check if elements have mixed types
23072321 k = 1
23082322 while k < elems.length
@@ -5945,6 +5959,15 @@
59455959 register_cvar(qname, val_t)
59465960 try_fold_cvar_init(qname, @nd_expression[nid])
59475961 end
5962+ # `@@x op= val` / `@@x ||= val` / `@@x &&= val` — same
5963+ # storage as plain `@@x = ...`. Register the cvar so the
5964+ # static decl pass emits a slot, and seed its type from the
5965+ # rhs (or default int for `||=`/`&&=` reading nil).
5966+ if t == "ClassVariableOperatorWriteNode" || t == "ClassVariableOrWriteNode" || t == "ClassVariableAndWriteNode"
5967+ qname = cvar_qname(class_idx, @nd_name[nid])
5968+ val_t = infer_type(@nd_expression[nid])
5969+ register_cvar(qname, val_t)
5970+ end
59485971 cs = []
59495972 push_child_ids(nid, cs)
59505973 k = 0