From e8debeef4f9293255cabb1f9069033fa68756c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Sun, 8 Nov 2026 15:43:12 +0300 Subject: [PATCH] ZJIT: Match Integer#downto comparison semantics Use the same comparison as the C implementation so NaN endpoints continue iterating under ZJIT. Raise when the comparator returns nil, matching the C error path. Fixes Shopify/ruby#1114. --- numeric.rb | 5 ++++- test/ruby/test_integer.rb | 4 ++++ test/ruby/test_zjit_cli.rb | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/numeric.rb b/numeric.rb index 306561943d5029..3d21a9cea340f6 100644 --- a/numeric.rb +++ b/numeric.rb @@ -341,7 +341,10 @@ def downto(to) # :nodoc: end from = self - while from >= to + until (cmp = from < to) + if cmp.nil? + Primitive.cstmt! %{ rb_cmperr_reason(from, to, "comparator returned nil"); } + end yield from from = from.pred end diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 7753bce20c559d..33fa1a7feba5ed 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -309,6 +309,10 @@ def test_downto a = [] assert_equal(y, y.downto(y-2) {|x| a << x }) assert_equal([y, y-1, y-2], a) + + a = [] + 1.downto(Float::NAN) {|x| a << x; break if a.size == 3 } + assert_equal([1, 0, -1], a) end def test_times diff --git a/test/ruby/test_zjit_cli.rb b/test/ruby/test_zjit_cli.rb index 8b9765e7a2252c..950e30801b2710 100644 --- a/test/ruby/test_zjit_cli.rb +++ b/test/ruby/test_zjit_cli.rb @@ -102,6 +102,23 @@ def test = 42 } end + def test_integer_downto_comparator_returns_nil + assert_runs 'true', <<~RUBY + class Integer + def <(other) + nil + end + end + + begin + 1.downto(3.5) {} + false + rescue ArgumentError => error + error.message.include?("comparator returned nil") + end + RUBY + end + def test_enable_through_env child_env = {'RUBY_YJIT_ENABLE' => nil, 'RUBY_ZJIT_ENABLE' => '1'} assert_in_out_err([child_env, '-v'], '') do |stdout, stderr|