Skip to content

Fix Time.new when using (empty) keywords (2 issues)#8858

Merged
headius merged 2 commits intojruby:masterfrom
PChambino:time-new-with-empty-keywords
Jun 23, 2025
Merged

Fix Time.new when using (empty) keywords (2 issues)#8858
headius merged 2 commits intojruby:masterfrom
PChambino:time-new-with-empty-keywords

Conversation

@PChambino
Copy link
Contributor

@PChambino PChambino commented Jun 5, 2025

Fixes #8854 and #8860.

#8854
irb> Time.new(2025, 4, 3, **{})
org.jruby.dist/org.jruby.RubyTime.initialize(RubyTime.java:1758):
Cannot load from object array because "opts" is null (Java::JavaLang::NullPointerException)

#8860
irb> Time.new(2025, 4, 3, in: 'UTC')
=> 2025-04-01 00:00:00 +0000
# The day is incorrect. It always loses the last arg.

The travel_to test helper from ActiveSupport triggers the empty keywords issue.

As a result of fixing the NullPointerException and writing a regression test, it actually triggered the other issue of losing the last argument when using keywords.

I realised that if keywords was empty, hasKeywords still returns true and extractKeywordArgs returns null, and args doesn't have the Hash object, it is just missing. So that is why we can only subtract 1 from argc after extractKeywordArgs and confirming it is not null. Also, since we are already subtracting one, then we don't need the keywords condition again in the argc switch.

I added a test for it since it was a regression from JRuby v9.

PChambino added 2 commits June 5, 2025 18:50
Solves jruby#8854

Prevents `Time.new(2023, 2, 1, **{})` from raising NullPointerException
when accessing `opts` which can happened when keyword args are empty, e.g:
https://github.com/rails/rails/blob/v7.1.5.1/activesupport/lib/active_support/testing/time_helpers.rb#L185

Unsure if this is the best way, but it does the job for now I think.

I realised that if keywords was empty, `hasKeywords` still returns true
and `extractKeywordArgs` returns null, and `args` doesn't have the Hash object,
it is just missing. So that is why we can only subtract 1 from
argc after `extractKeywordArgs` and confirming it is not null.

It feels there is a more general problem of how keywords are handled or/and
a missing helper to avoid these issues as I think this bug may be in a
couple more places in the codebase, but probably people don't hit them frequently.

I added a test for it since it was a regression from JRuby v9, but also
unsure where best to put the test or if it is something we want / need.

Feel free to make any changes or suggestions.
@PChambino PChambino changed the title Handle empty keywords for Time.new Correctly handle args + (empty) keywords for Time.new Jun 11, 2025
@PChambino PChambino changed the title Correctly handle args + (empty) keywords for Time.new Fix Time.new when using (empty) keywords (2 issues) Jun 21, 2025
@headius
Copy link
Member

headius commented Jun 23, 2025

Hey sorry I missed this, it's been a busy month!

Thanks so much for pitching in and fixing these issues! I'll review today and get it merged for future releases.

@headius
Copy link
Member

headius commented Jun 23, 2025

Looks good to me. Typical case of us not using new mechanism for detecting keywords.

@headius headius merged commit 16cd846 into jruby:master Jun 23, 2025
72 checks passed
@headius headius added this to the JRuby 10.0.1.0 milestone Jun 23, 2025
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.

Java::JavaLang::NullPointerException: Unable to find org.jruby.RubyTime.initialize(RubyTime.java to read failed line

2 participants