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.accessor reads with no args return "poly" when find_module_acc_idx resolves and the constant-fold path is empty or poisoned. So puts M.adapter routes the arg through sp_poly_puts.

  • codegen (writer path at the = arm): when the constant-fold resolver returns "" or "?", emit cst_<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 of M.x correctly 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

@@ -4906,6 +4906,29 @@
49064906 end
49074907
49084908 def infer_constant_recv_type(nid, mname, recv)
4909+ # `Mod.accessor` read for a module-level singleton attr_accessor.
4910+ # The poly-slot const synthesized at module-collect time
4911+ # backs the read; codegen returns it as cst_<Mod>_<accessor>
4912+ # (sp_RbVal). Skip when args are present so this doesn't
4913+ # collide with method-style calls. Issue #511.
4914+ if recv >= 0 && @nd_type[recv] == "ConstantReadNode"
4915+ args_id_acc = @nd_arguments[nid]
4916+ if args_id_acc < 0 || get_args(args_id_acc).length == 0
4917+ rcname_acc = @nd_name[recv]
4918+ if module_name_exists(rcname_acc) == 1
4919+ if find_module_acc_idx(rcname_acc + "." + mname) >= 0
4920+ slot_a = rcname_acc + "_" + mname
4921+ if find_const_idx(slot_a) >= 0
4922+ rconsts_a = module_acc_resolved(rcname_acc, mname)
4923+ if rconsts_a == "" || rconsts_a == "?"
4924+ @needs_rb_value = 1
4925+ return "poly"
4926+ end
4927+ end
4928+ end
4929+ end
4930+ end
4931+ end
49094932 # File operations
49104933 if recv >= 0
49114934 if @nd_type[recv] == "ConstantReadNode"
@@ -9051,6 +9074,11 @@
90519074 # module-level singleton accessor. Stage 1 of the
90529075 # accessor's value is resolved later via the constant-fold pass
90539076 # (rewrite_module_singleton_accessors) once we've seen all writes.
9077+ # ALSO synthesize a poly-typed file-scope const `<Mod>_<accessor>`
9078+ # so that non-constant assignments (`M.adapter = "x"`) have a
9079+ # backing slot to write to. The constant-fold path takes priority
9080+ # when every RHS is a ConstantReadNode; otherwise codegen falls
9081+ # through to a direct cst_<Mod>_<accessor> read/write. Issue #511.
90549082 if @nd_type[sid] == "SingletonClassNode"
90559083 sbody = @nd_body[sid]
90569084 if sbody >= 0
@@ -9065,6 +9093,14 @@
90659093 accessor = @nd_content[aid]
90669094 @module_acc_keys.push(mname + "." + accessor)
90679095 @module_acc_consts.push("")
9096+ slot_name = mname + "_" + accessor
9097+ if find_const_idx(slot_name) < 0
9098+ @const_names.push(slot_name)
9099+ @const_types.push("poly")
9100+ @const_expr_ids.push(-1)
9101+ @const_scope_names.push(mname)
9102+ @needs_rb_value = 1
9103+ end
90689104 end
90699105 }
90709106 end