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

@@ -611,7 +611,19 @@
611611 s->body = nt_ref(c->nt, id, "body");
612612 s->class_id = class_id; /* instance method of the enclosing class */
613613 /* `def self.foo` / `def Klass.foo`: a class (singleton) method. */
614- if (nt_ref(c->nt, id, "receiver") >= 0) s->is_cmethod = 1;
614+ int defrecv = nt_ref(c->nt, id, "receiver");
615+ if (defrecv >= 0) {
616+ s->is_cmethod = 1;
617+ /* `def Klass.foo` with an explicit constant receiver (typically defined
618+ outside the class body, where the enclosing class_id is -1) attaches
619+ to that class's singleton chain rather than becoming a top-level free
620+ function. `def self.foo` keeps the enclosing class. */
621+ const char *rty = nt_type(c->nt, defrecv);
622+ if (rty && !strcmp(rty, "ConstantReadNode")) {
623+ int rci = comp_class_index(c, nt_str(c->nt, defrecv, "name"));
624+ if (rci >= 0) s->class_id = rci;
625+ }
626+ }
615627 collect_def_params(c, id, s);
616628 child = new_idx;
617629 }