Commit d531879d

d531879d898aMerged

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

Description

Store class-instance-variables in class methods in per-class static slots

A class-instance-variable used inside a class method, e.g. def self.cache; @cache ||= compute; end, emitted self->iv_cache even though the class-method C function takes no self parameter, producing a C compile error 'self' undeclared. This broke the ubiquitous memoized-class-method idiom (def self.config; @config ||= ...; end) found across several gems.

A class method has no instance, so @x there is a class-instance- variable, not an instance ivar. Lower it to a per-class file-scope static slot cst_<Class>_<x>, mirroring the existing module class-method path (cst_<Mod>_<x>).

The slot is typed poly (sp_RbVal). A class-instance-variable is genuinely nullable -- @x ||= ... starts from nil and a pure read returns nil -- and the poly tagged-union is the one storage form with a real nil state at file-scope init ({SP_TAG_NIL,...}). Typed slots (int 0, interned "") have no nil and would break ||= and nil reads. A tagged-union slot for a nullable class-ivar is a type-system representation, not a runtime global sentinel.

analyze registers the slot when walking every def self.X body (and the class << self form), and prefers it over the instance-ivar table in infer_type's ivar-read / or-write arms. codegen mirrors this in ivar_lhs (lowering reads, writes, and the ||= form), in the shared-body ivar-read type arm, and in the statement/expression write and operator/or-write nil-check type lookups.

The change is additive: class-instance-vars in class methods were a hard compile error before, so there is no prior behavior to regress. The compilers themselves declare no class methods, so no cst_<Class>_<x> slots are registered when self-hosting and stage-2 output is unchanged.

Known limitation (pre-existing, not regressed): a class-ivar initialized in the class body (@reg = 5 outside any method) does not share storage with the class-method slot; the gem idioms write inside the method via = / ||=, which work.

Closes #1352

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
d531879d898a
Parent
f09a856abdf8

spinel_analyze.rbmodifiedView file

