Commit 15a33057

15a3305780d0Merged

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

Description

Lower String += to a concat call for nullable-string operands too

local += rhs where the local is a string only lowered to sp_str_concat when both the local and the rhs typed exactly "string"; a string? (nullable-string -- e.g. a method returning nil-or-String, or Digest::MD5.digest) operand fell through to a raw C vref += rhs, i.e. const char * + const char * -- "invalid operands to binary +", a failed cc. A const-char*-typed string is still a Ruby String regardless of nullability.

Broaden the concat condition to base_type(...) == "string" on both sides so string / string? combinations all lower to sp_str_concat (which already treats a NULL/nil operand as empty). Verified with an equivalent nil | String-returning method that produces the identical const char * + const char * error the issue reports via aes-everywhere; the fix lowers it to sp_str_concat and the result matches CRuby. Gate green (test/bench/optcarrot 59662/self-host fixpoint).

Fixes #1341.

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

Details

Commit
15a3305780d0
Parent
c7c2884b7f53

spinel_codegen.rbmodifiedView file

@@ -37082,7 +37082,12 @@
3708237082 return
3708337083 end
3708437084 if op == "+"
37085- if vt == "string" && infer_type(@nd_expression[nid]) == "string"
37085+ if base_type(vt) == "string" && base_type(infer_type(@nd_expression[nid])) == "string"
37086+ # A `const char *`-typed string is still a Ruby String, including the
37087+ # nullable `string?` slot a method like `mk -> nil | "x"` produces.
37088+ # `vt OP= rhs` on two such operands must lower to a string concat, not
37089+ # raw C pointer `+` (invalid operands to binary +). sp_str_concat
37090+ # treats a NULL (nil) operand as empty. Issue #1341.
3708637091 emit(" " + vref + " = sp_str_concat(" + vref + ", " + val + ");")
3708737092 elsif vt == "complex" && infer_type(@nd_expression[nid]) == "complex"
3708837093 # `+=` on a struct doesn't compile in C; desugar to