Commit 48ec92a3

48ec92a3db83Merged

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

Description

Resolve self.class.new(args) to the static enclosing constructor

self.class.new(args) from an instance method emitted "cannot resolve call to 'new' on class" and invalid C: the receiver is an sp_Class value, and the recv_type=="class" new arm only handled the no-arg dynamic case (sp_class_new_dispatch); with args it fell through. This is the idiomatic functional-update pattern (AST::Node#updated and similar).

When self.class.new(args) appears in an instance method of a class with no user subclass, the runtime self is exactly that class, so construct it statically: a new self_class_enclosing_ctor_cname helper (mirrored in analyze + codegen) recognizes the self.class.new shape and returns the enclosing class name, gated on class_has_user_subclass being false. Analyze types the node obj_<C>; codegen routes it through the existing compile_constructor_expr static path (sp_<C>_new), gated on analyze having annotated the node obj_<C> so the inferred and emitted types always agree. Class-method bodies are excluded (there self is the class, so self.class is Class).

A class WITH subclasses keeps falling through (argful construction over a dynamic class value is unsupported) rather than wrongly constructing the base class.

Fixes #1039

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

Details

Commit
48ec92a3db83
Parent
6f663077151f

spinel_analyze.rbmodifiedView file

@@ -1846,6 +1846,53 @@
18461846 0
18471847 end
18481848
1849+ # 1 if any registered class is a proper descendant of class index ci.
1850+ def class_has_user_subclass(ci)
1851+ k = 0
1852+ while k < @cls_names.length
1853+ if cls_is_descendant(k, ci) == 1
1854+ return 1
1855+ end
1856+ k = k + 1
1857+ end
1858+ 0
1859+ end
1860+
1861+ # For `self.class.new(...)` written inside an instance method, return
1862+ # the enclosing class name when that class has no user subclass (so
1863+ # the runtime `self` is exactly that class and static construction is
1864+ # correct); "" otherwise. This lets the idiomatic functional-update
1865+ # `self.class.new(args)` lower to the static constructor instead of
1866+ # failing on a dynamic class value. Class-method bodies are excluded:
1867+ # there `self` is the class, so `self.class` is Class, not the
1868+ # enclosing class. Analyze marks class-method bodies with a `_cls_`
1869+ # infix in @current_method_name (@current_method_has_self is unused
1870+ # on this side).
1871+ def self_class_enclosing_ctor_cname(nid)
1872+ if @nd_name[nid] != "new"
1873+ return ""
1874+ end
1875+ recv = @nd_receiver[nid]
1876+ if recv < 0 || @nd_type[recv] != "CallNode" || @nd_name[recv] != "class"
1877+ return ""
1878+ end
1879+ inner = @nd_receiver[recv]
1880+ if inner < 0 || @nd_type[inner] != "SelfNode"
1881+ return ""
1882+ end
1883+ if @current_class_idx < 0
1884+ return ""
1885+ end
1886+ cm_marker = @current_method_name.index("_cls_")
1887+ if cm_marker != nil && cm_marker >= 0
1888+ return ""
1889+ end
1890+ if class_has_user_subclass(@current_class_idx) == 1
1891+ return ""
1892+ end
1893+ @cls_names[@current_class_idx]
1894+ end
1895+
18491896 def cls_cmethod_owner(ci, mname)
18501897 if ci < 0
18511898 return -1
@@ -4358,6 +4405,12 @@
43584405 # works unchanged.
43594406 if mname == "new"
43604407 if recv >= 0 && infer_type(recv) == "class"
4408+ # `self.class.new(args)` in an instance method of a leaf (no
4409+ # subclass) class -> construct the enclosing class statically.
4410+ cn_sc = self_class_enclosing_ctor_cname(nid)
4411+ if cn_sc != ""
4412+ return "obj_" + cn_sc
4413+ end
43614414 rty_n = @nd_type[recv]
43624415 if rty_n != "ConstantReadNode" && rty_n != "ConstantPathNode"
43634416 return "poly"