Commit b4f21c79

b4f21c798b7fMerged

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

Description

Resolve a constant class-alias as a method-call receiver

A = SomeClass; A.foo failed ("unsupported CallNode foo recv=ConstantRead") because class-method dispatch only resolves a receiver whose name is itself a class; a constant aliasing a class was not followed. M::N.foo (direct) and class A reopening already worked.

rewrite_const_alias_receivers runs once after class registration and rewrites a ConstantRead receiver A to its underlying class name (via the existing resolve_class_alias) when A = SomeClass/A = M::N, so every dispatch site resolves it exactly like the direct form. Mirrors the class CONST reopening rewrite in walk_scope.

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

Details

Commit
b4f21c798b7f
Parent
34f8cd5461a2

src/analyze.cmodifiedView file

@@ -395,6 +395,27 @@
395395 for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, node, i, &n); for (int k = 0; k < n; k++) mark_ie_subtree(c, ids[k], cls); }
396396 }
397397
398+/* `A = SomeClass` (a constant aliasing a class) then `A.foo`: rewrite the
399+ ConstantRead receiver's name to the underlying class so class-method dispatch
400+ resolves it exactly like the direct `SomeClass.foo`. Mirrors the `class CONST`
401+ reopening rewrite in walk_scope. Runs once after classes are registered. */
402+void rewrite_const_alias_receivers(Compiler *c) {
403+ const NodeTable *nt = c->nt;
404+ for (int id = 0; id < nt->count; id++) {
405+ const char *ty = nt_type(nt, id);
406+ if (!ty || strcmp(ty, "CallNode")) continue;
407+ int recv = nt_ref(nt, id, "receiver");
408+ if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ConstantReadNode")) continue;
409+ const char *rn = nt_str(nt, recv, "name");
410+ if (!rn || comp_class_index(c, rn) >= 0) continue; /* already a class name */
411+ const char *real = resolve_class_alias(c, rn);
412+ if (real && strcmp(real, rn)) {
413+ char buf[256]; snprintf(buf, sizeof buf, "%s", real); /* copy: set frees rn */
414+ nt_set_str((NodeTable *)nt, recv, "name", buf);
415+ }
416+ }
417+}
418+
398419 /* For a receiverless instance_eval/exec CallNode with a literal block inside
399420 an instance method, the receiver is self (CRuby resolves it to
400421 self.instance_exec). Return that class index, else -1. The literal-block
@@ -1123,6 +1144,7 @@
11231144 register_aliases(c);
11241145 register_undefs(c);
11251146 register_globals_consts(c);
1147+ rewrite_const_alias_receivers(c);
11261148 register_ffi_decls(c);
11271149
11281150 /* rescue variables (`rescue => e`) are typed as exception objects */