Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparing Date object with nil sets global $! variable to NoMethodError #6386

Closed
amitsuryavanshi opened this issue Sep 8, 2020 · 2 comments · Fixed by #6392
Closed

Comparing Date object with nil sets global $! variable to NoMethodError #6386

amitsuryavanshi opened this issue Sep 8, 2020 · 2 comments · Fixed by #6392
Milestone

Comments

@amitsuryavanshi
Copy link

amitsuryavanshi commented Sep 8, 2020

Date.today.compare_with_coercion nil or Date.today <=> nil sets the ruby global variable $! to #<NoMethodError: undefined method coerce for nil:NilClass>. It does not throw the exception but sets the global $! variable. Now when I raise the exception in my app code the raised exception's cause is set to #<NoMethodError: undefined method coerce' for nil:NilClass>`.

Environment Information

  • JRuby version - jruby-9.2.13.0
  • Operating system - MaC OS Catalina
  • Rails - 6.0.3.2

Expected Behavior

  • Raise the exception #<NoMethodError: undefined method coerce for nil:NilClass> rather than just setting it in $!

Actual Behavior

  • Sets the ruby's $! variable to #<NoMethodError: undefined method coerce for nil:NilClass> but does not raises it.

Script

require 'rubygems'
require 'active_support'
require "date"

puts 'Before $! => '
puts  $!
Date.today <=> nil
puts 'After $! => '
puts  $!

Script Output :

Before $! => 

After $! => 
undefined method `coerce' for nil:NilClass
@klobuczek
Copy link

Another way to illustrate the problem:

require "date"
require 'active_support'
begin
  Date.today <=> nil
  raise Exception
rescue Exception => e
  puts e.cause
end

The comparison does not raise any non rescued exceptions but it wrongfully sets the next Exception's cause. This does not happen on any MRI >= 2.5.0.

Unfortunately, I was not able to reproduce this behavior with plain exceptions without the Date comparison. There is something special about Date implementation.

@headius
Copy link
Member

headius commented Sep 14, 2020

Thanks @ahorek for the fix!

@headius headius added this to the JRuby 9.3.0.0 milestone Sep 14, 2020
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 a pull request may close this issue.

3 participants