Commit 153e9f2b
153e9f2b6fdbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Fix TY_CLASS dispatch breaking user class .new, ivar op-assign type inference
- TY_CLASS dispatch block incorrectly intercepted
.newreturning TY_CLASS instead of TY_OBJECT(class); exclude.newso it falls through to the AST-based handler that knows which class the constant refers to - InstanceVariableOperatorWriteNode (@b += rhs): use the operator method's return type when the ivar slot holds a user object, not the RHS literal type (which was widening TY_OBJECT(Box) to TY_POLY via ty_unify)
- LocalVariableOperatorWriteNode: same fix in infer_write_types for local object-typed slots
Fixes class_in_collection, class_value_builtin_and_nested, op_assign_user_operator, class_hierarchy, class_hierarchy_module, class_lt_builtin_parent; reduces ERR count from 108 to 103.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 153e9f2b6fdb
- Parent
- d25174a70ce7
src/analyze.cmodifiedView file
@@ -4,6 +4,22 @@
44
#include <stdlib.h>55
#include <string.h>66
7+
static int is_builtin_class_name(const char *n) {8+
if (!n) return 0;9+
static const char *const CL[] = {10+
"Integer","Float","String","Symbol","Array","Hash","Range","Time",11+
"Module","Class","NilClass","TrueClass","FalseClass","Numeric",12+
"Comparable","Enumerable","Object","BasicObject","Proc","Kernel",13+
"IO","File","Exception","StandardError","RuntimeError","TypeError",14+
"ArgumentError","NameError","NoMethodError","StopIteration","Math",15+
"Complex","Rational","Encoding","Method","UnboundMethod","Fiber",16+
"Thread","Mutex","GC","ObjectSpace","Signal","Process","Regexp",17+
"MatchData","StringIO","StringScanner",NULL18+
};19+
for (int i = 0; CL[i]; i++) if (!strcmp(n, CL[i])) return 1;20+
return 0;21+
}22+
723
static int is_builtin_exception_name(const char *n) {824
if (!n) return 0;925
static const char *const EXC[] = {@@ -398,6 +414,19 @@
398414
if (!strcmp(name, "parameters")) return TY_POLY_ARRAY;399415
}400416
417+
/* TY_CLASS method dispatch -- do NOT intercept .new here; fall through to418+
the AST-based handler that knows which class the constant refers to. */419+
if (recv >= 0 && rt == TY_CLASS && strcmp(name, "new") != 0) {420+
if (argc == 0 && (!strcmp(name, "to_s") || !strcmp(name, "name") || !strcmp(name, "inspect")))421+
return TY_STRING;422+
if (argc == 0 && !strcmp(name, "nil?")) return TY_BOOL;423+
if (argc == 0 && !strcmp(name, "class")) return TY_STRING;424+
if (argc == 0 && !strcmp(name, "superclass")) return TY_CLASS;425+
if (argc == 1 && (!strcmp(name, "==") || !strcmp(name, "eql?") || !strcmp(name, "!="))) return TY_BOOL;426+
if (argc == 1 && (!strcmp(name, "<") || !strcmp(name, ">") || !strcmp(name, "<=") || !strcmp(name, ">="))) return TY_BOOL;427+
if (argc == 0 && !strcmp(name, "ancestors")) return TY_POLY_ARRAY;428+
}429+
401430
/* __method__ / __callee__ -> the enclosing method's name (a symbol) */402431
if (recv < 0 && argc == 0 &&403432
(!strcmp(name, "__method__") || !strcmp(name, "__callee__")))@@ -476,13 +505,17 @@
476505
}477506
}478507
479-
/* SomeClass.name / .to_s / .inspect / .superclass -> the class-name string */508+
/* SomeClass.name / .to_s / .inspect -> class name string */480509
if (recv >= 0 && argc == 0 &&481-
(!strcmp(name, "name") || !strcmp(name, "to_s") || !strcmp(name, "inspect") ||482-
!strcmp(name, "superclass")) &&510+
(!strcmp(name, "name") || !strcmp(name, "to_s") || !strcmp(name, "inspect")) &&483511
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") &&484512
nt_str(nt, recv, "name") && comp_class_index(c, nt_str(nt, recv, "name")) >= 0)485513
return TY_STRING;514+
/* SomeClass.superclass -> sp_Class value for the parent class */515+
if (recv >= 0 && argc == 0 && !strcmp(name, "superclass") &&516+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode") &&517+
nt_str(nt, recv, "name") && comp_class_index(c, nt_str(nt, recv, "name")) >= 0)518+
return TY_CLASS;486519
487520
/* SomeClass.ancestors -> PolyArray of class objects */488521
if (recv >= 0 && argc == 0 && !strcmp(name, "ancestors") &&@@ -1970,6 +2003,7 @@
19702003
!strcmp(nm, "RUBY_REVISION") || !strcmp(nm, "RUBY_COPYRIGHT"))) return TY_STRING;19712004
if (nm && !strcmp(nm, "ARGV")) return TY_STR_ARRAY;19722005
if (nm && comp_class_index(c, nm) >= 0) return TY_CLASS;2006+
if (nm && is_builtin_class_name(nm)) return TY_CLASS;19732007
return TY_UNKNOWN;19742008
}19752009
if (!strcmp(ty, "DefinedNode")) return TY_STRING; /* a label string, or nil (NULL) */@@ -2002,6 +2036,7 @@
20022036
if (nm && (!strcmp(nm, "MAX") || !strcmp(nm, "MIN"))) return TY_UNKNOWN; /* raises NameError */20032037
}20042038
if (nm && comp_class_index(c, nm) >= 0) return TY_CLASS;2039+
if (nm && is_builtin_class_name(nm)) return TY_CLASS;20052040
return TY_UNKNOWN;20062041
}20072042
if (!strcmp(ty, "SelfNode")) {@@ -3302,6 +3337,17 @@
33023337
int old_ni = ci->nivars;33033338
int iv = comp_ivar_intern(ci, nm);33043339
if (ci->nivars != old_ni) changed = 1; /* new ivar registered, need another pass */3340+
/* For operator-write (@b += rhs), vt is the RHS type, not the result type.3341+
When the slot holds a user object, the result is the method's return type. */3342+
if (!strcmp(ty, "InstanceVariableOperatorWriteNode") && ty_is_object(ci->ivar_types[iv])) {3343+
const char *op2 = nt_str(nt, id, "binary_operator");3344+
int cid2 = ty_object_class(ci->ivar_types[iv]);3345+
int mi2 = op2 ? comp_method_in_chain(c, cid2, op2, NULL) : -1;3346+
if (mi2 >= 0 && c->scopes[mi2].ret != TY_UNKNOWN)3347+
vt = c->scopes[mi2].ret;3348+
else3349+
vt = ci->ivar_types[iv]; /* keep existing type, don't widen */3350+
}33053351
TyKind merged = ty_unify(ci->ivar_types[iv], vt);33063352
if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }33073353
}@@ -4512,8 +4558,15 @@
45124558
int iidx = nm ? comp_ivar_index(&c->classes[s2->class_id], nm) : -1;45134559
slot_t = iidx >= 0 ? c->classes[s2->class_id].ivar_types[iidx] : TY_UNKNOWN;45144560
}4515-
if (!ty_is_object(slot_t)) continue;4516-
int cid2 = ty_object_class(slot_t);4561+
/* For TY_POLY slots, scan all user classes for a matching operator method. */4562+
int cid2 = -1;4563+
if (ty_is_object(slot_t)) cid2 = ty_object_class(slot_t);4564+
else if (slot_t == TY_POLY) {4565+
for (int _sc = 0; _sc < c->nclasses; _sc++) {4566+
if (comp_method_in_chain(c, _sc, op, NULL) >= 0) { cid2 = _sc; break; }4567+
}4568+
}4569+
if (cid2 < 0) continue;45174570
int mi2 = comp_method_in_chain(c, cid2, op, NULL);45184571
if (mi2 < 0) continue;45194572
Scope *ms2 = &c->scopes[mi2];src/codegen.cmodifiedView file
Loading diff…