Commit 18ea9a96

18ea9a96c426Merged

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

Description

Emit a static slot for a cvar first assigned inside a method body

A class variable first assigned inside a method, with no class-body initialization, was never declared in the emitted C, so the build failed with 'cvar_C_x' undeclared. collect_cvars_in deliberately skips DefNode bodies (a method-body literal could widen a class-body-declared type to poly), so such a cvar was never registered, and the static-decl pass emit_global_constants -- which runs before the method bodies are compiled -- had no slot to emit.

Add a second collect pass, collect_method_cvars_in, that runs after the main pass (so every class-body cvar type is already settled) and descends into method bodies. It registers each cvar write target ONLY IF the name is not already known (register_cvar_if_absent), so a genuinely method-only cvar gets a static slot while an established class-body type is never widened, regardless of source order. No init folding: the value is assigned at call time, not startup.

Closes #1354

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

Details

Commit
18ea9a96c426
Parent
9421518d0204

spinel_analyze.rbmodifiedView file

@@ -9998,6 +9998,16 @@
99989998 return
99999999 end
1000010000 collect_cvars_in(root, -1)
10001+ # Second pass: register cvars whose only appearance is inside a
10002+ # method body. collect_cvars_in deliberately skips DefNode bodies
10003+ # to avoid a method-body literal widening a class-body-declared
10004+ # type. After the first pass all class-body types are settled, so
10005+ # this pass walks method bodies and registers each cvar target
10006+ # ONLY IF the name is not already known -- so it can emit a static
10007+ # slot for a genuinely method-only cvar without ever widening an
10008+ # established type. No init folding: the value is assigned at call
10009+ # time, not startup, so the slot keeps its type default.
10010+ collect_method_cvars_in(root, -1)
1000110011 end
1000210012
1000310013 # Recursively scan `nid`'s subtree for ClassVariable*WriteNode and
@@ -10095,6 +10105,58 @@
1009510105 end
1009610106 end
1009710107
10108+ # Register a cvar slot for `qname` ONLY when it isn't already
10109+ # known. Used by the method-body pass: a genuinely method-only
10110+ # cvar gets a static slot emitted, but a name already registered
10111+ # by a class-body write is left untouched so the method-body
10112+ # write can never widen its established type.
10113+ def register_cvar_if_absent(qname, t)
10114+ if find_cvar_idx(qname) >= 0
10115+ return
10116+ end
10117+ register_cvar(qname, t)
10118+ end
10119+
10120+ # Companion to collect_cvars_in that DOES descend into DefNode
10121+ # bodies. Runs after the main collect pass so every class-body
10122+ # cvar type is already settled. Registers method-body cvar write
10123+ # targets only-if-absent, with a type inferred from the rhs (or
10124+ # int for the compound forms reading nil), so a cvar first
10125+ # assigned inside a method still gets a static slot. Mirrors
10126+ # collect_cvars_in's class/module namespace switching.
10127+ def collect_method_cvars_in(nid, class_idx)
10128+ if nid < 0
10129+ return
10130+ end
10131+ t = @nd_type[nid]
10132+ if t == "ClassNode"
10133+ cp = @nd_constant_path[nid]
10134+ if cp >= 0
10135+ cname = const_ref_flat_name(cp)
10136+ ci = find_class_idx(cname)
10137+ if ci >= 0
10138+ collect_method_cvars_in(@nd_body[nid], ci)
10139+ end
10140+ end
10141+ return
10142+ end
10143+ if t == "ModuleNode"
10144+ collect_method_cvars_in(@nd_body[nid], -1)
10145+ return
10146+ end
10147+ if t == "ClassVariableWriteNode" || t == "ClassVariableOperatorWriteNode" || t == "ClassVariableOrWriteNode" || t == "ClassVariableAndWriteNode"
10148+ qname = cvar_qname(class_idx, @nd_name[nid])
10149+ register_cvar_if_absent(qname, infer_type(@nd_expression[nid]))
10150+ end
10151+ cs = []
10152+ push_child_ids(nid, cs)
10153+ k = 0
10154+ while k < cs.length
10155+ collect_method_cvars_in(cs[k], class_idx)
10156+ k = k + 1
10157+ end
10158+ end
10159+
1009810160 def node_contains_empty_array_ivar_write(nid)
1009910161 if nid < 0
1010010162 return 0