Skip to content

Return nil from Date#<=> for a NaN - #199

Merged
jeremyevans merged 2 commits into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan
Nov 17, 2026
Merged

jeremyevans merged 2 commits into
ruby:masterfrom
thomasklemm:thomasklemm/cmp-nan

Conversation

@thomasklemm

@thomasklemm thomasklemm commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for maintaining date.

While testing Active Model's changed? with random values, I found that Date#<=> raises ArgumentError when the other value is Float::NAN or BigDecimal("NaN"), and Date#== raises with it, while 1 <=> Float::NAN, Rational(1) <=> Float::NAN and Time.now <=> Float::NAN return nil. cmp_gen passes the result of ajd <=> other to rb_cmpint, which turns the nil from Rational#<=> into that exception.

This returns nil when ajd <=> other does and still normalizes other values to -1, 0 or 1. Date <=> NaN is now nil and Date == NaN is false, while Date < NaN still raises ArgumentError, as Comparable does for Time and Rational. The second commit makes the comparisons in step, upto and downto raise ArgumentError for a limit that Date#<=> can't compare, instead of passing nil to FIX2INT. The suite passes on Ruby 4.0.9 and master.

thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 5, 2026
Move ruby-bench #569-#572 from Merged to Open (ready), drop the duplicate
with_advisory_lock#150 entry, add ruby/date#199 and the closed ferrum#697,
mark ransack#1795 and tzinfo#175 ready, refresh heads (#59614, #59668,
ruby-bench#568, ruby/ruby#19681, tzinfo#171), add missing merge dates, and
record the 2026-11-05 open counts. RA1 item synced to ready.
@jeremyevans

Copy link
Copy Markdown
Contributor

This returns the result unchanged. Date <=> NaN is now nil and Date == NaN is false, while Date < NaN still raises ArgumentError, as for Integer.

Integer#< doesn't seem to raise ArgumentError for NaN:

$ ruby -r bigdecimal -e 'p(1 < Float::NAN); p(1 < BigDecimal("NaN"))'
false
false

@thomasklemm

Copy link
Copy Markdown
Contributor Author

You're right, thanks. Integer and Float define their own <, while Date gets it from Comparable, so Date < NaN raises as Time.now < Float::NAN and Rational(1) < Float::NAN do. I've fixed the description.

thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 14, 2026
Date#<=> passed the result of ajd <=> other to rb_cmpint, which turns
the nil that Rational#<=> returns for a NaN into an ArgumentError, so
Date <=> Float::NAN raised and Date == Float::NAN raised with it.
Return nil when ajd <=> other does, as Integer#<=> and Rational#<=> do,
and keep normalizing other results to -1, 0 or 1.
Date#step, #upto and #downto passed the result of Date#<=> to FIX2INT,
which expects a Fixnum; for nil it raises TypeError on 64-bit Linux and
macOS and its behaviour depends on the build elsewhere. Use rb_cmpint,
which raises ArgumentError for nil.
thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 15, 2026
Rebased VB1 (#59803), #59609, #59617, #59621, #59574, UG82 (#59675),
ruby/date#199 and ruby/ruby #19681, #19659, #19492 onto their current
bases; re-committed sqlite3-ruby#770 to rerun a musl Ractor flake.
All six Rails PRs and #770 are green. cells-rails#75 is red from
dependency drift on its master and was left alone.
@jeremyevans
jeremyevans merged commit e743163 into ruby:master Nov 17, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants