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
val_t = infer_type(@nd_expression[nid]) register_cvar(qname, val_t) end # Class-variable targets in a destructuring assignment # (`@@a, @@b = 1, 2`) register and fold their init the same as a # plain write, so the static decl pass emits an initialized slot. if t == "MultiWriteNode" val_id_cv = @nd_expression[nid] has_array_rhs_cv = (val_id_cv >= 0 && @nd_type[val_id_cv] == "ArrayNode") lefts_cv = parse_id_list(@nd_targets[nid]) i_cv = 0 lefts_cv.each { |tid_cv| if @nd_type[tid_cv] == "ClassVariableTargetNode" qn_cv = cvar_qname(class_idx, @nd_name[tid_cv]) register_cvar(qn_cv, multi_write_target_type(val_id_cv, i_cv)) # Fold the i-th literal RHS element into the static init. Index # the parse_id_list result directly (an int node-id list) so the # node-id passed to try_fold_cvar_init never widens to string. if has_array_rhs_cv relems_cv = parse_id_list(@nd_elements[val_id_cv]) if i_cv < relems_cv.length try_fold_cvar_init(qn_cv, relems_cv[i_cv]) end end end i_cv = i_cv + 1 } parse_id_list(@nd_rights[nid]).each { |tid_cv| if @nd_type[tid_cv] == "ClassVariableTargetNode" register_cvar(cvar_qname(class_idx, @nd_name[tid_cv]), "int") end } end cs = [] push_child_ids(nid, cs) k = 0spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/masgn_classvar_target.rbaddedView file
Loading diff…
test/masgn_constpath_target.rbaddedView file
Loading diff…