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

@@ -2833,6 +2833,17 @@
28332833 end
28342834 end
28352835 et = infer_type(elems[0])
2836+ # A poly (sp_RbVal) first element — e.g. `[a, b]` where a, b were
2837+ # destructured from a heterogeneous multi-return tuple (poly slots)
2838+ # or otherwise widened to poly. The element is already boxed, so the
2839+ # literal is a poly_array; the int_array default at the bottom would
2840+ # otherwise emit `sp_IntArray_push((lv_a).v.i)`, unboxing the poly
2841+ # as a raw int.
2842+ if et == "poly"
2843+ @needs_gc = 1
2844+ @needs_rb_value = 1
2845+ return "poly_array"
2846+ end
28362847 if et == "symbol"
28372848 # Check if ALL elements are symbols
28382849 all_sym = 1
@@ -8136,6 +8147,37 @@
81368147 "int"
81378148 end
81388149
8150+ def tuple_arity(t)
8151+ tuple_elem_types_str(t).split(",", -1).length
8152+ end
8153+
8154+ # Unify two same-arity tuple types element-wise. A position where
8155+ # the two element types disagree widens to `poly`; positions that
8156+ # match keep their concrete type. Used to merge divergent
8157+ # `return a, b` paths in unify_return_type. Two identical tuples
8158+ # round-trip to the same string (every position agrees), so the
8159+ # homogeneous case is unchanged.
8160+ def unify_two_tuple_types(a, b)
8161+ pa = tuple_elem_types_str(a).split(",", -1)
8162+ pb = tuple_elem_types_str(b).split(",", -1)
8163+ out = "".split(",", -1)
8164+ i = 0
8165+ while i < pa.length
8166+ ea = pa[i]
8167+ eb = "int"
8168+ if i < pb.length
8169+ eb = pb[i]
8170+ end
8171+ if ea == eb
8172+ out.push(ea)
8173+ else
8174+ out.push("poly")
8175+ end
8176+ i = i + 1
8177+ end
8178+ "tuple:" + out.join(",")
8179+ end
8180+
81398181
81408182
81418183 # Whether a tuple element type must be traced by the GC scan function.
@@ -24535,6 +24577,19 @@
2453524577 result = t
2453624578 elsif t == "int"
2453724579 # int is default/unresolved — keep existing result
24580+ elsif is_tuple_type(result) == 1 && is_tuple_type(t) == 1 && tuple_arity(result) == tuple_arity(t)
24581+ # Two `return a, b` paths whose element types disagree (e.g.
24582+ # `return s, s` => tuple:string,string vs `return nil, nil`
24583+ # => tuple:nil,nil). A scalar `poly` here would force the
24584+ # caller onto the (less-exercised) boxed-scalar destructure
24585+ # path; instead unify element-wise into a same-arity tuple
24586+ # whose disagreeing positions widen to `poly`. Positions that
24587+ # already agree keep their concrete type, so the function
24588+ # signature stays a tuple and the existing tuple destructure
24589+ # machinery (tuple_elem_type_at -> per-slot poly) applies.
24590+ merged_tt = unify_two_tuple_types(result, t)
24591+ register_tuple_type(merged_tt)
24592+ result = merged_tt
2453824593 else
2453924594 # Genuinely different types
2454024595 return "poly"