Commit 9ba5a420

9ba5a4205e9cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Constant-aliased class reopen + module include into a built-in

INT_ALIAS = 1.class; class INT_ALIAS; include IntWords; end; 42.wordy now works (and the user-class BOX_ALIAS = Box shape):

  • A class CONST whose name is a constant aliasing a class (CONST = SomeClass or CONST = <expr>.class) reopens that class. resolve_class_alias maps the alias to the underlying class and the ClassNode's AST name is rewritten so every later pass (registration, includes) agrees.
  • When a module is included into a built-in class (Integer/String/...), the transplanted method body is cloned and re-walked so its self re-attributes to the built-in's scalar type -- previously the shared body left self typed as the module, so self.to_s mis-dispatched to "".

Fixes const_aliased_class_reopen_include.

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

Details

Commit
9ba5a4205e9c
Parent
a2f2f0c75325

src/analyze.cmodifiedView file

@@ -2910,6 +2910,42 @@
29102910 return 1;
29112911 }
29122912
2913+/* The class name a TyKind denotes (for `<x>.class` alias resolution). */
2914+static const char *builtin_class_of_type(TyKind t) {
2915+ if (t == TY_INT || t == TY_BIGINT) return "Integer";
2916+ if (t == TY_FLOAT) return "Float";
2917+ if (t == TY_STRING) return "String";
2918+ if (t == TY_SYMBOL) return "Symbol";
2919+ return NULL;
2920+}
2921+
2922+/* If `cname` is a constant assigned a class value (`CONST = SomeClass` or
2923+ `CONST = <expr>.class`), return the underlying class name so `class CONST`
2924+ reopens that class. Returns NULL if `cname` is a plain new class name. */
2925+static const char *resolve_class_alias(Compiler *c, const char *cname) {
2926+ const NodeTable *nt = c->nt;
2927+ for (int id = 0; id < nt->count; id++) {
2928+ const char *ty = nt_type(nt, id);
2929+ if (!ty || strcmp(ty, "ConstantWriteNode")) continue;
2930+ const char *n = nt_str(nt, id, "name");
2931+ if (!n || strcmp(n, cname)) continue;
2932+ int v = nt_ref(nt, id, "value");
2933+ if (v < 0) return NULL;
2934+ const char *vty = nt_type(nt, v);
2935+ if (vty && (!strcmp(vty, "ConstantReadNode") || !strcmp(vty, "ConstantPathNode"))) {
2936+ const char *vn = nt_str(nt, v, "name");
2937+ if (vn && (comp_class_index(c, vn) >= 0 || is_builtin_class_name(vn))) return vn;
2938+ }
2939+ if (vty && !strcmp(vty, "CallNode") && nt_str(nt, v, "name") &&
2940+ !strcmp(nt_str(nt, v, "name"), "class")) {
2941+ int r = nt_ref(nt, v, "receiver");
2942+ if (r >= 0) return builtin_class_of_type(infer_type(c, r));
2943+ }
2944+ return NULL;
2945+ }
2946+ return NULL;
2947+}
2948+
29132949 static void walk_scope(Compiler *c, int id, int scope_idx, int class_id) {
29142950 if (id < 0 || id >= c->nt->count) return;
29152951 c->nscope[id] = scope_idx;
@@ -2953,6 +2989,16 @@
29532989 if (ty && (!strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode"))) {
29542990 int cp = nt_ref(c->nt, id, "constant_path");
29552991 const char *cname = cp >= 0 ? nt_str(c->nt, cp, "name") : NULL;
2992+ /* `class CONST` where CONST aliases an existing class reopens that class.
2993+ Rewrite the AST name so every later pass (registration, includes) agrees. */
2994+ if (cname && cp >= 0 && comp_class_index(c, cname) < 0) {
2995+ const char *real = resolve_class_alias(c, cname);
2996+ if (real) {
2997+ char buf[256]; snprintf(buf, sizeof buf, "%s", real); /* copy: set frees cname */
2998+ nt_set_str((NodeTable *)c->nt, cp, "name", buf);
2999+ cname = nt_str(c->nt, cp, "name");
3000+ }
3001+ }
29563002 if (cname && comp_class_index(c, cname) < 0) {
29573003 comp_class_new(c, cname, id);
29583004 child_class = c->nclasses - 1;
@@ -3842,6 +3888,7 @@
38423888 /* Process include calls in a single class body, creating scope copies for each
38433889 included module method. We copy (not mutate) so multiple classes can include
38443890 the same module independently. */
3891+static int g_inc_did_clone = 0;
38453892 static void process_include_body(Compiler *c, int ci, int body_node) {
38463893 const NodeTable *nt = c->nt;
38473894 int n = 0;
@@ -3871,9 +3918,25 @@
38713918 if (comp_method_in_class(c, ci, src->name) >= 0) continue;
38723919 /* Create a new scope sharing the same AST nodes but owned by ci. */
38733920 Scope *dst = comp_scope_new(c, src->name, src->def_node);
3921+ int dst_idx = c->nscopes - 1;
38743922 /* comp_scope_new may realloc c->scopes; re-derive src pointer. */
38753923 src = &c->scopes[ms];
3876- dst->body = src->body;
3924+ /* When the target is a built-in class, `self` has a different (scalar)
3925+ type than the module's object self, so clone the body and re-attribute
3926+ it to the target -- otherwise the shared SelfNode resolves to the
3927+ module and e.g. `self.to_s` mis-dispatches. */
3928+ if (is_builtin_class_name(c->classes[ci].name) && src->body >= 0) {
3929+ int nb = nt_clone_subtree((NodeTable *)nt, src->body);
3930+ if (nb >= 0) {
3931+ comp_grow_node_arrays(c);
3932+ src = &c->scopes[ms]; dst = &c->scopes[dst_idx];
3933+ dst->body = nb;
3934+ walk_scope(c, nb, dst_idx, ci);
3935+ g_inc_did_clone = 1;
3936+ } else dst->body = src->body;
3937+ } else {
3938+ dst->body = src->body;
3939+ }
38773940 dst->class_id = ci;
38783941 dst->is_cmethod = 0;
38793942 dst->reachable = src->reachable;
@@ -3924,6 +3987,7 @@
39243987 class so they are reachable via comp_method_in_chain. */
39253988 static void register_includes(Compiler *c) {
39263989 const NodeTable *nt = c->nt;
3990+ g_inc_did_clone = 0;
39273991 /* First pass: process def_node bodies (first class definition). */
39283992 for (int ci = 0; ci < c->nclasses; ci++) {
39293993 int body = nt_ref(nt, c->classes[ci].def_node, "body");
@@ -3942,6 +4006,7 @@
39424006 int body = nt_ref(nt, id, "body");
39434007 process_include_body(c, ci, body);
39444008 }
4009+ if (g_inc_did_clone) register_locals(c);
39454010 }
39464011
39474012 /* For each class, find `extend M` declarations and transplant M's instance