Commit 9d9a7088

9d9a70880703Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

codegen: coerce poly values to const char* at the join/split string boundary

verify-full of flutie 2.2.1 was reported to compile and then hang (#1351). On current master the hang no longer reproduces -- intervening inference fixes resolved it -- but two pre-existing compile-robustness bugs blocked the build, both a poly value reaching a builtin that wants a const char*:

  • Array#join: the separator is emitted raw, so a separator that widened to poly (a reader whose ivar unified to poly) passed an sp_RbVal where sp_Array_join / sp_PolyArray_join expects a const char. Coerce a poly separator with sp_poly_to_s.
  • String#split: a String-typed receiver that resolves to a poly nil -- an unresolvable chain like Rails.application.class.to_s in a method that is compiled but never called -- emits sp_box_nil(); route it through sp_poly_to_s (yields "") so the string-op block type-checks.

With both, flutie compiles, runs, and terminates with CRuby-identical output. Regression tests cover the join poly-separator and the split-on-unresolvable-chain shapes.

Fixes #1351

Details

Commit
9d9a70880703
Parent
7d670aecfa07

src/codegen_call.cmodifiedView file

@@ -7172,7 +7172,11 @@
71727172 }
71737173 if (!strcmp(name, "join") && argc <= 1) {
71747174 buf_printf(b, "sp_%sArray_join(", k); emit_expr(c, recv, b); buf_puts(b, ", ");
7175- if (argc == 1) emit_expr(c, argv[0], b); else buf_puts(b, "\"\"");
7175+ if (argc == 1 && comp_ntype(c, argv[0]) == TY_POLY) {
7176+ buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[0], b); buf_puts(b, ")");
7177+ }
7178+ else if (argc == 1) emit_expr(c, argv[0], b);
7179+ else buf_puts(b, "\"\"");
71767180 buf_puts(b, ")");
71777181 return;
71787182 }
@@ -7476,7 +7480,13 @@
74767480 }
74777481 if (!strcmp(name, "join") && argc <= 1) {
74787482 buf_puts(b, "sp_PolyArray_join("); emit_expr(c, recv, b); buf_puts(b, ", ");
7479- if (argc == 1) emit_expr(c, argv[0], b); else buf_puts(b, "\"\"");
7483+ /* the separator must be a const char*; a poly separator (e.g. a reader
7484+ whose ivar widened to poly) is converted with sp_poly_to_s. */
7485+ if (argc == 1 && comp_ntype(c, argv[0]) == TY_POLY) {
7486+ buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[0], b); buf_puts(b, ")");
7487+ }
7488+ else if (argc == 1) emit_expr(c, argv[0], b);
7489+ else buf_puts(b, "\"\"");
74807490 buf_puts(b, ")");
74817491 return;
74827492 }
@@ -7731,6 +7741,11 @@
77317741 Buf rs; memset(&rs, 0, sizeof rs);
77327742 emit_expr(c, recv, &rs);
77337743 const char *r = rs.p ? rs.p : "";
7744+ /* A String-typed receiver that resolved to a poly nil -- e.g. an
7745+ unresolvable chain like `Rails.application.class.to_s` in a method that
7746+ is compiled but never called -- emits sp_box_nil(); coerce it to a
7747+ const char* (yields "" at runtime) so the string ops below type-check. */
7748+ if (rt == TY_STRING && !strcmp(r, "sp_box_nil()")) r = "sp_poly_to_s(sp_box_nil())";
77347749 int handled = 1;
77357750
77367751 if (rt == TY_STRING) {