Commit 97da57c9

97da57c9d882Merged

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

Description

Compare a poly_array against a typed array for Array#==

[1, nil, 2].compact yields a poly_array, but an array literal like [1, 2] stays an int_array, so compact == [1, 2] emitted a comparison of distinct pointer types and the -Werror build rejected it. The array == dispatch only handled same-type operands (plus an all-nil literal special case).

Add a poly_array-vs-typed-array equality (both operand orders) that boxes each int/str/float element of the typed side and compares it to the poly element via sp_poly_eq, after a length check. Placed after the all-nil-literal arm so poly == [nil, nil] keeps its dedicated path.

Fixes #1326

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

Details

Commit
97da57c9d882
Parent
b2587aed6e91

spinel_codegen.rbmodifiedView file

@@ -16547,6 +16547,28 @@
1654716547 "0"
1654816548 end
1654916549
16550+ # Equality of a poly_array against a typed (int/str/float) array: box
16551+ # each typed element and compare via sp_poly_eq, element by element.
16552+ # Covers `[1, nil, 2].compact == [1, 2]` where compact yields a
16553+ # poly_array but the literal is an int_array. `poly_c` is the
16554+ # poly_array side, `typed_c` the typed side.
16555+ def poly_typed_array_eq(poly_c, typed_c, typed_t)
16556+ @needs_rb_value = 1
16557+ tpfx = array_c_prefix(typed_t)
16558+ box = "sp_box_int"
16559+ if typed_t == "str_array"
16560+ box = "sp_box_str"
16561+ end
16562+ if typed_t == "float_array"
16563+ box = "sp_box_float"
16564+ end
16565+ pe = new_temp
16566+ ae = new_temp
16567+ ie = new_temp
16568+ re = new_temp
16569+ "({ sp_PolyArray *" + pe + " = (sp_PolyArray *)(" + poly_c + "); sp_" + tpfx + " *" + ae + " = (sp_" + tpfx + " *)(" + typed_c + "); mrb_bool " + re + "; if (!" + pe + " || !" + ae + ") " + re + " = ((void *)" + pe + " == (void *)" + ae + "); else if (" + pe + "->len != " + ae + "->len) " + re + " = FALSE; else { " + re + " = TRUE; for (mrb_int " + ie + " = 0; " + ie + " < " + pe + "->len; " + ie + "++) { if (!sp_poly_eq(sp_PolyArray_get(" + pe + ", " + ie + "), " + box + "(sp_" + tpfx + "_get(" + ae + ", " + ie + ")))) { " + re + " = FALSE; break; } } } " + re + "; })"
16570+ end
16571+
1655016572 # Equality of two ptr_array elements (each a `void*` from
1655116573 # sp_PtrArray_get) given the static element type. Typed-array elements
1655216574 # deep-compare via their own _eq; a nested ptr_array recurses one
@@ -32280,6 +32302,19 @@
3228032302 end
3228132303 end
3228232304 end
32305+ # poly_array vs a typed (int/str/float) array -- e.g.
32306+ # `[1, nil, 2].compact == [1, 2]`, where compact yields a poly_array
32307+ # but the literal stays int_array. Box the typed side per element and
32308+ # compare through sp_poly_eq. Placed after the all-nil literal arm so
32309+ # `poly == [nil, nil]` keeps its dedicated nil-boxing path.
32310+ if lt == "poly_array" && (at == "int_array" || at == "str_array" || at == "float_array")
32311+ pte = poly_typed_array_eq(lc, rc, at)
32312+ return op == "==" ? pte : "(!" + pte + ")"
32313+ end
32314+ if at == "poly_array" && (lt == "int_array" || lt == "str_array" || lt == "float_array")
32315+ pte = poly_typed_array_eq(rc, lc, lt)
32316+ return op == "==" ? pte : "(!" + pte + ")"
32317+ end
3228332318 # Hash equality arms. Same shape as the typed-array arms
3228432319 # above. Issue #555. Compares by sorted key set + per-key
3228532320 # value equality (each variant's _eq helper handles its