@@ -2423,12 +2423,20 @@
24232423 # (union of the prior value and the rhs, but Spinel widens those
24242424 # via update_ivar_type already, so reading the slot type is the
24252425 # same answer).
2426+ cls_ct_ow = class_def_ivar_const_type(@nd_name[nid])
2427+ if cls_ct_ow != ""
2428+ return cls_ct_ow
2429+ end
24262430 if @current_class_idx >= 0
24272431 return cls_ivar_type(@current_class_idx, @nd_name[nid])
24282432 end
24292433 return "int"
24302434 end
24312435 if t == "InstanceVariableReadNode"
2436+ cls_ct_r = class_def_ivar_const_type(@nd_name[nid])
2437+ if cls_ct_r != ""
2438+ return cls_ct_r
2439+ end
24322440 if @current_class_idx >= 0
24332441 return cls_ivar_type(@current_class_idx, @nd_name[nid])
24342442 end
@@ -11576,6 +11584,17 @@
1157611584 body_stmts.each { |sid|
1157711585 if @nd_type[sid] == "DefNode"
1157811586 collect_class_method(ci, sid)
11587+ # Class method (`def self.X`) body: a bare `@v` there is a
11588+ # class-instance-variable, not an instance ivar. The emitted
11589+ # C function takes no `self`, so register a `<Class>_<v>` const
11590+ # slot for it to lower against (cst_<Class>_<v>), mirroring the
11591+ # module class-method path. Issue #1352.
11592+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
11593+ dbody_civ = @nd_body[sid]
11594+ if dbody_civ >= 0
11595+ register_class_def_ivars(cname, dbody_civ)
11596+ end
11597+ end
1157911598 end
1158011599 # `class << self; def X; ...; end; end` inside a class body --
1158111600 # the def is a singleton method on the enclosing class,
@@ -11595,6 +11614,11 @@
1159511614 sb_defs = collect_defaults_str(sb)
1159611615 sb_body_id = @nd_body[sb]
1159711616 append_cls_cmeth(ci, sb_name, sb_params, sb_ptypes, "int", sb_body_id, sb_defs)
11617+ # Same class-instance-variable const registration for the
11618+ # `class << self` form. Issue #1352.
11619+ if sb_body_id >= 0
11620+ register_class_def_ivars(cname, sb_body_id)
11621+ end
1159811622 end
1159911623 sbi = sbi + 1
1160011624 end
@@ -14027,6 +14051,123 @@
1402714051 end
1402814052 end
1402914053
14054+ # During class-method body inference, @current_method_name is set
14055+ # to "<Class>_cls_<m>" (see the cmeth inference drivers). A bare
14056+ # `@x` there is a class-instance-variable stored in the per-class
14057+ # `cst_<Class>_<x>` slot. Return that slot's recorded type, or ""
14058+ # when the current scope isn't a class method or no slot matches.
14059+ # Issue #1352.
14060+ def class_def_ivar_const_type(name)
14061+ if @current_method_name == ""
14062+ return ""
14063+ end
14064+ mark = @current_method_name.index("_cls_")
14065+ if mark == nil || mark < 0
14066+ return ""
14067+ end
14068+ owning = @current_method_name[0, mark]
14069+ if find_class_idx(owning) < 0
14070+ return ""
14071+ end
14072+ cname = owning + "_" + name[1, name.length - 1]
14073+ ci = find_const_idx(cname)
14074+ if ci >= 0
14075+ return @const_types[ci]
14076+ end
14077+ ""
14078+ end
14079+
14080+ # Register the per-class const slot `<cname>_<iv>` for a
14081+ # class-instance-variable seen in a `def self.X` body. The slot is
14082+ # typed `poly` (sp_RbVal): a class-instance-variable is genuinely
14083+ # nullable (`@x ||= ...` starts from nil, a pure read returns nil),
14084+ # and the poly tagged-union is the one storage with a real nil state
14085+ # at file-scope init (`{SP_TAG_NIL,...}`). Typed slots (int 0,
14086+ # interned "") have no nil, which breaks `||=` and nil reads.
14087+ # codegen lowers `@x` to this `cst_<Class>_<x>` slot. Issue #1352.
14088+ def register_class_def_ivar_slot(cname, iname)
14089+ slot = cname + "_" + iname[1, iname.length - 1]
14090+ if find_const_idx(slot) >= 0
14091+ return
14092+ end
14093+ @const_names.push(slot)
14094+ @const_types.push("poly")
14095+ @const_expr_ids.push(-1)
14096+ @const_scope_names.push(cname)
14097+ end
14098+
14099+ def register_class_def_ivars(cname, nid)
14100+ if nid < 0
14101+ return
14102+ end
14103+ t = @nd_type[nid]
14104+ if t == "InstanceVariableWriteNode" || t == "InstanceVariableReadNode" ||
14105+ t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableAndWriteNode" ||
14106+ t == "InstanceVariableOrWriteNode" || t == "InstanceVariableTargetNode"
14107+ register_class_def_ivar_slot(cname, @nd_name[nid])
14108+ end
14109+ if @nd_body[nid] >= 0
14110+ register_class_def_ivars(cname, @nd_body[nid])
14111+ end
14112+ stmts = parse_id_list(@nd_stmts[nid])
14113+ k = 0
14114+ while k < stmts.length
14115+ register_class_def_ivars(cname, stmts[k])
14116+ k = k + 1
14117+ end
14118+ if @nd_expression[nid] >= 0
14119+ register_class_def_ivars(cname, @nd_expression[nid])
14120+ end
14121+ if @nd_predicate[nid] >= 0
14122+ register_class_def_ivars(cname, @nd_predicate[nid])
14123+ end
14124+ if @nd_subsequent[nid] >= 0
14125+ register_class_def_ivars(cname, @nd_subsequent[nid])
14126+ end
14127+ if @nd_else_clause[nid] >= 0
14128+ register_class_def_ivars(cname, @nd_else_clause[nid])
14129+ end
14130+ if @nd_receiver[nid] >= 0
14131+ register_class_def_ivars(cname, @nd_receiver[nid])
14132+ end
14133+ if @nd_arguments[nid] >= 0
14134+ register_class_def_ivars(cname, @nd_arguments[nid])
14135+ end
14136+ args = parse_id_list(@nd_args[nid])
14137+ k = 0
14138+ while k < args.length
14139+ register_class_def_ivars(cname, args[k])
14140+ k = k + 1
14141+ end
14142+ conds = parse_id_list(@nd_conditions[nid])
14143+ k = 0
14144+ while k < conds.length
14145+ register_class_def_ivars(cname, conds[k])
14146+ k = k + 1
14147+ end
14148+ if @nd_left[nid] >= 0
14149+ register_class_def_ivars(cname, @nd_left[nid])
14150+ end
14151+ if @nd_right[nid] >= 0
14152+ register_class_def_ivars(cname, @nd_right[nid])
14153+ end
14154+ if @nd_block[nid] >= 0
14155+ register_class_def_ivars(cname, @nd_block[nid])
14156+ end
14157+ elems = parse_id_list(@nd_elements[nid])
14158+ k = 0
14159+ while k < elems.length
14160+ register_class_def_ivars(cname, elems[k])
14161+ k = k + 1
14162+ end
14163+ if @nd_rescue_clause[nid] >= 0
14164+ register_class_def_ivars(cname, @nd_rescue_clause[nid])
14165+ end
14166+ if @nd_ensure_clause[nid] >= 0
14167+ register_class_def_ivars(cname, @nd_ensure_clause[nid])
14168+ end
14169+ end
14170+
1403014171 def infer_ivar_init_type(nid)
1403114172 if nid < 0
1403214173 return "int"