Commit 76fc0597

76fc0597fe76Merged

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

Description

Handle class-name when-arms for all scrutinee types in case/when

Add emit_poly_class_when helper that emits tag/cls_id checks for class-name when-arms (Integer, String, Float, Symbol, Range, etc.) against a TY_POLY scrutinee. For concrete-typed scrutinees, resolve via ty_matches_class (static 0 or 1). Place the ConstantReadNode check before type-specific branches in both emit_case and emit_case_expr to avoid falling through to emit_expr, which would crash on a bare class name.

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

Details

Commit
76fc0597fe76
Parent
62a3227c72a4

src/codegen.cmodifiedView file

@@ -9105,6 +9105,8 @@
91059105 buf_printf(b, "(%s.tag == SP_TAG_BOOL && !%s.v.b)", tmp, tmp);
91069106 else if (!strcmp(cn, "Numeric"))
91079107 buf_printf(b, "(%s.tag == SP_TAG_INT || %s.tag == SP_TAG_FLT)", tmp, tmp);
9108+ else if (!strcmp(cn, "Range"))
9109+ buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id == SP_BUILTIN_RANGE)", tmp, tmp);
91089110 else if (!strcmp(cn, "Array"))
91099111 buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id <= -1 && %s.cls_id >= -12)", tmp, tmp, tmp);
91109112 else if (!strcmp(cn, "Hash"))
@@ -9203,6 +9205,19 @@
92039205 else buf_puts(b, "0");
92049206 }
92059207 else {
9208+ /* when ClassName: Module#=== resolves via is_a? semantics */
9209+ const char *cty2 = nt_type(nt, conds[j]);
9210+ const char *cn2 = cty2 && !strcmp(cty2, "ConstantReadNode") ? nt_str(nt, conds[j], "name") : NULL;
9211+ if (cn2 && pt == TY_POLY) {
9212+ char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9213+ if (!emit_poly_class_when(c, conds[j], tmp, b))
9214+ buf_puts(b, "0");
9215+ }
9216+ else if (cn2) {
9217+ int yes = ty_matches_class(pt, cn2, 0);
9218+ buf_printf(b, "%d", yes > 0 ? 1 : 0);
9219+ }
9220+ else {
92069221 int reidx = re_lit_index(c, conds[j]);
92079222 if (reidx >= 0 && pt == TY_STRING) {
92089223 buf_printf(b, "sp_re_match_p(sp_re_pat_%d, _t%d)", reidx, t);
@@ -9221,13 +9236,12 @@
92219236 buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")");
92229237 }
92239238 else if (pt == TY_POLY) {
9224- char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9225- if (!emit_poly_class_when(c, conds[j], tmp, b))
9226- { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
9239+ buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")");
92279240 }
92289241 else {
92299242 buf_printf(b, "(_t%d == ", t); emit_expr(c, conds[j], b); buf_puts(b, ")");
92309243 }
9244+ } /* close non-ConstantReadNode else */
92319245 } /* close else { int reidx... } */
92329246 }
92339247 }
@@ -9291,6 +9305,15 @@
92919305 for (int j = 0; j < wc; j++) {
92929306 if (j) buf_puts(b, " || ");
92939307 if (pred >= 0) {
9308+ /* when ClassName: Module#=== resolves via is_a? semantics */
9309+ const char *cty2 = nt_type(nt, conds[j]);
9310+ const char *cn2 = cty2 && !strcmp(cty2, "ConstantReadNode") ? nt_str(nt, conds[j], "name") : NULL;
9311+ if (cn2 && pt == TY_POLY) {
9312+ char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9313+ if (!emit_poly_class_when(c, conds[j], tmp, b)) buf_puts(b, "0");
9314+ }
9315+ else if (cn2) { int yes = ty_matches_class(pt, cn2, 0); buf_printf(b, "%d", yes > 0 ? 1 : 0); }
9316+ else {
92949317 int reidx = re_lit_index(c, conds[j]);
92959318 if (reidx >= 0 && pt == TY_STRING) { buf_printf(b, "sp_re_match_p(sp_re_pat_%d, _t%d)", reidx, t); }
92969319 else if (comp_ntype(c, conds[j]) == TY_RANGE && pt != TY_STRING) {
@@ -9303,12 +9326,9 @@
93039326 buf_puts(b, "0");
93049327 }
93059328 else if (pt == TY_STRING) { buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
9306- else if (pt == TY_POLY) {
9307- char tmp[32]; snprintf(tmp, sizeof tmp, "_t%d", t);
9308- if (!emit_poly_class_when(c, conds[j], tmp, b))
9309- { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
9310- }
9329+ else if (pt == TY_POLY) { buf_printf(b, "sp_poly_eq(_t%d, ", t); emit_boxed(c, conds[j], b); buf_puts(b, ")"); }
93119330 else { buf_printf(b, "(_t%d == ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
9331+ } /* close non-ConstantReadNode else */
93129332 }
93139333 else { buf_puts(b, "("); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
93149334 }