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:

  1. Treat NIL as "unresolved" alongside VALUE in resolve_class_types, allowing subsequent type inference to override NIL-typed ivars

  2. 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

  3. 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

1+# Test: nil-initialized ivar type inference
2+# When @left/@right are initialized to nil and later assigned Node objects,
3+# the compiler should infer them as OBJECT(Node), not NIL.
4+
5+class Node
6+ attr_accessor :left, :right, :val
7+ def initialize(v)
8+ @val = v
9+ @left = nil
10+ @right = nil
11+ end
12+end
13+
14+class Tree
15+ attr_accessor :root
16+ def initialize
17+ @root = nil
18+ end
19+ def set_root(n)
20+ @root = n
21+ end
22+end
23+
24+def make_node(depth)
25+ n = Node.new(depth)
26+ if depth > 0
27+ child_l = make_node(depth - 1)
28+ child_r = make_node(depth - 1)
29+ n.left = child_l
30+ n.right = child_r
31+ end
32+ n
33+end
34+
35+tree = make_node(10)
36+puts tree.val
37+puts tree.left.val
38+puts tree.right.val
39+
40+t = Tree.new
41+t.set_root(tree)
42+puts t.root.val
43+
44+puts "done"