Commit 522df524

522df5242667Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): multi-write destructure dispatches by RHS array type

a, b = self.method(...) previously emitted

sp_IntArray *_t = sp_N_method(self);
lv_a = sp_IntArray_get(_t, 0);
lv_b = sp_IntArray_get(_t, 1);

regardless of method's actual return type. So a method returning e.g. [Mat.new, Mat.new] (statically inferred as obj_Mat_ptr_array) C-compile-failed at the temp declaration (sp_IntArray *_t = sp_PtrArray *call();) and dispatched the wrong getter.

compile_multi_write now dispatches the temp declaration and the per-slot _get by the RHS's static type:

  • poly_array / poly → existing sp_PolyArray_get path
  • str_array → sp_StrArray (const char * element)
  • float_array → sp_FloatArray (mrb_float element)
  • sym_array → sp_IntArray cast to sp_sym
  • obj__ptr_array → sp_PtrArray cast to sp_<C> *
  • else → sp_IntArray (existing default)

Add test/multi_write_typed_array_dispatch.rb covering Mat-pair (homogeneous PtrArray-of-class), float-pair, and string-pair destructure shapes.

Heterogeneous tuples (e.g. [Mat.new, [1.0]]) still produce incorrect runtime values — the codegen now compiles, but the poly_dispatch path for boxed_float + ... reads .v.i instead of .v.f. Filing that separately.

Suite: 389 pass, 0 fail.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
522df5242667
Parent
9153a62f417c

spinel_codegen.rbmodifiedView file

@@ -30938,10 +30938,15 @@
3093830938 end
3093930939 else
3094030940 # RHS is a function call returning a typed array. Dispatch by
30941- # the RHS's static type — IntArray default, but PolyArray when
30942- # the inner is heterogeneous (optcarrots `@io_addr,
30941+ # the RHS's static type — IntArray default, PolyArray when the
30942+ # inner is heterogeneous (optcarrots `@io_addr,
3094330943 # @bg_pattern_lut_fetched, _ = @attr_lut[i]` where attr_lut
30944- # elements are 3-tuples of mixed types).
30944+ # elements are 3-tuples of mixed types), and the matching typed-
30945+ # array variants otherwise. Without per-type dispatch a method
30946+ # returning e.g. `[Mat.new, Mat.new]` (typed obj_Mat_ptr_array)
30947+ # destructured via `a, b = self.two_mats` got compiled as
30948+ # `sp_IntArray *_t = sp_N_two_mats(...); sp_IntArray_get(_t, i)`,
30949+ # mismatching the actual sp_PtrArray * return.
3094530950 val_t_local = infer_type(val_id)
3094630951 @needs_gc = 1
3094730952 tmp = new_temp
@@ -30958,6 +30963,16 @@
3095830963 rb_tmp_mw = new_temp
3095930964 emit(" sp_RbVal " + rb_tmp_mw + " = " + compile_expr(val_id) + ";")
3096030965 emit(" sp_PolyArray *" + tmp + " = (" + rb_tmp_mw + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + rb_tmp_mw + ".v.p : NULL;")
30966+ elsif val_t_local == "str_array"
30967+ @needs_str_array = 1
30968+ emit(" sp_StrArray *" + tmp + " = " + compile_expr(val_id) + ";")
30969+ elsif val_t_local == "float_array"
30970+ @needs_float_array = 1
30971+ emit(" sp_FloatArray *" + tmp + " = " + compile_expr(val_id) + ";")
30972+ elsif val_t_local == "sym_array"
30973+ emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
30974+ elsif is_ptr_array_type(val_t_local) == 1
30975+ emit(" sp_PtrArray *" + tmp + " = " + compile_expr(val_id) + ";")
3096130976 else
3096230977 @needs_int_array = 1
3096330978 emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
@@ -30969,6 +30984,19 @@
3096930984 if val_t_local == "poly_array" || val_t_local == "poly"
3097030985 rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"
3097130986 val_t_for_target = "poly"
30987+ elsif val_t_local == "str_array"
30988+ rhs = "sp_StrArray_get(" + tmp + ", " + k.to_s + ")"
30989+ val_t_for_target = "string"
30990+ elsif val_t_local == "float_array"
30991+ rhs = "sp_FloatArray_get(" + tmp + ", " + k.to_s + ")"
30992+ val_t_for_target = "float"
30993+ elsif val_t_local == "sym_array"
30994+ rhs = "((sp_sym)sp_IntArray_get(" + tmp + ", " + k.to_s + "))"
30995+ val_t_for_target = "symbol"
30996+ elsif is_ptr_array_type(val_t_local) == 1
30997+ elem_t = ptr_array_elem_type(val_t_local)
30998+ rhs = "((" + c_type(elem_t) + ")sp_PtrArray_get(" + tmp + ", " + k.to_s + "))"
30999+ val_t_for_target = elem_t
3097231000 else
3097331001 rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
3097431002 val_t_for_target = "int"