Commit 382796cd
382796cd0226Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): String.new dispatches as mutable_str constructor
String.new and String.new("seed") warned 'cannot resolve call
to 'new' on int (emitting 0)' and emitted 0 — the constructor
dispatch had no String arm, so the receiver was treated as a
generic obj-class lookup that fell through. Code combining the
result with << (the Rails view-emit accumulator pattern,
io = String.new; io << "<html>"; ...; io) then mistyped lv_io
as sp_String * from the downstream << while assigning 0,
failing the C compile.
Add String to both:
- infer_constructor_type: returns "mutable_str" (same type that
s = ""; s << ...widens to). - compile_constructor_expr: emits sp_String_new(seed) — the runtime helper already exists and accepts a NUL-terminated seed, defaulting to "".
Closes #203
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 382796cd0226
- Parent
- 291eada83ae8
spinel_codegen.rbmodifiedView file
@@ -3172,6 +3172,15 @@
31723172
if rn == "Hash"31733173
return "str_int_hash"31743174
end3175+
if rn == "String"3176+
# Issue #203: `String.new` / `String.new("...")` returns3177+
# a fresh mutable string buffer (sp_String *), the same3178+
# type that `s = ""; s << ...` widens a string local3179+
# into. Without this branch, the receiver dispatch3180+
# treated `String` as an unknown obj-class and the call3181+
# warned-and-emitted-0.3182+
return "mutable_str"3183+
end31753184
if rn == "Proc"31763185
return "proc"31773186
end@@ -17280,6 +17289,23 @@
1728017289
@needs_int_array = 11728117290
return "sp_IntArray_new()"1728217291
end17292+
if cname == "String"17293+
# Issue #203: `String.new` and `String.new("seed")` produce17294+
# the same sp_String * value that `s = ""` widens into when17295+
# followed by `<<`. Mirror sp_String_new's signature: a NUL-17296+
# terminated seed (defaulting to "") that the runtime copies17297+
# into the freshly allocated buffer.17298+
@needs_mutable_str = 117299+
@needs_gc = 117300+
args_id = @nd_arguments[nid]17301+
if args_id >= 017302+
aargs = get_args(args_id)17303+
if aargs.length >= 117304+
return "sp_String_new(" + compile_expr(aargs.first) + ")"17305+
end17306+
end17307+
return "sp_String_new(\"\")"17308+
end1728317309
if cname == "Hash"1728417310
@needs_str_int_hash = 11728517311
@needs_gc = 1test/issue203_string_new.rbaddedView file
Loading diff…