Commit 8d52d1fd

8d52d1fd69a1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #136 from companygardener/codex/fix-empty-poly-array

fix(codegen): preserve mixed arrays after empty reassignment

Details

Commit
8d52d1fd69a1
Parent
9ad6108be22e
Merge parent
3a44076eaaa8

spinel_codegen.rbmodifiedView file

@@ -2931,6 +2931,19 @@
29312931 end
29322932 return "string"
29332933 end
2934+ if rt == "poly_array"
2935+ args_id = @nd_arguments[nid]
2936+ if args_id >= 0
2937+ a = get_args(args_id)
2938+ if a.length >= 1 && @nd_type[a[0]] == "RangeNode"
2939+ return "poly_array"
2940+ end
2941+ if a.length >= 2
2942+ return "poly_array"
2943+ end
2944+ end
2945+ return "poly"
2946+ end
29342947 if is_ptr_array_type(rt) == 1
29352948 return ptr_array_elem_type(rt)
29362949 end
@@ -6155,6 +6168,20 @@
61556168 end
61566169 return at
61576170 end
6171+ if is_array_type(old_pt) == 1 && is_array_type(at) == 1
6172+ if old_pt == "poly_array" || at == "poly_array"
6173+ @needs_rb_value = 1
6174+ return "poly_array"
6175+ end
6176+ if old_pt == "int_array"
6177+ return at
6178+ end
6179+ if at == "int_array"
6180+ return old_pt
6181+ end
6182+ @needs_rb_value = 1
6183+ return "poly_array"
6184+ end
61586185 if at == "int"
61596186 # Numeric compat: int + float is safe in both directions.
61606187 if old_pt == "float"
@@ -12436,6 +12463,15 @@
1243612463 if names[ki] == lname
1243712464 if types[ki] != at
1243812465 if types[ki] != "poly"
12466+ if is_array_type(types[ki]) == 1 && is_array_type(at) == 1
12467+ types[ki] = unify_call_types(types[ki], at, @nd_expression[nid])
12468+ if types[ki] == "poly_array"
12469+ @needs_rb_value = 1
12470+ @needs_gc = 1
12471+ end
12472+ ki = ki + 1
12473+ next
12474+ end
1243912475 # Genuine polymorphism: both the first write and this
1244012476 # write were explicit literals, and their types differ.
1244112477 # This catches `x = 1; x = "hello"` which the legacy
@@ -17409,6 +17445,14 @@
1740917445 if mname == "[]"
1741017446 return "sp_PolyArray_get(" + rc + ", " + compile_arg0(nid) + ")"
1741117447 end
17448+ if mname == "push"
17449+ arg_id = -1
17450+ aargs = get_args(@nd_arguments[nid])
17451+ if aargs.length > 0
17452+ arg_id = aargs[0]
17453+ end
17454+ return "(sp_PolyArray_push(" + rc + ", " + box_expr_to_poly(arg_id) + "), 0)"
17455+ end
1741217456 end
1741317457 ""
1741417458 end
@@ -19767,7 +19811,7 @@
1976719811 # early here also preserves the scope's already-promoted type
1976819812 # (issue #58, #85) — the fall-through path below would clobber
1976919813 # vt with infer_type([])'s "int_array" via set_var_type.
19770- if vt == "str_array" || vt == "float_array" || vt == "sym_array" || is_ptr_array_type(vt) == 1
19814+ if vt == "str_array" || vt == "float_array" || vt == "sym_array" || vt == "poly_array" || is_ptr_array_type(vt) == 1
1977119815 expr_id = @nd_expression[nid]
1977219816 if expr_id >= 0 && @nd_type[expr_id] == "ArrayNode"
1977319817 elems = parse_id_list(@nd_elements[expr_id])
@@ -19785,6 +19829,10 @@
1978519829 @needs_int_array = 1
1978619830 @needs_gc = 1
1978719831 emit(" " + vref + " = sp_IntArray_new();")
19832+ elsif vt == "poly_array"
19833+ @needs_rb_value = 1
19834+ @needs_gc = 1
19835+ emit(" " + vref + " = sp_PolyArray_new();")
1978819836 else
1978919837 @needs_gc = 1
1979019838 emit(" " + vref + " = sp_PtrArray_new();")