Commit b84ace11

b84ace118500Merged

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

Description

fix(codegen): generic tuple .inspect + .to_s for all variants

Tuples returned by Integer#divmod, Array#minmax, String#partition, String#rpartition, etc. used to fall through to the unresolved-call warning when chained into inspect or puts: the previous compile_inspect_for only handled three specific arities (int,int / int,float / float,float), so any tuple shape outside that triplet emitted 0 instead of the formatted string.

Two changes:

  • compile_inspect_for grows a generic fallback for any is_tuple_type. It walks tuple_elem_type_at over the arity and emits an sp_sprintf with element-specific formatters (int -> %lld, float -> sp_float_inspect, string -> "%s" with quoting, mutable_str -> ->data, symbol -> :name, bool -> "true"/"false", nil -> "nil"). Output round-trips with Ruby's array-style representation ("[a, b, c]").
  • compile_call's tuple-recv arm intercepts inspect / to_s and routes through the new compile_inspect_for path before the unresolved-call fallback.

Test: test/tuple_inspect.rb covers minmax (tuple:int,int), divmod (tuple:int,int), partition + rpartition (tuple:string,string,string).

464 tests pass; optcarrot checksum 59662.

Closes #724 Closes #726

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
b84ace118500
Parent
ac4e9f893e39

spinel_codegen.rbmodifiedView file

@@ -15954,6 +15954,14 @@
1595415954
1595515955 # Tuple methods
1595615956 if is_tuple_type(recv_type) == 1
15957+ if mname == "inspect" || mname == "to_s"
15958+ # Route through the shared tuple-inspect builder. Issues
15959+ # #724, #726.
15960+ ins_t = compile_inspect_for(recv_type, rc)
15961+ if ins_t != ""
15962+ return ins_t
15963+ end
15964+ end
1595715965 if mname == "[]"
1595815966 args_id = @nd_arguments[nid]
1595915967 if args_id >= 0
@@ -34756,6 +34764,46 @@
3475634764 if at == "tuple:float,float"
3475734765 return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))"
3475834766 end
34767+ # Generic tuple inspect for any other arity / element-type
34768+ # combination. Build "[<e0>, <e1>, ...]" with per-element
34769+ # formatters chosen by tuple_elem_type_at. Covers str
34770+ # partition (`tuple:string,string,string`) and the other
34771+ # tuple shapes that fell through before. Issues #724, #726.
34772+ if is_tuple_type(at) == 1
34773+ arity_ins = tuple_arity(at)
34774+ fmt = "["
34775+ args = ""
34776+ i_ins = 0
34777+ while i_ins < arity_ins
34778+ et = tuple_elem_type_at(at, i_ins)
34779+ sep = (i_ins == 0) ? "" : ", "
34780+ field = val + "->_" + i_ins.to_s
34781+ if et == "int"
34782+ fmt = fmt + sep + "%lld"
34783+ args = args + ", (long long)" + field
34784+ elsif et == "float"
34785+ fmt = fmt + sep + "%s"
34786+ args = args + ", sp_float_inspect(" + field + ")"
34787+ elsif et == "string" || et == "mutable_str"
34788+ fmt = fmt + sep + "\\\"%s\\\""
34789+ src = (et == "mutable_str") ? "(const char *)(" + field + "->data)" : field
34790+ args = args + ", " + src
34791+ elsif et == "symbol"
34792+ fmt = fmt + sep + ":%s"
34793+ args = args + ", sp_sym_name((sp_sym)" + field + ")"
34794+ elsif et == "bool"
34795+ fmt = fmt + sep + "%s"
34796+ args = args + ", (" + field + ") ? \"true\" : \"false\""
34797+ elsif et == "nil"
34798+ fmt = fmt + sep + "nil"
34799+ else
34800+ fmt = fmt + sep + "?"
34801+ end
34802+ i_ins = i_ins + 1
34803+ end
34804+ fmt = fmt + "]"
34805+ return "sp_sprintf(\"" + fmt + "\"" + args + ")"
34806+ end
3475934807 if at == "sym_int_hash"
3476034808 @needs_sym_int_hash = 1
3476134809 return "sp_SymIntHash_inspect(" + val + ")"