Commit b29baef9
b29baef903c2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Compare String and non-String scalars as unequal in ==
"3" == 3 failed to compile: the string == arm passed the integer to
sp_str_eq's const char* parameter (and the symmetric 3 == "3" compared
an mrb_int to a char*). Ruby does no implicit coercion here, so the
answer is simply false.
Add a heterogeneous arm (String vs int/float/bool/bigint, either
direction) that returns FALSE for == / TRUE for !=, mirroring the
existing nil- and symbol-mismatch arms. Both operands are cast to void
so their side effects still fire.
"3" == 3 # => false (was: compile error) 3 != "3" # => true "3" == "3" # => true (same-type path unchanged)
Fixes #1168
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b29baef903c2
- Parent
- b7d6e2341b30
spinel_codegen.rbmodifiedView file
# non-sym lhs vs sym rhs: also always unequal return op == "==" ? "FALSE" : "TRUE" end # A String and a non-String scalar are never == in Ruby (no implicit # coercion: `"3" == 3` is false). Without this the string arm below # passes the scalar to sp_str_eq's const char* param, or the scalar # fallthrough compares an mrb_int to a char* -- both fail to compile. # Cast both operands to void so their side effects still fire. if (lt == "string" && (at == "int" || at == "float" || at == "bool" || at == "bigint")) || ((lt == "int" || lt == "float" || lt == "bool" || lt == "bigint") && at == "string") if op == "==" return "(((void)(" + lc + ")), ((void)(" + rc + ")), FALSE)" else return "(((void)(" + lc + ")), ((void)(" + rc + ")), TRUE)" end end if lt == "string" if at == "nil" if op == "=="test/eq_string_vs_int.rbaddedView file
Loading diff…
test/eq_string_vs_int.rb.expectedaddedView file
Loading diff…