Commit 2b02f7b1

2b02f7b1a841Merged

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

Description

fix(codegen): #313 unbox poly-typed value to const char * at String#<< call site

When a module method returns @hash[k] || "" over a heterogeneously- valued hash, the inferred return type is sp_RbVal (poly) per #303's write-side fix. Passing that value directly into String#<< (which lowers to sp_String_append, signature const char * for the appended payload) emitted a raw sp_RbVal argument and the C compiler rejected the type mismatch.

Both << codegen sites (expression context returning the buffer, statement context discarding the result) now detect a poly arg and wrap it in sp_poly_to_s — the universal poly→string coercion that already handles every tag (string passes through, ints stringify, sym/nil/bool fall through).

Sister fix to #303: that one made the hash poly-typed when written heterogeneously; this is the read path flowing into a narrow callee. Same family as #207 / #286 / #287 / #304 (widening must propagate across whatever lowering the value lands at).

Fixes #313

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
2b02f7b1a841
Parent
ef05cb1aa590

spinel_codegen.rbmodifiedView file

@@ -21561,6 +21561,17 @@
2156121561 if lt == "mutable_str"
2156221562 @needs_mutable_str = 1
2156321563 rc = compile_expr_gc_rooted(recv)
21564+ # Issue #313: unbox a poly-typed arg to const char * before
21565+ # sp_String_append (which takes const char *). sp_poly_to_s
21566+ # handles every tag — string passes through, others coerce.
21567+ args_id = @nd_arguments[nid]
21568+ if args_id >= 0
21569+ arg_ids = get_args(args_id)
21570+ if arg_ids.length > 0 && infer_type(arg_ids[0]) == "poly"
21571+ val = "sp_poly_to_s(" + compile_expr(arg_ids[0]) + ")"
21572+ return "(sp_String_append(" + rc + ", " + val + "), " + rc + ")"
21573+ end
21574+ end
2156421575 val = compile_arg0(nid)
2156521576 return "(sp_String_append(" + rc + ", " + val + "), " + rc + ")"
2156621577 end
@@ -28622,7 +28633,15 @@
2862228633 if at == "mutable_str"
2862328634 emit(" sp_String_append(" + rc + ", " + val + "->data);")
2862428635 else
28625- emit(" sp_String_append(" + rc + ", " + val + ");")
28636+ if at == "poly"
28637+ # Issue #313: a poly-typed source (e.g. a module
28638+ # method returning sp_RbVal from a SymPolyHash
28639+ # read) needs runtime unbox to const char * before
28640+ # sp_String_append. sp_poly_to_s handles every tag.
28641+ emit(" sp_String_append(" + rc + ", sp_poly_to_s(" + val + "));")
28642+ else
28643+ emit(" sp_String_append(" + rc + ", " + val + ");")
28644+ end
2862628645 end
2862728646 end
2862828647 end