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

@@ -30041,6 +30041,19 @@
3004130041 # non-sym lhs vs sym rhs: also always unequal
3004230042 return op == "==" ? "FALSE" : "TRUE"
3004330043 end
30044+ # A String and a non-String scalar are never == in Ruby (no implicit
30045+ # coercion: `"3" == 3` is false). Without this the string arm below
30046+ # passes the scalar to sp_str_eq's const char* param, or the scalar
30047+ # fallthrough compares an mrb_int to a char* -- both fail to compile.
30048+ # Cast both operands to void so their side effects still fire.
30049+ if (lt == "string" && (at == "int" || at == "float" || at == "bool" || at == "bigint")) ||
30050+ ((lt == "int" || lt == "float" || lt == "bool" || lt == "bigint") && at == "string")
30051+ if op == "=="
30052+ return "(((void)(" + lc + ")), ((void)(" + rc + ")), FALSE)"
30053+ else
30054+ return "(((void)(" + lc + ")), ((void)(" + rc + ")), TRUE)"
30055+ end
30056+ end
3004430057 if lt == "string"
3004530058 if at == "nil"
3004630059 if op == "=="