Commit b69ff9f5
b69ff9f545fdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): heterogeneous Array#<< and Array#+ widen to poly_array
Fixes #662 and #663.
#663 (Array#<<): scan_locals's push/<< inference latched on to the first push's concrete family (int_array -> str_array etc.) but never widened when a subsequent push was a different type. The codegen then emitted e.g. sp_StrArray_push with an int literal and failed -Werror. Now: when the slot is already a concrete family that doesn't match the new push's element type, widen to poly_array.
#662 (Array#+): the typed + codegen dup'd lhs and pushed each rhs
element via the same prefix (sp_IntArray_push(dup, sp_IntArray_get(rhs, i))),
which mismatched when lhs and rhs had different element families.
Now: infer_call_type returns "poly_array" for lhs + rhs when the
two arrays disagree on element type, and compile_+ builds an
sp_PolyArray boxing each source element via sp_box_int / sp_box_str
/ sp_box_float / sp_box_sym / sp_box_obj(cls_id) as appropriate.
Closes #662 #663.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b69ff9f545fd
- Parent
- 5de7de3d6c5f
spinel_analyze.rbmodifiedView file
return "poly" end if is_array_type(lt) == 1 # Heterogeneous Array#+ (issue #662): when the rhs is also an # Array but of a different element type, the result widens to # poly_array. The codegen builds an sp_PolyArray that boxes each # source element; reflect that here so the LV slot inferred from # this expression carries through as poly_array too. args_id_arr_plus = @nd_arguments[nid] if args_id_arr_plus >= 0 aargs_arr_plus = get_args(args_id_arr_plus) if aargs_arr_plus.length > 0 rt_arr_plus = infer_type(aargs_arr_plus[0]) if is_array_type(rt_arr_plus) == 1 && base_type(lt) != base_type(rt_arr_plus) return "poly_array" end end end return lt end if lt == "float" end end end # Type-vs-current-slot widening table. The first push out of # int_array (the empty-literal default) lands on the matching # concrete variant below. A subsequent push of an incompatible # type widens to poly_array. Issue #663. if is_obj_type(arg_type) == 1 target_type = arg_type + "_ptr_array" @needs_gc = 1 if names[ki] == arr_name if types[ki] == "int_array" types[ki] = target_type elsif types[ki] != target_type && is_array_type(types[ki]) == 1 @needs_rb_value = 1 @needs_gc = 1 types[ki] = "poly_array" end end ki = ki + 1 if names[ki] == arr_name if types[ki] == "int_array" types[ki] = "str_array" elsif types[ki] != "str_array" && is_array_type(types[ki]) == 1 @needs_rb_value = 1 @needs_gc = 1 types[ki] = "poly_array" end end ki = ki + 1 if names[ki] == arr_name if types[ki] == "int_array" types[ki] = "float_array" elsif types[ki] != "float_array" && is_array_type(types[ki]) == 1 @needs_rb_value = 1 @needs_gc = 1 types[ki] = "poly_array" end end ki = ki + 1 if names[ki] == arr_name if types[ki] == "int_array" types[ki] = "sym_array" elsif types[ki] != "sym_array" && is_array_type(types[ki]) == 1 @needs_rb_value = 1 @needs_gc = 1 types[ki] = "poly_array" end end ki = ki + 1spinel_codegen.rbmodifiedView file
Loading diff…
test/array_plus_cross_type.rbaddedView file
Loading diff…
test/array_push_mixed_types.rbaddedView file
Loading diff…