Commit 9ddfede0
9ddfede0ad39Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 4 files · +1 added · ~3 modified
Description
Fix nil-initialized ivar type inference and object pointer return types
When a class initializes ivars to nil (@left = nil), the type was set to NIL and never updated from later assignments. This fixes three aspects:
-
Treat NIL as "unresolved" alongside VALUE in resolve_class_types, allowing subsequent type inference to override NIL-typed ivars
-
Scan all method bodies (class methods and free functions) for setter calls (recv.ivar = expr) and @ivar = expr assignments, updating NIL/VALUE-typed ivars from the assigned expression's type
-
Use as_ptr=true for vt_ctype in function/method return types and parameter declarations so non-value-type objects correctly emit as pointers (sp_Node *) rather than values (sp_Node)
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9ddfede0ad39
- Parent
- 6e49fb9acb0b
examples/bm_nil_ivar.rbaddedView file
# Test: nil-initialized ivar type inference# When @left/@right are initialized to nil and later assigned Node objects,# the compiler should infer them as OBJECT(Node), not NIL.class Node attr_accessor :left, :right, :val def initialize(v) @val = v @left = nil @right = nil endendclass Tree attr_accessor :root def initialize @root = nil end def set_root(n) @root = n endenddef make_node(depth) n = Node.new(depth) if depth > 0 child_l = make_node(depth - 1) child_r = make_node(depth - 1) n.left = child_l n.right = child_r end nendtree = make_node(10)puts tree.valputs tree.left.valputs tree.right.valt = Tree.newt.set_root(tree)puts t.root.valputs "done"src/codegen.cmodifiedView file
Loading diff…
src/emit.cmodifiedView file
Loading diff…
src/type.cmodifiedView file
Loading diff…