Commit 3a44076e

3a44076eaaa8Merged

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

Description

fix(codegen): preserve mixed arrays after empty reassignment

Fixes #134

Co-Authored-By: OpenAI Codex [email protected]

Details

Commit
3a44076eaaa8
Parent
84715d7bed78

spinel_codegen.rbmodifiedView file

@@ -2905,6 +2905,19 @@
29052905 end
29062906 return "string"
29072907 end
2908+ if rt == "poly_array"
2909+ args_id = @nd_arguments[nid]
2910+ if args_id >= 0
2911+ a = get_args(args_id)
2912+ if a.length >= 1 && @nd_type[a[0]] == "RangeNode"
2913+ return "poly_array"
2914+ end
2915+ if a.length >= 2
2916+ return "poly_array"
2917+ end
2918+ end
2919+ return "poly"
2920+ end
29082921 if is_ptr_array_type(rt) == 1
29092922 return ptr_array_elem_type(rt)
29102923 end
@@ -6050,6 +6063,20 @@
60506063 end
60516064 return at
60526065 end
6066+ if is_array_type(old_pt) == 1 && is_array_type(at) == 1
6067+ if old_pt == "poly_array" || at == "poly_array"
6068+ @needs_rb_value = 1
6069+ return "poly_array"
6070+ end
6071+ if old_pt == "int_array"
6072+ return at
6073+ end
6074+ if at == "int_array"
6075+ return old_pt
6076+ end
6077+ @needs_rb_value = 1
6078+ return "poly_array"
6079+ end
60536080 if at == "int"
60546081 # Numeric compat: int + float is safe in both directions.
60556082 if old_pt == "float"
@@ -12301,6 +12328,15 @@
1230112328 if names[ki] == lname
1230212329 if types[ki] != at
1230312330 if types[ki] != "poly"
12331+ if is_array_type(types[ki]) == 1 && is_array_type(at) == 1
12332+ types[ki] = unify_call_types(types[ki], at, @nd_expression[nid])
12333+ if types[ki] == "poly_array"
12334+ @needs_rb_value = 1
12335+ @needs_gc = 1
12336+ end
12337+ ki = ki + 1
12338+ next
12339+ end
1230412340 # Genuine polymorphism: both the first write and this
1230512341 # write were explicit literals, and their types differ.
1230612342 # This catches `x = 1; x = "hello"` which the legacy
@@ -17244,6 +17280,14 @@
1724417280 if mname == "[]"
1724517281 return "sp_PolyArray_get(" + rc + ", " + compile_arg0(nid) + ")"
1724617282 end
17283+ if mname == "push"
17284+ arg_id = -1
17285+ aargs = get_args(@nd_arguments[nid])
17286+ if aargs.length > 0
17287+ arg_id = aargs[0]
17288+ end
17289+ return "(sp_PolyArray_push(" + rc + ", " + box_expr_to_poly(arg_id) + "), 0)"
17290+ end
1724717291 end
1724817292 ""
1724917293 end
@@ -19602,7 +19646,7 @@
1960219646 # early here also preserves the scope's already-promoted type
1960319647 # (issue #58, #85) — the fall-through path below would clobber
1960419648 # vt with infer_type([])'s "int_array" via set_var_type.
19605- if vt == "str_array" || vt == "float_array" || vt == "sym_array" || is_ptr_array_type(vt) == 1
19649+ if vt == "str_array" || vt == "float_array" || vt == "sym_array" || vt == "poly_array" || is_ptr_array_type(vt) == 1
1960619650 expr_id = @nd_expression[nid]
1960719651 if expr_id >= 0 && @nd_type[expr_id] == "ArrayNode"
1960819652 elems = parse_id_list(@nd_elements[expr_id])
@@ -19620,6 +19664,10 @@
1962019664 @needs_int_array = 1
1962119665 @needs_gc = 1
1962219666 emit(" " + vref + " = sp_IntArray_new();")
19667+ elsif vt == "poly_array"
19668+ @needs_rb_value = 1
19669+ @needs_gc = 1
19670+ emit(" " + vref + " = sp_PolyArray_new();")
1962319671 else
1962419672 @needs_gc = 1
1962519673 emit(" " + vref + " = sp_PtrArray_new();")