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 1
37823782 end
3783+ if mname == "nonzero?"
3784+ return 1
3785+ end
37833786 if mname == "itself" || mname == "then" || mname == "yield_self"
37843787 return 1
37853788 end
@@ -4450,6 +4453,15 @@
44504453 return "int"
44514454 end
44524455 end
4456+ # Integer#nonzero? returns nil when receiver is 0, self otherwise.
4457+ # Surfaces as int? (nullable int) so downstream .nil? and
4458+ # arithmetic handling stay on the integer path. Issue #874.
4459+ if mname == "nonzero?" && recv >= 0
4460+ rt_nz = infer_type(recv)
4461+ if rt_nz == "int"
4462+ return "int?"
4463+ end
4464+ end
44534465 # Integer#gcdlcm returns [gcd, lcm] as a tuple value. Issue #894.
44544466 if mname == "gcdlcm"
44554467 tt_gl_inf = "tuple:int,int"