Commit c54aa12f
c54aa12fd0f6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix case/when with qualified constants and unresolved ConstantPathNode
- emit_poly_class_when: accept ConstantPathNode in addition to
ConstantReadNode so
when Mod::Klasson poly predicates works - emit_case / emit_case_expr: accept ConstantPathNode in class-matching branch; also handle typed object predicates (user classes) by resolving the class id and emitting a compile-time 0/1 comparison via comp_class_index + is_descendant (was returning -1 from ty_matches_class which treats object types as unknown)
- ConstantPathNode expression: replace hard unsupported() exit with a runtime NameError raise (uninitialized constant Mod::NAME) so unresolved qualified constants are rescuable instead of crashing the compiler
Fixes case_when_qualified_class and const_path_unresolved_require tests.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- c54aa12fd0f6
- Parent
- 463acf8b5d9a
src/codegen.cmodifiedView file
@@ -11774,7 +11774,15 @@
1177411774
buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant Integer::%s\"), 0)", nm);1177511775
return;1177611776
}11777-
unsupported(c, id, "constant path read");11777+
/* unresolved qualified constant: raise NameError at runtime */11778+
{11779+
char fullname[512];11780+
if (par_nmc && nm) snprintf(fullname, sizeof fullname, "%s::%s", par_nmc, nm);11781+
else if (nm) snprintf(fullname, sizeof fullname, "%s", nm);11782+
else snprintf(fullname, sizeof fullname, "?");11783+
buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant %s\"), (mrb_int)0)", fullname);11784+
}11785+
return;1177811786
}1177911787
if (!strcmp(ty, "DefinedNode")) {1178011788
/* compile-time defined? -> a label string, or nil (NULL) when undefined */@@ -12871,7 +12879,7 @@
1287112879
static int emit_poly_class_when(Compiler *c, int cond_id, const char *tmp, Buf *b) {1287212880
const NodeTable *nt = c->nt;1287312881
const char *cty = nt_type(nt, cond_id);12874-
if (!cty || strcmp(cty, "ConstantReadNode")) return 0;12882+
if (!cty || (strcmp(cty, "ConstantReadNode") && strcmp(cty, "ConstantPathNode"))) return 0;1287512883
const char *cn = nt_str(nt, cond_id, "name");1287612884
if (!cn) return 0;1287712885
if (!strcmp(cn, "Integer") || !strcmp(cn, "Fixnum"))@@ -13190,14 +13198,21 @@
1319013198
else buf_puts(b, "0");1319113199
}1319213200
else {13193-
/* when ClassName: Module#=== resolves via is_a? semantics */13201+
/* when ClassName / when Mod::Klass: Module#=== via is_a? semantics */1319413202
const char *cty2 = nt_type(nt, conds[j]);13195-
const char *cn2 = cty2 && !strcmp(cty2, "ConstantReadNode") ? nt_str(nt, conds[j], "name") : NULL;13203+
const char *cn2 = cty2 && (!strcmp(cty2, "ConstantReadNode") || !strcmp(cty2, "ConstantPathNode"))13204+
? nt_str(nt, conds[j], "name") : NULL;1319613205
if (cn2 && pt == TY_POLY) {1319713206
char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);1319813207
if (!emit_poly_class_when(c, conds[j], tmp, b))1319913208
buf_puts(b, "0");1320013209
}13210+
else if (cn2 && ty_is_object(pt)) {13211+
int cid = ty_object_class(pt);13212+
int tcid = comp_class_index(c, cn2);13213+
int yes = (tcid >= 0) && (cid == tcid || is_descendant(c, cid, tcid));13214+
buf_printf(b, "%d", yes ? 1 : 0);13215+
}1320113216
else if (cn2) {1320213217
int yes = ty_matches_class(pt, cn2, 0);1320313218
buf_printf(b, "%d", yes > 0 ? 1 : 0);@@ -13290,13 +13305,19 @@
1329013305
for (int j = 0; j < wc; j++) {1329113306
if (j) buf_puts(b, " || ");1329213307
if (pred >= 0) {13293-
/* when ClassName: Module#=== resolves via is_a? semantics */13308+
/* when ClassName / Mod::Klass: Module#=== via is_a? semantics */1329413309
const char *cty2 = nt_type(nt, conds[j]);13295-
const char *cn2 = cty2 && !strcmp(cty2, "ConstantReadNode") ? nt_str(nt, conds[j], "name") : NULL;13310+
const char *cn2 = cty2 && (!strcmp(cty2, "ConstantReadNode") || !strcmp(cty2, "ConstantPathNode"))13311+
? nt_str(nt, conds[j], "name") : NULL;1329613312
if (cn2 && pt == TY_POLY) {1329713313
char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);1329813314
if (!emit_poly_class_when(c, conds[j], tmp, b)) buf_puts(b, "0");1329913315
}13316+
else if (cn2 && ty_is_object(pt)) {13317+
int cid = ty_object_class(pt); int tcid = comp_class_index(c, cn2);13318+
int yes = (tcid >= 0) && (cid == tcid || is_descendant(c, cid, tcid));13319+
buf_printf(b, "%d", yes ? 1 : 0);13320+
}1330013321
else if (cn2) { int yes = ty_matches_class(pt, cn2, 0); buf_printf(b, "%d", yes > 0 ? 1 : 0); }1330113322
else {1330213323
int reidx = re_lit_index(c, conds[j]);@@ -13307,7 +13328,6 @@
1330713328
buf_printf(b, "; sp_range_include(&_t%d, _t%d); })", tr, t);1330813329
}1330913330
else if (eq_family(pt) && eq_family(comp_ntype(c, conds[j])) && eq_family(pt) != eq_family(comp_ntype(c, conds[j]))) {13310-
/* a when value of a different comparable family never matches */1331113331
buf_puts(b, "0");1331213332
}1331313333
else if (pt == TY_STRING) { buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }