Commit f5151015

f515101544cdMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix: infer multi-return return a, b as a tuple struct

return a, b is a fixed-arity heterogeneous record. Spinel already has a tuple type (used by divmod / minmax / partition / zip), so use it. Previously the multi-arg case fell through to single-arg handling and silently dropped tail values.

Three pillars:

  • collect_return_types and compile_return_stmt produce tuple:T0,T1,... from the actual element types — registered via the new tuple_type_from_elems helper. Allocation passes tuple_scan_name(tt) for the GC scan function (#93 convention), so pointer fields like tuple:int,string are correctly traced.
  • compile_multi_write grows a tuple branch alongside the array-literal / int_array paths and destructures via tmp->_0, tmp->_1, ... The existing typed-array path is untouched.
  • The two scan_locals* walkers ask a new multi_write_target_type helper for each lvalue's type, so i, s = obj.int_and_str declares lv_i as mrb_int and lv_s as const char *. Non-tuple RHS keeps the legacy "int" default (the only path in wide use).

infer_call_type also gains a small fix: bare calls (no receiver) prefer a user-defined top-level method over name-based builtin inference. Without this, def minmax(a,b); …; end; lo,hi = minmax(1,2) was inferred as Array#minmax's tuple:int,int even though the user def returns int_array — causing a type mismatch at the destructure site once tuple-aware destructuring landed.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
f515101544cd
Parent
5ba02252d2e5

spinel_codegen.rbmodifiedView file

@@ -1521,6 +1521,16 @@
15211521 end
15221522 end
15231523
1524+ # User-defined top-level method (bare call): take precedence over
1525+ # name-based builtin inference so `def minmax(a,b); ... end; minmax(1,2)`
1526+ # binds to the user def instead of Array#minmax's tuple return.
1527+ if recv < 0
1528+ mi_user = find_method_idx(mname)
1529+ if mi_user >= 0
1530+ return @meth_return_types[mi_user]
1531+ end
1532+ end
1533+
15241534 # Method name-based type inference
15251535 r = infer_method_name_type(nid, mname, recv)
15261536 if r != ""
@@ -3137,6 +3147,33 @@
31373147 end
31383148 end
31393149
3150+ # Build "tuple:T0,T1,..." from a list of element node ids and register it.
3151+ def tuple_type_from_elems(elems)
3152+ parts = "".split(",")
3153+ k = 0
3154+ while k < elems.length
3155+ parts.push(infer_type(elems[k]))
3156+ k = k + 1
3157+ end
3158+ tt = "tuple:" + parts.join(",")
3159+ register_tuple_type(tt)
3160+ tt
3161+ end
3162+
3163+ # Inferred C type of the i-th lvalue in `a, b, c = rhs`. Tuple RHS gives
3164+ # per-position types; everything else falls back to "int" (matching the
3165+ # legacy default — only the homogeneous int_array case is in wide use).
3166+ def multi_write_target_type(val_id, ti)
3167+ if val_id < 0
3168+ return "int"
3169+ end
3170+ rt = infer_type(val_id)
3171+ if is_tuple_type(rt) == 1
3172+ return tuple_elem_type_at(rt, ti)
3173+ end
3174+ "int"
3175+ end
3176+
31403177 def type_is_pointer(t)
31413178 if is_nullable_type(t) == 1
31423179 t = base_type(t)
@@ -6635,6 +6672,12 @@
66356672 args_id = @nd_arguments[nid]
66366673 if args_id >= 0
66376674 arg_ids = get_args(args_id)
6675+ if arg_ids.length > 1
6676+ # `return a, b` materializes as a fixed-arity tuple. Heterogeneous
6677+ # element types are preserved unboxed (no poly_array fallback).
6678+ types.push(tuple_type_from_elems(arg_ids))
6679+ return
6680+ end
66386681 if arg_ids.length > 0
66396682 types.push(infer_type(arg_ids[0]))
66406683 return
@@ -7754,16 +7797,19 @@
77547797 end
77557798 if @nd_type[nid] == "MultiWriteNode"
77567799 targets = parse_id_list(@nd_targets[nid])
7800+ val_id = @nd_expression[nid]
7801+ ti = 0
77577802 targets.each { |tid|
77587803 if @nd_type[tid] == "LocalVariableTargetNode"
77597804 lname = @nd_name[tid]
77607805 if not_in(lname, names) == 1
77617806 if not_in(lname, params) == 1
77627807 names.push(lname)
7763- types.push("int")
7808+ types.push(multi_write_target_type(val_id, ti))
77647809 end
77657810 end
77667811 end
7812+ ti = ti + 1
77677813 }
77687814 end
77697815 # Recurse
@@ -10994,18 +11040,21 @@
1099411040 end
1099511041 if @nd_type[nid] == "MultiWriteNode"
1099611042 targets = parse_id_list(@nd_targets[nid])
11043+ val_id2 = @nd_expression[nid]
11044+ ti2 = 0
1099711045 targets.each { |tid|
1099811046 if @nd_type[tid] == "LocalVariableTargetNode"
1099911047 lname = @nd_name[tid]
1100011048 if not_in(lname, names) == 1
1100111049 if not_in(lname, params) == 1
1100211050 names.push(lname)
11003- types.push("int")
11051+ types.push(multi_write_target_type(val_id2, ti2))
1100411052 @scan_literal_flags.push("")
1100511053 @scan_empty_flags.push("")
1100611054 end
1100711055 end
1100811056 end
11057+ ti2 = ti2 + 1
1100911058 }
1101011059 if @nd_expression[nid] >= 0
1101111060 scan_locals(@nd_expression[nid], names, types, params)
@@ -17977,6 +18026,21 @@
1797718026 end
1797818027 k = k + 1
1797918028 end
18029+ elsif is_tuple_type(infer_type(val_id)) == 1
18030+ # RHS is a tuple-returning call — destructure via field access.
18031+ val_t = infer_type(val_id)
18032+ @needs_gc = 1
18033+ tmp = new_temp
18034+ emit(" " + c_type(val_t) + " " + tmp + " = " + compile_expr(val_id) + ";")
18035+ emit(" SP_GC_ROOT(" + tmp + ");")
18036+ k = 0
18037+ while k < targets.length
18038+ tid = targets[k]
18039+ if @nd_type[tid] == "LocalVariableTargetNode"
18040+ emit(" " + fiber_var_ref(@nd_name[tid]) + " = " + tmp + "->_" + k.to_s + ";")
18041+ end
18042+ k = k + 1
18043+ end
1798018044 else
1798118045 # RHS is a function call returning int_array
1798218046 @needs_int_array = 1
@@ -18536,6 +18600,24 @@
1853618600 args_id = @nd_arguments[nid]
1853718601 if args_id >= 0
1853818602 arg_ids = get_args(args_id)
18603+ if arg_ids.length > 1
18604+ # `return a, b [, c]` — materialize as a fixed-arity tuple struct.
18605+ @needs_gc = 1
18606+ tt = tuple_type_from_elems(arg_ids)
18607+ tname = tuple_c_name(tt)
18608+ arr_tmp = new_temp
18609+ emit(" " + tname + " *" + arr_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
18610+ k = 0
18611+ while k < arg_ids.length
18612+ emit(" " + arr_tmp + "->_" + k.to_s + " = " + compile_expr(arg_ids[k]) + ";")
18613+ k = k + 1
18614+ end
18615+ if @in_gc_scope == 1
18616+ emit(" SP_GC_RESTORE();")
18617+ end
18618+ emit(" return " + arr_tmp + ";")
18619+ return
18620+ end
1853918621 if arg_ids.length > 0
1854018622 rt = infer_type(arg_ids[0])
1854118623 # return nil in a nullable pointer method → return NULL