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
# Tuple methods if is_tuple_type(recv_type) == 1 if mname == "inspect" || mname == "to_s" # Route through the shared tuple-inspect builder. Issues # #724, #726. ins_t = compile_inspect_for(recv_type, rc) if ins_t != "" return ins_t end end if mname == "[]" args_id = @nd_arguments[nid] if args_id >= 0 if at == "tuple:float,float" return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))" end # Generic tuple inspect for any other arity / element-type # combination. Build "[<e0>, <e1>, ...]" with per-element # formatters chosen by tuple_elem_type_at. Covers str # partition (`tuple:string,string,string`) and the other # tuple shapes that fell through before. Issues #724, #726. if is_tuple_type(at) == 1 arity_ins = tuple_arity(at) fmt = "[" args = "" i_ins = 0 while i_ins < arity_ins et = tuple_elem_type_at(at, i_ins) sep = (i_ins == 0) ? "" : ", " field = val + "->_" + i_ins.to_s if et == "int" fmt = fmt + sep + "%lld" args = args + ", (long long)" + field elsif et == "float" fmt = fmt + sep + "%s" args = args + ", sp_float_inspect(" + field + ")" elsif et == "string" || et == "mutable_str" fmt = fmt + sep + "\\\"%s\\\"" src = (et == "mutable_str") ? "(const char *)(" + field + "->data)" : field args = args + ", " + src elsif et == "symbol" fmt = fmt + sep + ":%s" args = args + ", sp_sym_name((sp_sym)" + field + ")" elsif et == "bool" fmt = fmt + sep + "%s" args = args + ", (" + field + ") ? \"true\" : \"false\"" elsif et == "nil" fmt = fmt + sep + "nil" else fmt = fmt + sep + "?" end i_ins = i_ins + 1 end fmt = fmt + "]" return "sp_sprintf(\"" + fmt + "\"" + args + ")" end if at == "sym_int_hash" @needs_sym_int_hash = 1 return "sp_SymIntHash_inspect(" + val + ")"test/tuple_inspect.rbaddedView file
Loading diff…
test/tuple_inspect.rb.expectedaddedView file
Loading diff…