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
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); }}/* `A = SomeClass` (a constant aliasing a class) then `A.foo`: rewrite the ConstantRead receiver's name to the underlying class so class-method dispatch resolves it exactly like the direct `SomeClass.foo`. Mirrors the `class CONST` reopening rewrite in walk_scope. Runs once after classes are registered. */void rewrite_const_alias_receivers(Compiler *c) { const NodeTable *nt = c->nt; for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue; int recv = nt_ref(nt, id, "receiver"); if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ConstantReadNode")) continue; const char *rn = nt_str(nt, recv, "name"); if (!rn || comp_class_index(c, rn) >= 0) continue; /* already a class name */ const char *real = resolve_class_alias(c, rn); if (real && strcmp(real, rn)) { char buf[256]; snprintf(buf, sizeof buf, "%s", real); /* copy: set frees rn */ nt_set_str((NodeTable *)nt, recv, "name", buf); } }}/* For a receiverless instance_eval/exec CallNode with a literal block inside an instance method, the receiver is self (CRuby resolves it to self.instance_exec). Return that class index, else -1. The literal-block register_aliases(c); register_undefs(c); register_globals_consts(c); rewrite_const_alias_receivers(c); register_ffi_decls(c); /* rescue variables (`rescue => e`) are typed as exception objects */test/const_alias_method_call.rbaddedView file
Loading diff…