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_idsthat returnspoly_array(no<hash>_ptr_arrayslot exists). - SymIntHash / SymStrHash dispatch arms in
compile_poly_method_call's[]-with-symbol-key block soarr[i][:n]resolves through poly to the right hash. Gated on@needs_sym_*_hashso 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
@needs_rb_value = 1 return "poly_array" end # Hash literals as elements (`[{n: 3}, {n: 1}]`): each # element is a heap-allocated hash pointer. Spinel has no # typed `<hash>_ptr_array` slot, so box via poly_array; # sp_box_hash_to_poly is called on each push and the # poly-builtin dispatch on `arr[i]` recovers the hash. # Without this arm, the array's inferred type fell back to # `int_array` (the bottom of this function), and # `sp_IntArray_push` was called with a hash pointer — # int-from-pointer C-compile error. if is_hash_type(et) == 1 @needs_gc = 1 @needs_rb_value = 1 return "poly_array" end # Check if elements have mixed types k = 1 while k < elems.length register_cvar(qname, val_t) try_fold_cvar_init(qname, @nd_expression[nid]) end # `@@x op= val` / `@@x ||= val` / `@@x &&= val` — same # storage as plain `@@x = ...`. Register the cvar so the # static decl pass emits a slot, and seed its type from the # rhs (or default int for `||=`/`&&=` reading nil). if t == "ClassVariableOperatorWriteNode" || t == "ClassVariableOrWriteNode" || t == "ClassVariableAndWriteNode" qname = cvar_qname(class_idx, @nd_name[nid]) val_t = infer_type(@nd_expression[nid]) register_cvar(qname, val_t) end cs = [] push_child_ids(nid, cs) k = 0spinel_codegen.rbmodifiedView file
Loading diff…