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:

  1. collect_cvars now starts from the program root with class_idx = -1 (the Toplevel namespace) and lets collect_cvars_in re-enter the class/module distinction itself. Module-scope, top-level, and class-body cvars all flow through the same registration path.

  2. collect_cvars_in learns three new dispatches:

    • ClassNode: switch the namespace to that class's index
    • ModuleNode: keep the Toplevel namespace, recurse
    • DefNode: skip. Method-body @@x = v writes 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.
  3. infer_type learns ClassVariableWriteNode -- returns the rhs's inferred type. Without this, a method whose last expression is @@x = v resolves 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

@@ -2539,6 +2539,16 @@
25392539 end
25402540 return "int"
25412541 end
2542+ if t == "ClassVariableWriteNode"
2543+ # `@@x = expr` as an expression returns the assigned value, so
2544+ # the static type is the rhs's type. Without this case the
2545+ # caller's `infer_type` falls through to the default (int) and
2546+ # surrounding code -- e.g. a method whose body's last
2547+ # expression is `@@x = v` -- gets `mrb_int` as the inferred
2548+ # return, producing a const char* / mrb_int mismatch when v
2549+ # is a string.
2550+ return infer_type(@nd_expression[nid])
2551+ end
25422552 if t == "ConstantReadNode"
25432553 if @nd_name[nid] == "ARGV"
25442554 return "argv"
@@ -7031,29 +7041,23 @@
70317041 collect_class_with_prefix(nid, "")
70327042 end
70337043
7034- # Walk every class body for class-var writes; register each
7035- # (class, name) pair so the static-declaration pass can emit
7036- # `static <type> cvar_<qname> = <default>;` ahead of the
7037- # functions that touch it.
7044+ # Walk class bodies, module bodies, and the top-level statement
7045+ # list for class-var writes; register each (class-or-Toplevel,
7046+ # name) pair so the static-declaration pass can emit
7047+ # `static <type> cvar_<qname> = <default>;` ahead of the functions
7048+ # that touch it.
7049+ #
7050+ # Module-scope and top-level `@@x = ...` writes belong to the
7051+ # `Toplevel` namespace -- spinel models cvars per-class, modules
7052+ # don't have a cls_id of their own, and tying them to "Toplevel"
7053+ # matches what the read/write codegen already emits when
7054+ # `@current_class_idx == -1`.
70387055 def collect_cvars
70397056 root = @root_id
70407057 if @nd_type[root] != "ProgramNode"
70417058 return
70427059 end
7043- stmts = get_body_stmts(root)
7044- stmts.each { |sid|
7045- if @nd_type[sid] == "ClassNode"
7046- cp = @nd_constant_path[sid]
7047- if cp >= 0
7048- cname = const_ref_flat_name(cp)
7049- ci = find_class_idx(cname)
7050- if ci >= 0
7051- body_id = @nd_body[sid]
7052- collect_cvars_in(body_id, ci)
7053- end
7054- end
7055- end
7056- }
7060+ collect_cvars_in(root, -1)
70577061 end
70587062
70597063 # Recursively scan `nid`'s subtree for ClassVariable*WriteNode and
@@ -7062,11 +7066,41 @@
70627066 # registered when their parent ClassVariableWriteNode is seen, OR
70637067 # lazily during compile_stmt if no plain Write precedes them in
70647068 # the same class.
7069+ #
7070+ # When the walk crosses into a nested ClassNode or ModuleNode the
7071+ # context flips: nested ClassNode switches to that class's
7072+ # @cls_names index, ModuleNode keeps the Toplevel namespace.
70657073 def collect_cvars_in(nid, class_idx)
70667074 if nid < 0
70677075 return
70687076 end
70697077 t = @nd_type[nid]
7078+ if t == "ClassNode"
7079+ cp = @nd_constant_path[nid]
7080+ if cp >= 0
7081+ cname = const_ref_flat_name(cp)
7082+ ci = find_class_idx(cname)
7083+ if ci >= 0
7084+ collect_cvars_in(@nd_body[nid], ci)
7085+ end
7086+ end
7087+ return
7088+ end
7089+ if t == "ModuleNode"
7090+ collect_cvars_in(@nd_body[nid], -1)
7091+ return
7092+ end
7093+ if t == "DefNode"
7094+ # Method bodies aren't walked at collect-time. A `@@x = v`
7095+ # inside a method writes during the call, but `v`'s type isn't
7096+ # yet resolved (LocalVariableReadNode falls back to "int" when
7097+ # the var-type table hasn't been populated), which would
7098+ # spuriously widen a class-body literal's `string`/`float`
7099+ # initialization to poly. The method-body write is registered
7100+ # defensively at compile_stmt time anyway, when v's call-site-
7101+ # resolved type is known.
7102+ return
7103+ end
70707104 if t == "ClassVariableWriteNode"
70717105 qname = cvar_qname(class_idx, @nd_name[nid])
70727106 val_t = infer_type(@nd_expression[nid])