Commit 2ecfa55b
2ecfa55b05baMerged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): coerce poly args in chained concat and print to const char*
A poly local that survives an if/else conditional assignment landed
as sp_RbVal in two codegen sites that downstream expected
const char *. Both produced cc-rejected C:
-
puts "[ " + text + " ]"-- chained-concat collapse routes throughcollect_concat_parts->sp_str_concat3(a, b, c)which takes threeconst char *s. The collector handled string/int/float arg shapes but fell through with the raw expression for poly. The matching 2-argsp_str_concatsite upstream already coerced poly viasp_poly_to_s; mirror that here, and add the same coercion for symbol / mutable_str / bigint so any chained shape that types its leaves through those slots also concats cleanly. -
print textwheretextis poly --compile_print's fallback emittedprintf("%lld", (long long)<val>), which cc rejects because sp_RbVal is a struct (no scalar cast). Route poly throughsp_poly_to_sto mirror the puts-sidesp_poly_putsdispatch (without the trailing newline thatprintshouldn't emit).
The canonical shape that surfaces this is a str_poly_hash lookup
fused with a nil-guard fallback: text = h["k"].nil? ? "" : h["k"]
where the else branch hands back the hash's poly value union. The
union stays sp_RbVal because the analyze pass widens to poly when
the if and else branches produce different static types -- "" is
string but the else branch is poly. Re-narrowing across
conditional assignments would be the more principled fix; this
patch closes the cc-error gap that blocked any source touching
this shape.
The sp_poly_to_s route is more permissive than CRuby's
String#+ (which raises TypeError on non-String operands).
Aligning to the strict-raise behaviour would need a new
sp_poly_as_string_or_raise helper; defer that to a separate
PR once the narrowing-pass fix lands and the coercion path
becomes a fallback rather than the common path.
Test test/concat_print_poly_unbox.rb covers concat3 / 4-part
chain / print with a str_poly_hash lookup driving the poly
local. Bootstrap fixpoint green; 619 / 619 pass.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 2ecfa55b05ba
- Parent
- c360ff731f28
spinel_codegen.rbmodifiedView file
parts.push("sp_bigint_to_s((sp_Bigint *)" + compile_expr(aargs[0]) + ")") elsif at == "float" parts.push("sp_float_to_s(" + compile_expr(aargs[0]) + ")") elsif at == "poly" # `"a" + poly_local + "b"` -- the 2-arg sp_str_concat path at # the single-`+` site already coerces via sp_poly_to_s; mirror # that here for chained concat collapsing into sp_str_concat3 / # sp_str_concat_arr. Without this, the chained form silently # passed an sp_RbVal into a `const char *` parameter and cc # rejected the generated code. Common shape: a local typed # through `if x.nil?; ""; else; x; end` when x came from a # poly-valued hash lookup -- the union stays sp_RbVal but its # string-side payload is always a valid c-string at runtime. parts.push("sp_poly_to_s(" + compile_expr(aargs[0]) + ")") elsif at == "symbol" parts.push("sp_sym_to_s(" + compile_expr(aargs[0]) + ")") elsif at == "mutable_str" parts.push("(" + compile_expr(aargs[0]) + ")->data") elsif at == "bigint" parts.push("sp_bigint_to_s(" + compile_expr(aargs[0]) + ")") else parts.push(compile_expr(aargs[0])) end end if at == "int" emit(" printf(\"%lld\", (long long)" + val + ");") elsif at == "mutable_str" emit(" fputs(" + val + "->data, stdout);") elsif at == "string" || at == "string?" emit(" if (" + val + ") fputs(" + val + ", stdout);") elsif at == "float" emit(" fputs(sp_float_to_s(" + val + "), stdout);") elsif at == "bool" emit(" fputs(" + val + " ? \"true\" : \"false\", stdout);") elsif at == "poly" # `print poly_local` -- route through sp_poly_to_s rather than # falling through to the `printf("%lld", (long long)val)` path, # which is a C-level type error (sp_RbVal is a struct; the cast # rejects). sp_poly_to_s switches on the tag and returns a # valid c-string for every variant, matching CRuby's # `print obj` -> `obj.to_s` semantics. @needs_rb_value = 1 emit(" fputs(sp_poly_to_s(" + val + "), stdout);") else if at == "mutable_str" emit(" fputs(" + val + "->data, stdout);") else if at == "string" emit(" fputs(" + val + ", stdout);") else emit(" printf(\"%lld\", (long long)" + val + ");") end end emit(" printf(\"%lld\", (long long)" + val + ");") end k = k + 1 endtest/concat_print_poly_unbox.rbaddedView file
Loading diff…
test/concat_print_poly_unbox.rb.expectedaddedView file
Loading diff…