Commit 40eca6fa

40eca6faf074Merged

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

Description

Support constant and constant-path compound assignment

A += 2, A ||= 2, A &&= 2 and the constant-path forms M::X += 2 etc. previously failed to compile: the parser had no case for the Constant/ConstantPath Operator/Or/And write nodes, so they fell through to UnsupportedNode and codegen refused them.

Flatten the six node types in spinel_parse.c. In codegen, lower each to a runtime read-modify-write on the cst_<name> slot (constants are write-once at init, but the slot is a mutable global): arithmetic for the operator forms, and a truthy_c_expr-gated conditional for ||= / &&= so Ruby truthiness holds (a zero-valued constant stays put under ||=, since 0 is truthy in Ruby).

Because the slot is now mutated at runtime, its reads can no longer be folded to the declaration literal. A new @const_mutated flag, set by an analyze pass over compound-write targets and threaded through the IR, suppresses that fold in const_literal_c_value while leaving the slot's init untouched, so readers load the live value.

Declared-first constants are the supported case; an undefined constant (no slot) is left unemitted rather than hard-failing, roughly matching CRuby's NameError. Constant-path first-definition stays a pre-existing limitation, the same as standalone M::X =.

Fixes #1290 Refs #1291

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

Details

Commit
40eca6faf074
Parent
e59d250ab7af

spinel_analyze.rbmodifiedView file

@@ -366,6 +366,10 @@
366366 # leaves the cvar at its type-default until first write.
367367 @cvar_init_values = "".split(",", -1)
368368 @const_expr_ids = []
369+ # Parallel to @const_names: 1 when const `<X>` is compound-assigned
370+ # (`X += 2`, `X ||= 2`, ...) somewhere, so its read sites must load the
371+ # live `cst_<X>` slot instead of folding to the declaration literal.
372+ @const_mutated = []
369373 @const_scope_names = "".split(",", -1)
370374 # Parallel to @const_names: when const `<X>`'s init RHS is the
371375 # canonical `<Class>.new(...)` shape, record `<Class>` here so
@@ -2619,6 +2623,25 @@
26192623 end
26202624 return "int"
26212625 end
2626+ # Compound assignment to a constant used in value position
2627+ # (`x = (A += 2)`); the result type is the constant's own type.
2628+ if t == "ConstantOperatorWriteNode" || t == "ConstantOrWriteNode" || t == "ConstantAndWriteNode"
2629+ ci_cw = find_const_idx(resolve_const_read_name(@nd_name[nid]))
2630+ if ci_cw >= 0
2631+ return @const_types[ci_cw]
2632+ end
2633+ return "int"
2634+ end
2635+ if t == "ConstantPathOperatorWriteNode" || t == "ConstantPathOrWriteNode" || t == "ConstantPathAndWriteNode"
2636+ cpn_cw = resolve_const_ref_name(@nd_target[nid])
2637+ if cpn_cw != ""
2638+ ci_cw2 = find_const_idx(cpn_cw)
2639+ if ci_cw2 >= 0
2640+ return @const_types[ci_cw2]
2641+ end
2642+ end
2643+ return "int"
2644+ end
26222645 if t == "CallNode"
26232646 return infer_call_type(nid)
26242647 end
@@ -8611,6 +8634,20 @@
86118634 end
86128635 }
86138636
8637+ # Pass 2.1: constants that are compound-assigned anywhere lose their
8638+ # foldable literal so reads load the live slot (runs after all const
8639+ # registration so the targets resolve). @const_mutated is sized to
8640+ # match @const_names here, after every constant is registered.
8641+ @const_mutated = []
8642+ cmi = 0
8643+ while cmi < @const_names.length
8644+ @const_mutated.push(0)
8645+ cmi = cmi + 1
8646+ end
8647+ stmts.each { |sid|
8648+ scan_mutated_constants(sid, "")
8649+ }
8650+
86148651 # Top-level `alias $copy $orig` and BEGIN. Aliases are
86158652 # recorded into @galias_* (consulted by sanitize_gvar /
86168653 # scan_features / infer_type so $copy and $orig share
@@ -14265,6 +14302,49 @@
1426514302 @multi_const_inits.push(scope_name + "|" + nid.to_s)
1426614303 end
1426714304
14305+ # A constant that is compound-assigned (`A += 2`, `A ||= 2`, ...) is
14306+ # mutated at runtime, so its value can no longer be folded to the
14307+ # declaration literal at read sites -- readers must load the live
14308+ # `cst_<name>` slot. Walk the tree and clear the foldable expr id for
14309+ # every plain-constant compound-write target, mirroring how a constant
14310+ # multi-assign target marks @const_expr_ids = -1. Constant-path targets
14311+ # are never folded, so they need no marking here.
14312+ def scan_mutated_constants(nid, scope)
14313+ if nid < 0
14314+ return
14315+ end
14316+ t = @nd_type[nid]
14317+ if t == "ConstantOperatorWriteNode" || t == "ConstantOrWriteNode" || t == "ConstantAndWriteNode"
14318+ ci = -1
14319+ if scope != ""
14320+ ci = find_const_idx(scope + "_" + @nd_name[nid])
14321+ end
14322+ if ci < 0
14323+ ci = find_const_idx(@nd_name[nid])
14324+ end
14325+ if ci >= 0 && ci < @const_mutated.length
14326+ @const_mutated[ci] = 1
14327+ end
14328+ end
14329+ new_scope = scope
14330+ if t == "ModuleNode" || t == "ClassNode"
14331+ cp = @nd_constant_path[nid]
14332+ if cp >= 0
14333+ nm = const_ref_flat_name(cp)
14334+ if nm != ""
14335+ new_scope = nm
14336+ end
14337+ end
14338+ end
14339+ cs = []
14340+ push_child_ids(nid, cs)
14341+ k = 0
14342+ while k < cs.length
14343+ scan_mutated_constants(cs[k], new_scope)
14344+ k = k + 1
14345+ end
14346+ end
14347+
1426814348 # Synthetic built-in user class for `method(:foo)` capture: every
1426914349 # `m = method(:foo)` / `obj.method(:foo)` produces an instance of
1427014350 # this class, allocated by sp_Method_new and walked by the auto-
@@ -32559,6 +32639,7 @@
3255932639 buf = ir_emit_sa(buf, "@const_names", @const_names)
3256032640 buf = ir_emit_sa(buf, "@const_types", @const_types)
3256132641 buf = ir_emit_ia(buf, "@const_expr_ids", @const_expr_ids)
32642+ buf = ir_emit_ia(buf, "@const_mutated", @const_mutated)
3256232643 buf = ir_emit_sa(buf, "@const_scope_names", @const_scope_names)
3256332644 buf = ir_emit_sa(buf, "@const_init_class", @const_init_class)
3256432645 buf = ir_emit_sa(buf, "@cvar_names", @cvar_names)