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
# leaves the cvar at its type-default until first write. @cvar_init_values = "".split(",", -1) @const_expr_ids = [] # Parallel to @const_names: 1 when const `<X>` is compound-assigned # (`X += 2`, `X ||= 2`, ...) somewhere, so its read sites must load the # live `cst_<X>` slot instead of folding to the declaration literal. @const_mutated = [] @const_scope_names = "".split(",", -1) # Parallel to @const_names: when const `<X>`'s init RHS is the # canonical `<Class>.new(...)` shape, record `<Class>` here so end return "int" end # Compound assignment to a constant used in value position # (`x = (A += 2)`); the result type is the constant's own type. if t == "ConstantOperatorWriteNode" || t == "ConstantOrWriteNode" || t == "ConstantAndWriteNode" ci_cw = find_const_idx(resolve_const_read_name(@nd_name[nid])) if ci_cw >= 0 return @const_types[ci_cw] end return "int" end if t == "ConstantPathOperatorWriteNode" || t == "ConstantPathOrWriteNode" || t == "ConstantPathAndWriteNode" cpn_cw = resolve_const_ref_name(@nd_target[nid]) if cpn_cw != "" ci_cw2 = find_const_idx(cpn_cw) if ci_cw2 >= 0 return @const_types[ci_cw2] end end return "int" end if t == "CallNode" return infer_call_type(nid) end end } # Pass 2.1: constants that are compound-assigned anywhere lose their # foldable literal so reads load the live slot (runs after all const # registration so the targets resolve). @const_mutated is sized to # match @const_names here, after every constant is registered. @const_mutated = [] cmi = 0 while cmi < @const_names.length @const_mutated.push(0) cmi = cmi + 1 end stmts.each { |sid| scan_mutated_constants(sid, "") } # Top-level `alias $copy $orig` and BEGIN. Aliases are # recorded into @galias_* (consulted by sanitize_gvar / # scan_features / infer_type so $copy and $orig share @multi_const_inits.push(scope_name + "|" + nid.to_s) end # A constant that is compound-assigned (`A += 2`, `A ||= 2`, ...) is # mutated at runtime, so its value can no longer be folded to the # declaration literal at read sites -- readers must load the live # `cst_<name>` slot. Walk the tree and clear the foldable expr id for # every plain-constant compound-write target, mirroring how a constant # multi-assign target marks @const_expr_ids = -1. Constant-path targets # are never folded, so they need no marking here. def scan_mutated_constants(nid, scope) if nid < 0 return end t = @nd_type[nid] if t == "ConstantOperatorWriteNode" || t == "ConstantOrWriteNode" || t == "ConstantAndWriteNode" ci = -1 if scope != "" ci = find_const_idx(scope + "_" + @nd_name[nid]) end if ci < 0 ci = find_const_idx(@nd_name[nid]) end if ci >= 0 && ci < @const_mutated.length @const_mutated[ci] = 1 end end new_scope = scope if t == "ModuleNode" || t == "ClassNode" cp = @nd_constant_path[nid] if cp >= 0 nm = const_ref_flat_name(cp) if nm != "" new_scope = nm end end end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length scan_mutated_constants(cs[k], new_scope) k = k + 1 end end # Synthetic built-in user class for `method(:foo)` capture: every # `m = method(:foo)` / `obj.method(:foo)` produces an instance of # this class, allocated by sp_Method_new and walked by the auto- buf = ir_emit_sa(buf, "@const_names", @const_names) buf = ir_emit_sa(buf, "@const_types", @const_types) buf = ir_emit_ia(buf, "@const_expr_ids", @const_expr_ids) buf = ir_emit_ia(buf, "@const_mutated", @const_mutated) buf = ir_emit_sa(buf, "@const_scope_names", @const_scope_names) buf = ir_emit_sa(buf, "@const_init_class", @const_init_class) buf = ir_emit_sa(buf, "@cvar_names", @cvar_names)spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/const_compound_assign.rbaddedView file
Loading diff…
test/const_path_compound_assign.rbaddedView file
Loading diff…