Commit bca81be8

bca81be8fa6bMerged

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

Description

codegen: coerce a poly arg to const char* at string-membership primitives

A String | nil value narrowed to String via is_a?(String) keeps an sp_RbVal representation, so passing it to a string primitive that takes a const char * (String#include? / start_with? / end_with? -> sp_str_*) emitted the raw sp_RbVal and failed to compile.

Add emit_str_expr (poly -> sp_poly_to_s, else direct) and use it for the argument of those three predicates, mirroring emit_int_expr.

Fixes #1512.

Default test 997 pass / 0 fail and optcarrot checksum 59662 unchanged.

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

Details

Commit
bca81be8fa6b
Parent
7deef8c82284

src/codegen.cmodifiedView file

@@ -65,6 +65,17 @@
6565 else { buf_puts(b, "(mrb_float)("); emit_expr(c, node, b); buf_puts(b, ")"); }
6666 }
6767
68+/* Emit a node as a `const char *` string. A poly value (e.g. a `String | nil`
69+ local narrowed to String by `is_a?(String)`, which keeps an sp_RbVal
70+ representation) is unboxed via sp_poly_to_s; a string-typed value emits
71+ directly. Used at string-primitive arg boundaries (sp_str_include, ...). */
72+void emit_str_expr(Compiler *c, int node, Buf *b) {
73+ if (comp_ntype(c, node) == TY_POLY) {
74+ buf_puts(b, "sp_poly_to_s("); emit_expr(c, node, b); buf_puts(b, ")");
75+ }
76+ else emit_expr(c, node, b);
77+}
78+
6879 void emit_boxed(Compiler *c, int node, Buf *b) {
6980 TyKind t = comp_ntype(c, node);
7081 if (t == TY_POLY) { emit_expr(c, node, b); return; }