Commit 4e8f9556

4e8f9556869cMerged

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

Description

fix(codegen): .nil? on module-singleton accessor slot

Module.accessor.nil? where accessor's value type is a class constant (e.g. ActiveRecord.adapter post-#404) used to fall through to the Module.accessor.<method> single-candidate fast path and emit sp_<Klass>_cls_nil_p(): a class-method dispatch against whatever concrete class the resolver pinned. That function is never defined; the call fails at link with "implicit declaration of function 'sp__cls_nil_p'".

The accessor slot is an int sentinel (sp_module__) that's 0 when un-assigned and non-zero when stamped by a Module.accessor = X writer. .nil? should compare the slot against 0, not dispatch on the resolved candidate.

Three coordinated changes:

  1. Recognize mname == "nil?" in the Module.accessor.<m> resolution path (before the single-candidate fast path) and emit (slot == 0).

  2. Always declare the slot when the accessor resolved to at least one candidate, not just multi-candidate. Pre-fix the decl was gated to consts.split(";").length > 1 because the single-candidate read path didn't reference it.

  3. Always emit the slot write at Module.accessor = X, not just multi-candidate. Pre-fix the single-candidate writer returned a no-op assuming reads fold to the constant; but .nil? needs the runtime sentinel update.

Real-blog ActiveRecord.adapter.nil? shape (1 archive error) closes. Regression test pins both the unset (returns nil) and post-assignment (returns false) paths.

Closes #465

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

Details

Commit
4e8f9556869c
Parent
3dc5bee4e5da

spinel_codegen.rbmodifiedView file

@@ -7139,7 +7139,12 @@
71397139 emitted = 0
71407140 while j < @module_acc_keys.length
71417141 consts = @module_acc_consts[j]
7142- if consts != "" && consts != "?" && consts.split(";").length > 1
7142+ # Emit the sentinel slot whenever the accessor resolved to
7143+ # one or more concrete constants. Even the single-candidate
7144+ # case needs the slot: assignments write to it
7145+ # (sp_module_<Mod>_<acc> = sentinel) and reads from `.nil?`
7146+ # / `.is_a?` need to compare against 0 (unassigned).
7147+ if consts != "" && consts != "?"
71437148 key = @module_acc_keys[j]
71447149 # key shape is "<Mod>.<accessor>"; turn the dot into an
71457150 # underscore for the C identifier.
@@ -11711,6 +11716,15 @@
1171111716 k = k + 1
1171211717 end
1171311718 end
11719+ # `.nil?` on a module-accessor slot resolved to one (or more)
11720+ # class candidates: the call should ask whether the slot is
11721+ # assigned, not dispatch a class method on the candidate
11722+ # class. The slot's int sentinel is 0 when unassigned (no
11723+ # module_sentinel maps to 0). Same shape for 0+ candidate
11724+ # cases since both reduce to "is the slot set?".
11725+ if mname == "nil?"
11726+ return "(sp_module_" + mod_name + "_" + sanitize_name(inner_mname) + " == 0)"
11727+ end
1171411728 if cands.length == 1
1171511729 return "sp_" + cands[0] + "_cls_" + sanitize_name(mname) + "(" + arg_strs + ")"
1171611730 end
@@ -17377,6 +17391,16 @@
1737717391 rhs_ne = compile_arg0(nid)
1737817392 return "(!sp_class_eq(" + rc + ", " + rhs_ne + "))"
1737917393 end
17394+ # `<class_value_slot>.nil?` — sp_Class's nil sentinel is
17395+ # cls_id == -1 (matches sp_class_superclass's root return and
17396+ # the default value emitted for unassigned `attr_accessor`
17397+ # class slots). Without this arm the call falls through to the
17398+ # constant-receiver dispatch which would emit
17399+ # `sp_<Klass>_cls_nil_p()` against whatever concrete class the
17400+ # slot was statically resolved to — an undeclared function.
17401+ if mname == "nil?"
17402+ return "((" + rc + ").cls_id == -1)"
17403+ end
1738017404 # class hierarchy queries.
1738117405 # .superclass -> sp_class_superclass(c) (returns sp_Class{-1} for root)
1738217406 # .ancestors -> sp_class_ancestors_arr(c) (returns sp_PolyArray of boxed sp_Class)
@@ -23105,20 +23129,17 @@
2310523129 end
2310623130
2310723131 # `Module.accessor = X` write.
23108- # Stage 1 (1 candidate): no emit; reads fold to that constant.
23109- # Stage 2 (2+ candidates): emit `slot = SP_MOD_<X>;` so the
23110- # read site's sentinel switch picks the right branch.
23132+ # Write the sentinel for the assigned module class. Both
23133+ # single-candidate and multi-candidate cases need this — the
23134+ # `.nil?` reader compares the slot against 0 to detect
23135+ # un-assigned, so the writer must stamp the sentinel even when
23136+ # the resolver only saw one candidate.
2311123137 if mname.length > 1 && mname[mname.length - 1] == "=" && recv >= 0 && @nd_type[recv] == "ConstantReadNode"
2311223138 mod_name = @nd_name[recv]
2311323139 if module_name_exists(mod_name) == 1
2311423140 accessor = mname[0, mname.length - 1]
2311523141 rconsts = module_acc_resolved(mod_name, accessor)
2311623142 if rconsts != "" && rconsts != "?"
23117- cands = rconsts.split(";")
23118- if cands.length == 1
23119- return
23120- end
23121- # Stage 2: write the sentinel for the assigned module.
2312223143 args_id2 = @nd_arguments[nid]
2312323144 if args_id2 >= 0
2312423145 ai = get_args(args_id2)