Commit 831caa0b

831caa0bf282Merged

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

Description

codegen: coerce poly args at sp_range_include and string-index builtins under promote

sp_range_include and the string slice/index builtins (sp_str_sub_range[_r], sp_str_char_at_or_nil) take a raw mrb_int, but under --int-overflow=promote the argument or case scrutinee may be poly. Coerce with sp_poly_to_i: at the include?/case-as-value/when range-membership sites for range, and via emit_int_expr for the string position args. Only poly operands change (the non-poly path still emits the value directly), so default mode is unaffected. Unblocks range, case, truncate_module_method.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
831caa0bf282
Parent
0fd967b16ab7

src/codegen_call.cmodifiedView file

@@ -2201,18 +2201,18 @@
22012201 int excl = (int)(nt_int(c->nt, rn, "flags", 0) & 4) ? 1 : 0;
22022202 int lo = nt_ref(c->nt, rn, "left"), hi = nt_ref(c->nt, rn, "right");
22032203 buf_printf(b, "sp_str_sub_range_r(%s, ", r);
2204- if (lo >= 0) emit_expr(c, lo, b); else buf_puts(b, "0");
2204+ if (lo >= 0) emit_int_expr(c, lo, b); else buf_puts(b, "0");
22052205 buf_puts(b, ", ");
2206- if (hi >= 0) { emit_expr(c, hi, b); buf_printf(b, ", %d)", excl); }
2206+ if (hi >= 0) { emit_int_expr(c, hi, b); buf_printf(b, ", %d)", excl); }
22072207 else buf_printf(b, "(mrb_int)sp_str_length(%s), 0)", r); /* endless: to the end */
22082208 }
22092209 else if ((!strcmp(name, "[]") || !strcmp(name, "slice")) && argc == 2) {
22102210 /* s[start, len] */
22112211 buf_printf(b, "sp_str_sub_range(%s, ", r);
2212- emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")");
2212+ emit_int_expr(c, argv[0], b); buf_puts(b, ", "); emit_int_expr(c, argv[1], b); buf_puts(b, ")");
22132213 }
22142214 else if ((!strcmp(name, "[]") || !strcmp(name, "slice")) && argc == 1) {
2215- buf_printf(b, "sp_str_char_at_or_nil(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")");
2215+ buf_printf(b, "sp_str_char_at_or_nil(%s, ", r); emit_int_expr(c, argv[0], b); buf_puts(b, ")");
22162216 }
22172217 else if (!strcmp(name, "split") && argc == 0) buf_printf(b, "sp_str_split_ws(%s)", r);
22182218 else if (!strcmp(name, "split") && argc == 1) {
@@ -3067,11 +3067,16 @@
30673067 else {
30683068 /* sp_range_include takes mrb_int; a float arg (`(1..).include?(2.4)`)
30693069 needs an explicit cast, else clang -Werror flags the implicit
3070- float-literal->int conversion (gcc truncates silently). */
3071- int arg_is_float = comp_ntype(c, argv[0]) == TY_FLOAT;
3070+ float-literal->int conversion (gcc truncates silently). A poly arg
3071+ (e.g. under --int-overflow=promote) is coerced with sp_poly_to_i. */
3072+ TyKind at0 = comp_ntype(c, argv[0]);
3073+ int arg_is_float = at0 == TY_FLOAT;
3074+ int arg_is_poly = at0 == TY_POLY;
30723075 buf_printf(b, "sp_range_include(&_t%d, ", t);
30733076 if (arg_is_float) buf_puts(b, "(mrb_int)(");
3077+ if (arg_is_poly) buf_puts(b, "sp_poly_to_i(");
30743078 emit_expr(c, argv[0], b);
3079+ if (arg_is_poly) buf_puts(b, ")");
30753080 if (arg_is_float) buf_puts(b, ")");
30763081 buf_puts(b, ")");
30773082 }