Commit 8ce5456a

8ce5456abae4Merged

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

Description

Support class-variable and constant-path multi-assign targets

@@a, @@b = 1, 2 and a, M::X = 1, 2 previously left the ClassVariableTargetNode / ConstantPathTargetNode unhandled: the parser did not flatten them and the masgn target emitter dropped them, so the destructured value silently produced no assignment.

Flatten both target nodes in spinel_parse.c. In analyze, register and fold class-variable masgn targets so the static cvar decl pass emits an initialized slot from the i-th literal of the right-hand side, matching a plain @@a = 1. In codegen, emit the runtime assignment for both target kinds and teach resolve_const_ref_name to flatten a ConstantPathTargetNode the same way as a ConstantPathNode.

Non-literal class-body cvar init (@@a, @@b = foo(), bar()) stays a pre-existing limitation shared by plain @@a = foo() writes; const-path targets require the constant to be declared, consistent with standalone M::X =.

Fixes #1289

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

Details

Commit
8ce5456abae4
Parent
7131b620300f

spinel_analyze.rbmodifiedView file

@@ -9344,6 +9344,36 @@
93449344 val_t = infer_type(@nd_expression[nid])
93459345 register_cvar(qname, val_t)
93469346 end
9347+ # Class-variable targets in a destructuring assignment
9348+ # (`@@a, @@b = 1, 2`) register and fold their init the same as a
9349+ # plain write, so the static decl pass emits an initialized slot.
9350+ if t == "MultiWriteNode"
9351+ val_id_cv = @nd_expression[nid]
9352+ has_array_rhs_cv = (val_id_cv >= 0 && @nd_type[val_id_cv] == "ArrayNode")
9353+ lefts_cv = parse_id_list(@nd_targets[nid])
9354+ i_cv = 0
9355+ lefts_cv.each { |tid_cv|
9356+ if @nd_type[tid_cv] == "ClassVariableTargetNode"
9357+ qn_cv = cvar_qname(class_idx, @nd_name[tid_cv])
9358+ register_cvar(qn_cv, multi_write_target_type(val_id_cv, i_cv))
9359+ # Fold the i-th literal RHS element into the static init. Index
9360+ # the parse_id_list result directly (an int node-id list) so the
9361+ # node-id passed to try_fold_cvar_init never widens to string.
9362+ if has_array_rhs_cv
9363+ relems_cv = parse_id_list(@nd_elements[val_id_cv])
9364+ if i_cv < relems_cv.length
9365+ try_fold_cvar_init(qn_cv, relems_cv[i_cv])
9366+ end
9367+ end
9368+ end
9369+ i_cv = i_cv + 1
9370+ }
9371+ parse_id_list(@nd_rights[nid]).each { |tid_cv|
9372+ if @nd_type[tid_cv] == "ClassVariableTargetNode"
9373+ register_cvar(cvar_qname(class_idx, @nd_name[tid_cv]), "int")
9374+ end
9375+ }
9376+ end
93479377 cs = []
93489378 push_child_ids(nid, cs)
93499379 k = 0