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 through collect_concat_parts -> sp_str_concat3(a, b, c) which takes three const char *s. The collector handled string/int/float arg shapes but fell through with the raw expression for poly. The matching 2-arg sp_str_concat site upstream already coerced poly via sp_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 text where text is poly -- compile_print's fallback emitted printf("%lld", (long long)<val>), which cc rejects because sp_RbVal is a struct (no scalar cast). Route poly through sp_poly_to_s to mirror the puts-side sp_poly_puts dispatch (without the trailing newline that print shouldn'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

@@ -15412,6 +15412,23 @@
1541215412 parts.push("sp_bigint_to_s((sp_Bigint *)" + compile_expr(aargs[0]) + ")")
1541315413 elsif at == "float"
1541415414 parts.push("sp_float_to_s(" + compile_expr(aargs[0]) + ")")
15415+ elsif at == "poly"
15416+ # `"a" + poly_local + "b"` -- the 2-arg sp_str_concat path at
15417+ # the single-`+` site already coerces via sp_poly_to_s; mirror
15418+ # that here for chained concat collapsing into sp_str_concat3 /
15419+ # sp_str_concat_arr. Without this, the chained form silently
15420+ # passed an sp_RbVal into a `const char *` parameter and cc
15421+ # rejected the generated code. Common shape: a local typed
15422+ # through `if x.nil?; ""; else; x; end` when x came from a
15423+ # poly-valued hash lookup -- the union stays sp_RbVal but its
15424+ # string-side payload is always a valid c-string at runtime.
15425+ parts.push("sp_poly_to_s(" + compile_expr(aargs[0]) + ")")
15426+ elsif at == "symbol"
15427+ parts.push("sp_sym_to_s(" + compile_expr(aargs[0]) + ")")
15428+ elsif at == "mutable_str"
15429+ parts.push("(" + compile_expr(aargs[0]) + ")->data")
15430+ elsif at == "bigint"
15431+ parts.push("sp_bigint_to_s(" + compile_expr(aargs[0]) + ")")
1541515432 else
1541615433 parts.push(compile_expr(aargs[0]))
1541715434 end
@@ -32150,16 +32167,25 @@
3215032167 end
3215132168 if at == "int"
3215232169 emit(" printf(\"%lld\", (long long)" + val + ");")
32170+ elsif at == "mutable_str"
32171+ emit(" fputs(" + val + "->data, stdout);")
32172+ elsif at == "string" || at == "string?"
32173+ emit(" if (" + val + ") fputs(" + val + ", stdout);")
32174+ elsif at == "float"
32175+ emit(" fputs(sp_float_to_s(" + val + "), stdout);")
32176+ elsif at == "bool"
32177+ emit(" fputs(" + val + " ? \"true\" : \"false\", stdout);")
32178+ elsif at == "poly"
32179+ # `print poly_local` -- route through sp_poly_to_s rather than
32180+ # falling through to the `printf("%lld", (long long)val)` path,
32181+ # which is a C-level type error (sp_RbVal is a struct; the cast
32182+ # rejects). sp_poly_to_s switches on the tag and returns a
32183+ # valid c-string for every variant, matching CRuby's
32184+ # `print obj` -> `obj.to_s` semantics.
32185+ @needs_rb_value = 1
32186+ emit(" fputs(sp_poly_to_s(" + val + "), stdout);")
3215332187 else
32154- if at == "mutable_str"
32155- emit(" fputs(" + val + "->data, stdout);")
32156- else
32157- if at == "string"
32158- emit(" fputs(" + val + ", stdout);")
32159- else
32160- emit(" printf(\"%lld\", (long long)" + val + ");")
32161- end
32162- end
32188+ emit(" printf(\"%lld\", (long long)" + val + ");")
3216332189 end
3216432190 k = k + 1
3216532191 end