Commit aff9b1fc
aff9b1fc536dMerged
Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): allocate Toplevel storage for module-scope @@cvars
@@var = ... inside class Foo; ...; end worked, but the same
construct inside a module ... end body (or at the top level)
referenced cvar_Toplevel_X without ever declaring it. C compile
failed with "use of undeclared identifier"; module-scope state
wasn't reachable.
Three coordinated changes:
-
collect_cvarsnow starts from the program root with class_idx = -1 (the Toplevel namespace) and letscollect_cvars_inre-enter the class/module distinction itself. Module-scope, top-level, and class-body cvars all flow through the same registration path. -
collect_cvars_inlearns three new dispatches:- ClassNode: switch the namespace to that class's index
- ModuleNode: keep the Toplevel namespace, recurse
- DefNode: skip. Method-body
@@x = vwrites are registered defensively at compile_stmt anyway, when v's call-site-resolved type is known. Walking method bodies at collect-time would spuriously widen literal-typed declarations to poly because LocalVariableReadNode falls back to "int" before var-types are populated.
-
infer_typelearns ClassVariableWriteNode -- returns the rhs's inferred type. Without this, a method whose last expression is@@x = vresolves to mrb_int, producing a const char* / mrb_int return-type mismatch when v is a string.
Regression test: test/module_cvars.rb covers module-scope @@var,
read/write through def self.x accessors, and a bare top-level
@@plain for completeness.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- aff9b1fc536d
- Parent
- d83163135c73
spinel_codegen.rbmodifiedView file
end return "int" end if t == "ClassVariableWriteNode" # `@@x = expr` as an expression returns the assigned value, so # the static type is the rhs's type. Without this case the # caller's `infer_type` falls through to the default (int) and # surrounding code -- e.g. a method whose body's last # expression is `@@x = v` -- gets `mrb_int` as the inferred # return, producing a const char* / mrb_int mismatch when v # is a string. return infer_type(@nd_expression[nid]) end if t == "ConstantReadNode" if @nd_name[nid] == "ARGV" return "argv" collect_class_with_prefix(nid, "") end # Walk every class body for class-var writes; register each # (class, name) pair so the static-declaration pass can emit # `static <type> cvar_<qname> = <default>;` ahead of the # functions that touch it. # Walk class bodies, module bodies, and the top-level statement # list for class-var writes; register each (class-or-Toplevel, # name) pair so the static-declaration pass can emit # `static <type> cvar_<qname> = <default>;` ahead of the functions # that touch it. # # Module-scope and top-level `@@x = ...` writes belong to the # `Toplevel` namespace -- spinel models cvars per-class, modules # don't have a cls_id of their own, and tying them to "Toplevel" # matches what the read/write codegen already emits when # `@current_class_idx == -1`. def collect_cvars root = @root_id if @nd_type[root] != "ProgramNode" return end stmts = get_body_stmts(root) stmts.each { |sid| if @nd_type[sid] == "ClassNode" cp = @nd_constant_path[sid] if cp >= 0 cname = const_ref_flat_name(cp) ci = find_class_idx(cname) if ci >= 0 body_id = @nd_body[sid] collect_cvars_in(body_id, ci) end end end } collect_cvars_in(root, -1) end # Recursively scan `nid`'s subtree for ClassVariable*WriteNode and # registered when their parent ClassVariableWriteNode is seen, OR # lazily during compile_stmt if no plain Write precedes them in # the same class. # # When the walk crosses into a nested ClassNode or ModuleNode the # context flips: nested ClassNode switches to that class's # @cls_names index, ModuleNode keeps the Toplevel namespace. def collect_cvars_in(nid, class_idx) if nid < 0 return end t = @nd_type[nid] if t == "ClassNode" cp = @nd_constant_path[nid] if cp >= 0 cname = const_ref_flat_name(cp) ci = find_class_idx(cname) if ci >= 0 collect_cvars_in(@nd_body[nid], ci) end end return end if t == "ModuleNode" collect_cvars_in(@nd_body[nid], -1) return end if t == "DefNode" # Method bodies aren't walked at collect-time. A `@@x = v` # inside a method writes during the call, but `v`'s type isn't # yet resolved (LocalVariableReadNode falls back to "int" when # the var-type table hasn't been populated), which would # spuriously widen a class-body literal's `string`/`float` # initialization to poly. The method-body write is registered # defensively at compile_stmt time anyway, when v's call-site- # resolved type is known. return end if t == "ClassVariableWriteNode" qname = cvar_qname(class_idx, @nd_name[nid]) val_t = infer_type(@nd_expression[nid])test/module_cvars.rbaddedView file
Loading diff…
test/module_cvars.rb.expectedaddedView file
Loading diff…