Commit 6c7a9c2a
6c7a9c2a1e2dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
codegen,analyze: Integer#nonzero? returns int? nullable
Integer#nonzero? returns nil when self is 0, self otherwise.
Routes through the int? (SP_INT_NIL sentinel) nullable
representation so downstream .nil? / .inspect / arithmetic
under a guard stay on the integer path.
Refs #874
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 6c7a9c2a1e2d
- Parent
- 887376694b63
spinel_analyze.rbmodifiedView file
@@ -3780,6 +3780,9 @@
37803780
if mname == "magnitude" || mname == "modulo" || mname == "remainder" || mname == "size"37813781
return 137823782
end3783+
if mname == "nonzero?"3784+
return 13785+
end37833786
if mname == "itself" || mname == "then" || mname == "yield_self"37843787
return 137853788
end@@ -4450,6 +4453,15 @@
44504453
return "int"44514454
end44524455
end4456+
# Integer#nonzero? returns nil when receiver is 0, self otherwise.4457+
# Surfaces as int? (nullable int) so downstream .nil? and4458+
# arithmetic handling stay on the integer path. Issue #874.4459+
if mname == "nonzero?" && recv >= 04460+
rt_nz = infer_type(recv)4461+
if rt_nz == "int"4462+
return "int?"4463+
end4464+
end44534465
# Integer#gcdlcm returns [gcd, lcm] as a tuple value. Issue #894.44544466
if mname == "gcdlcm"44554467
tt_gl_inf = "tuple:int,int"spinel_codegen.rbmodifiedView file
Loading diff…
test/integer_nonzero.rbaddedView file
Loading diff…
test/integer_nonzero.rb.expectedaddedView file
Loading diff…