Skip to content

kwargs in Array.each mutates argument #5788

@amacleay

Description

@amacleay

Environment

From https://oss.sonatype.org/content/repositories/snapshots/org/jruby/jruby-dist/9000.dev-SNAPSHOT/jruby-dist-9000.dev-20140908.223539-1-bin.tar.gz

$ bash jruby-9000.dev-SNAPSHOT/bin/jruby --version
jruby 9000.dev-SNAPSHOT (2.1.2p142) 2014-09-04 177ed02 Java HotSpot(TM) 64-Bit Server VM 25.181-b13 on 1.8.0_181-b13 [linux-amd64]
09:56:01 ~/repro
$ uname -a
Linux amacleay-cht-tp 4.15.0-54-generic #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Script:

# $ cat repro.rb 
list = Array.new(200, { a: 1, b: 2, c: 3 })
list.each do |a:, b:, **|
  puts "#{a} #{b}"
end

Expected Behavior

It should print 1 2 200 times and exit happily, as does MRI:

$ rvm ruby-2.5.0 do ruby repro.rb | nl | tail -n1
   200  1 2

Actual Behavior

Runs once and then fails. Further investigation shows that the runtime is mutating the invocant to .each

$ bash jruby-9000.dev-SNAPSHOT/bin/jruby repro.rb 
1 2
ArgumentError: missing keyword: a
  (root) at repro.rb:2
    each at org/jruby/RubyArray.java:1546
  (root) at repro.rb:2

More recent releases run a variable number of iterations and then fail:
9.2.5.0:

$ rvm jruby-9.2.5.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   177  1 2
10:03:08 ~/repro
$ rvm jruby-9.2.5.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   143  1 2
10:03:13 ~/repro
$ rvm jruby-9.2.5.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   142  1 2

9.2.7.0:

$ rvm jruby-9.2.7.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   151  1 2
10:03:38 ~/repro
$ rvm jruby-9.2.7.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   193  1 2
10:03:42 ~/repro
$ rvm jruby-9.2.7.0 do ruby repro.rb | nl | tail -n1
ArgumentError: missing keyword: a
  <main> at repro.rb:2
    each at org/jruby/RubyArray.java:1792
  <main> at repro.rb:2
   171  1 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions