Commit f968c231
f968c231f600Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(analyze,codegen): module-level class << self attr_accessor with non-const RHS
Closes #511. module M; class << self; attr_accessor :x; end; end
plus M.x = non_const_rhs previously left both reader and writer
unresolved when the RHS wasn't a ConstantReadNode -- the existing
resolve_module_singleton_accessors only collected constant-RHS
writes and poisoned the slot ("?") otherwise. Roundhouse hit this
with ActiveRecord.adapter = SqliteAdapter.new.
Synthesize a poly-typed backing slot:
-
analyze (collect_module_with_prefix's SingletonClassNode arm): when registering
<Mod>.<accessor>in @module_acc_keys, also register a file-scope const<Mod>_<accessor>with type "poly" in @const_names. The const tracks ANY-RHS state. -
analyze (infer_constant_recv_type):
Mod.accessorreads with no args return "poly" whenfind_module_acc_idxresolves and the constant-fold path is empty or poisoned. Soputs M.adapterroutes the arg through sp_poly_puts. -
codegen (writer path at the
=arm): when the constant-fold resolver returns "" or "?", emitcst_<Mod>_<accessor> = box_expr_to_poly(rhs);. Constant-fold callers still hit the original sentinel-stamp path. -
codegen (compile_constant_recv_expr reader arm): return
cst_<Mod>_<accessor>(sp_RbVal) when the same conditions hold. -
codegen (emit_global_constants): file-scope poly consts can't use the runtime-call default (
sp_box_nil()isn't a constant expression), so emit a struct initializer{SP_TAG_NIL, 0, {0}}pinning the tag to nil. An un-assigned read ofM.xcorrectly compares nil.
Known limitation: when the accessor name collides with
Class#name, the existing infer arm at line ~3390 returns
"string" before this new arm fires, so puts M.name after
M.name = "x" would treat the result as a string at the read
site. Workaround: use a non-colliding accessor name (adapter,
config, etc.); the issue's real-world cases all use unique
names.
Local: 483 tests pass, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f968c231f600
- Parent
- 3f45b24309c5
spinel_analyze.rbmodifiedView file
end def infer_constant_recv_type(nid, mname, recv) # `Mod.accessor` read for a module-level singleton attr_accessor. # The poly-slot const synthesized at module-collect time # backs the read; codegen returns it as cst_<Mod>_<accessor> # (sp_RbVal). Skip when args are present so this doesn't # collide with method-style calls. Issue #511. if recv >= 0 && @nd_type[recv] == "ConstantReadNode" args_id_acc = @nd_arguments[nid] if args_id_acc < 0 || get_args(args_id_acc).length == 0 rcname_acc = @nd_name[recv] if module_name_exists(rcname_acc) == 1 if find_module_acc_idx(rcname_acc + "." + mname) >= 0 slot_a = rcname_acc + "_" + mname if find_const_idx(slot_a) >= 0 rconsts_a = module_acc_resolved(rcname_acc, mname) if rconsts_a == "" || rconsts_a == "?" @needs_rb_value = 1 return "poly" end end end end end end # File operations if recv >= 0 if @nd_type[recv] == "ConstantReadNode" # module-level singleton accessor. Stage 1 of the # accessor's value is resolved later via the constant-fold pass # (rewrite_module_singleton_accessors) once we've seen all writes. # ALSO synthesize a poly-typed file-scope const `<Mod>_<accessor>` # so that non-constant assignments (`M.adapter = "x"`) have a # backing slot to write to. The constant-fold path takes priority # when every RHS is a ConstantReadNode; otherwise codegen falls # through to a direct cst_<Mod>_<accessor> read/write. Issue #511. if @nd_type[sid] == "SingletonClassNode" sbody = @nd_body[sid] if sbody >= 0 accessor = @nd_content[aid] @module_acc_keys.push(mname + "." + accessor) @module_acc_consts.push("") slot_name = mname + "_" + accessor if find_const_idx(slot_name) < 0 @const_names.push(slot_name) @const_types.push("poly") @const_expr_ids.push(-1) @const_scope_names.push(mname) @needs_rb_value = 1 end end } endspinel_codegen.rbmodifiedView file
Loading diff…
test/module_singleton_attr_accessor.rbaddedView file
Loading diff…
test/module_singleton_attr_accessor.rb.expectedaddedView file
Loading diff…