Commit 48f5ee66
48f5ee665084Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Attach def Klass.m (external singleton def) to its class
A singleton method defined outside the class body -- def Klass.foo, with
an explicit constant receiver -- was registered as a method scope whose
class_id was the enclosing class (-1 at top level), with is_cmethod
set but the receiver constant ignored. So it became a top-level free
function that no class-method dispatch could find: D.foo typed untyped
(poly) and failed downstream (puts D.go -> "unsupported puts argument
... recv=ConstantReadNode/ty38"), even without any yield. The in-body
def self.foo form worked because its enclosing class_id is already the
target.
Resolve a ConstantReadNode receiver to its class index at scope
registration and set the method's class_id to it, so it joins that
class's singleton chain exactly like def self.foo. Falls back to the
prior behavior (free function) when the constant is not a known class.
This was the separate pre-existing gap noted while fixing #1446
(self-method yield return type); both the plain and the yielding forms
of def Klass.m now type and run correctly.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 48f5ee665084
- Parent
- 533e8aa3a861
src/analyze_scope.cmodifiedView file
s->body = nt_ref(c->nt, id, "body"); s->class_id = class_id; /* instance method of the enclosing class */ /* `def self.foo` / `def Klass.foo`: a class (singleton) method. */ if (nt_ref(c->nt, id, "receiver") >= 0) s->is_cmethod = 1; int defrecv = nt_ref(c->nt, id, "receiver"); if (defrecv >= 0) { s->is_cmethod = 1; /* `def Klass.foo` with an explicit constant receiver (typically defined outside the class body, where the enclosing class_id is -1) attaches to that class's singleton chain rather than becoming a top-level free function. `def self.foo` keeps the enclosing class. */ const char *rty = nt_type(c->nt, defrecv); if (rty && !strcmp(rty, "ConstantReadNode")) { int rci = comp_class_index(c, nt_str(c->nt, defrecv, "name")); if (rci >= 0) s->class_id = rci; } } collect_def_params(c, id, s); child = new_idx; }test/external_singleton_def.rbaddedView file
Loading diff…
test/external_singleton_def.rb.expectedaddedView file
Loading diff…