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
"0" end # Equality of a poly_array against a typed (int/str/float) array: box # each typed element and compare via sp_poly_eq, element by element. # Covers `[1, nil, 2].compact == [1, 2]` where compact yields a # poly_array but the literal is an int_array. `poly_c` is the # poly_array side, `typed_c` the typed side. def poly_typed_array_eq(poly_c, typed_c, typed_t) @needs_rb_value = 1 tpfx = array_c_prefix(typed_t) box = "sp_box_int" if typed_t == "str_array" box = "sp_box_str" end if typed_t == "float_array" box = "sp_box_float" end pe = new_temp ae = new_temp ie = new_temp re = new_temp "({ 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 + "; })" end # Equality of two ptr_array elements (each a `void*` from # sp_PtrArray_get) given the static element type. Typed-array elements # deep-compare via their own _eq; a nested ptr_array recurses one end end end # poly_array vs a typed (int/str/float) array -- e.g. # `[1, nil, 2].compact == [1, 2]`, where compact yields a poly_array # but the literal stays int_array. Box the typed side per element and # compare through sp_poly_eq. Placed after the all-nil literal arm so # `poly == [nil, nil]` keeps its dedicated nil-boxing path. if lt == "poly_array" && (at == "int_array" || at == "str_array" || at == "float_array") pte = poly_typed_array_eq(lc, rc, at) return op == "==" ? pte : "(!" + pte + ")" end if at == "poly_array" && (lt == "int_array" || lt == "str_array" || lt == "float_array") pte = poly_typed_array_eq(rc, lc, lt) return op == "==" ? pte : "(!" + pte + ")" end # Hash equality arms. Same shape as the typed-array arms # above. Issue #555. Compares by sorted key set + per-key # value equality (each variant's _eq helper handles itstest/array_compact_eq.rbaddedView file
Loading diff…
test/array_compact_eq.rb.expectedaddedView file
Loading diff…