Commit 977a1562

977a1562b64cMerged

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

Description

fix: self.class.name/new in included/reopened methods

Two related fixes:

  1. X.class.name in a module method transplanted to a builtin-reopen class (Object, Array, Numeric): the analysis typed self.class as TY_CLASS (because self is TY_OBJECT in the original module scope), but codegen overrides self to TY_POLY in builtin-reopen context, making .class emit sp_poly_class_name (const char *) rather than sp_Class. The X.class.name wrapper then tried to assign a const char * to sp_Class _cl, causing a compile error. Fix: detect the builtin-reopen override in the .class.name handler and skip the sp_class_to_s wrapper.

  2. self.class.new(...) in a leaf instance method: analyze correctly resolves this to ty_object(enclosing_class) at line 539, but the earlier TY_CLASS + "new" fast-path at line 418 fires first and returns TY_POLY. The enclosing-class shortcut is never reached, so the method return type is TY_POLY and the generated C has sp_RbVal vs sp_Node * mismatch. Fix: exclude the self.class receiver pattern from the TY_POLY fast-path so the ty_object return at line 539 is reached.

Fixes open_class_object and self_class_new_construction tests.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
977a1562b64c
Parent
55cf625b0328

src/analyze.cmodifiedView file

@@ -414,11 +414,18 @@
414414 if (!strcmp(name, "parameters")) return TY_POLY_ARRAY;
415415 }
416416
417- /* TY_CLASS method dispatch -- .new on a dynamic class variable returns TY_POLY */
417+ /* TY_CLASS method dispatch -- .new on a dynamic class variable returns TY_POLY.
418+ Exception: self.class.new(...) resolves to the enclosing class statically. */
418419 if (recv >= 0 && rt == TY_CLASS && !strcmp(name, "new") &&
419420 nt_type(nt, recv) && strcmp(nt_type(nt, recv), "ConstantReadNode") != 0 &&
420- strcmp(nt_type(nt, recv), "ConstantPathNode") != 0)
421- return TY_POLY;
421+ strcmp(nt_type(nt, recv), "ConstantPathNode") != 0) {
422+ int _is_self_class = (nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
423+ nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "class") &&
424+ nt_ref(nt, recv, "receiver") >= 0 &&
425+ nt_type(nt, nt_ref(nt, recv, "receiver")) &&
426+ !strcmp(nt_type(nt, nt_ref(nt, recv, "receiver")), "SelfNode"));
427+ if (!_is_self_class) return TY_POLY;
428+ }
422429
423430 if (recv >= 0 && rt == TY_CLASS && strcmp(name, "new") != 0) {
424431 if (argc == 0 && (!strcmp(name, "to_s") || !strcmp(name, "name") || !strcmp(name, "inspect")))