Commit 8e272bd0
8e272bd092cbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Unify divergent multi-return tuple paths to a poly-element tuple
A method with two return a, b paths whose element types differ --
e.g. return s, s (string,string) on one path and return nil, nil
on another -- unified its return type to scalar poly while each
return a, b statement still emitted a concrete sp_Tuple_<t>_<t> *.
The function signature became sp_RbVal but the return statements
yielded tuple pointers, so cc failed with "incompatible types when
returning".
Unify divergent same-arity tuple return paths element-wise instead:
positions that agree keep their concrete type, positions that disagree
widen to poly (e.g. tuple:string,string + tuple:nil,nil ->
tuple:poly,poly). The method signature stays a tuple, so the existing
tuple destructure machinery (tuple_elem_type_at -> per-slot poly)
applies unchanged at the call site. Each return a, b path materializes
the unified tuple struct and boxes only the fields whose unified slot is
poly via box_value_to_poly.
Also type an array literal whose first element is poly (sp_RbVal) as a
poly_array. p [a, b] where a, b were destructured from the poly tuple
slots previously fell through to the int_array default and unboxed the
poly elements as raw ints.
Homogeneous multi-return tuples are byte-identical (verified by diffing emitted C): @current_method_return equals the per-path tuple, so no boxing is inserted and the struct type is unchanged. The new unify arm only fires on same-arity tuples with differing elements -- exactly the case that failed to compile before -- so no existing compiler code path changes (confirmed by diffing the compiler's own self-compiled C).
Closes #1342
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 8e272bd092cb
- Parent
- 4b380748cf20
spinel_analyze.rbmodifiedView file
end end et = infer_type(elems[0]) # A poly (sp_RbVal) first element — e.g. `[a, b]` where a, b were # destructured from a heterogeneous multi-return tuple (poly slots) # or otherwise widened to poly. The element is already boxed, so the # literal is a poly_array; the int_array default at the bottom would # otherwise emit `sp_IntArray_push((lv_a).v.i)`, unboxing the poly # as a raw int. if et == "poly" @needs_gc = 1 @needs_rb_value = 1 return "poly_array" end if et == "symbol" # Check if ALL elements are symbols all_sym = 1 "int" end def tuple_arity(t) tuple_elem_types_str(t).split(",", -1).length end # Unify two same-arity tuple types element-wise. A position where # the two element types disagree widens to `poly`; positions that # match keep their concrete type. Used to merge divergent # `return a, b` paths in unify_return_type. Two identical tuples # round-trip to the same string (every position agrees), so the # homogeneous case is unchanged. def unify_two_tuple_types(a, b) pa = tuple_elem_types_str(a).split(",", -1) pb = tuple_elem_types_str(b).split(",", -1) out = "".split(",", -1) i = 0 while i < pa.length ea = pa[i] eb = "int" if i < pb.length eb = pb[i] end if ea == eb out.push(ea) else out.push("poly") end i = i + 1 end "tuple:" + out.join(",") end # Whether a tuple element type must be traced by the GC scan function. result = t elsif t == "int" # int is default/unresolved — keep existing result elsif is_tuple_type(result) == 1 && is_tuple_type(t) == 1 && tuple_arity(result) == tuple_arity(t) # Two `return a, b` paths whose element types disagree (e.g. # `return s, s` => tuple:string,string vs `return nil, nil` # => tuple:nil,nil). A scalar `poly` here would force the # caller onto the (less-exercised) boxed-scalar destructure # path; instead unify element-wise into a same-arity tuple # whose disagreeing positions widen to `poly`. Positions that # already agree keep their concrete type, so the function # signature stays a tuple and the existing tuple destructure # machinery (tuple_elem_type_at -> per-slot poly) applies. merged_tt = unify_two_tuple_types(result, t) register_tuple_type(merged_tt) result = merged_tt else # Genuinely different types return "poly"spinel_codegen.rbmodifiedView file
Loading diff…
test/heterogeneous_tuple_return.rbaddedView file
Loading diff…
test/heterogeneous_tuple_return.rb.expectedaddedView file
Loading diff…