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

@@ -3260,6 +3260,21 @@
32603260 return "poly"
32613261 end
32623262 if is_array_type(lt) == 1
3263+ # Heterogeneous Array#+ (issue #662): when the rhs is also an
3264+ # Array but of a different element type, the result widens to
3265+ # poly_array. The codegen builds an sp_PolyArray that boxes each
3266+ # source element; reflect that here so the LV slot inferred from
3267+ # this expression carries through as poly_array too.
3268+ args_id_arr_plus = @nd_arguments[nid]
3269+ if args_id_arr_plus >= 0
3270+ aargs_arr_plus = get_args(args_id_arr_plus)
3271+ if aargs_arr_plus.length > 0
3272+ rt_arr_plus = infer_type(aargs_arr_plus[0])
3273+ if is_array_type(rt_arr_plus) == 1 && base_type(lt) != base_type(rt_arr_plus)
3274+ return "poly_array"
3275+ end
3276+ end
3277+ end
32633278 return lt
32643279 end
32653280 if lt == "float"
@@ -24505,6 +24520,10 @@
2450524520 end
2450624521 end
2450724522 end
24523+ # Type-vs-current-slot widening table. The first push out of
24524+ # int_array (the empty-literal default) lands on the matching
24525+ # concrete variant below. A subsequent push of an incompatible
24526+ # type widens to poly_array. Issue #663.
2450824527 if is_obj_type(arg_type) == 1
2450924528 target_type = arg_type + "_ptr_array"
2451024529 @needs_gc = 1
@@ -24513,6 +24532,10 @@
2451324532 if names[ki] == arr_name
2451424533 if types[ki] == "int_array"
2451524534 types[ki] = target_type
24535+ elsif types[ki] != target_type && is_array_type(types[ki]) == 1
24536+ @needs_rb_value = 1
24537+ @needs_gc = 1
24538+ types[ki] = "poly_array"
2451624539 end
2451724540 end
2451824541 ki = ki + 1
@@ -24524,6 +24547,10 @@
2452424547 if names[ki] == arr_name
2452524548 if types[ki] == "int_array"
2452624549 types[ki] = "str_array"
24550+ elsif types[ki] != "str_array" && is_array_type(types[ki]) == 1
24551+ @needs_rb_value = 1
24552+ @needs_gc = 1
24553+ types[ki] = "poly_array"
2452724554 end
2452824555 end
2452924556 ki = ki + 1
@@ -24535,6 +24562,10 @@
2453524562 if names[ki] == arr_name
2453624563 if types[ki] == "int_array"
2453724564 types[ki] = "float_array"
24565+ elsif types[ki] != "float_array" && is_array_type(types[ki]) == 1
24566+ @needs_rb_value = 1
24567+ @needs_gc = 1
24568+ types[ki] = "poly_array"
2453824569 end
2453924570 end
2454024571 ki = ki + 1
@@ -24548,6 +24579,10 @@
2454824579 if names[ki] == arr_name
2454924580 if types[ki] == "int_array"
2455024581 types[ki] = "sym_array"
24582+ elsif types[ki] != "sym_array" && is_array_type(types[ki]) == 1
24583+ @needs_rb_value = 1
24584+ @needs_gc = 1
24585+ types[ki] = "poly_array"
2455124586 end
2455224587 end
2455324588 ki = ki + 1