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 end
3175+ if rn == "String"
3176+ # Issue #203: `String.new` / `String.new("...")` returns
3177+ # a fresh mutable string buffer (sp_String *), the same
3178+ # type that `s = ""; s << ...` widens a string local
3179+ # into. Without this branch, the receiver dispatch
3180+ # treated `String` as an unknown obj-class and the call
3181+ # warned-and-emitted-0.
3182+ return "mutable_str"
3183+ end
31753184 if rn == "Proc"
31763185 return "proc"
31773186 end
@@ -17280,6 +17289,23 @@
1728017289 @needs_int_array = 1
1728117290 return "sp_IntArray_new()"
1728217291 end
17292+ if cname == "String"
17293+ # Issue #203: `String.new` and `String.new("seed")` produce
17294+ # the same sp_String * value that `s = ""` widens into when
17295+ # followed by `<<`. Mirror sp_String_new's signature: a NUL-
17296+ # terminated seed (defaulting to "") that the runtime copies
17297+ # into the freshly allocated buffer.
17298+ @needs_mutable_str = 1
17299+ @needs_gc = 1
17300+ args_id = @nd_arguments[nid]
17301+ if args_id >= 0
17302+ aargs = get_args(args_id)
17303+ if aargs.length >= 1
17304+ return "sp_String_new(" + compile_expr(aargs.first) + ")"
17305+ end
17306+ end
17307+ return "sp_String_new(\"\")"
17308+ end
1728317309 if cname == "Hash"
1728417310 @needs_str_int_hash = 1
1728517311 @needs_gc = 1