Skip to content

Class methods with default keyword argument accepts keyword arguments with local variable name #8344

@rajaravivarma-r

Description

@rajaravivarma-r

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
    • jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 22+35-2369 on 22+35-2369 +jit [arm64-darwin]
    • Happens with and without JRUBY_OPTS="--dev"
  • Operating system and platform (e.g. uname -a)
    • Darwin admins-MacBook-Pro.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030 arm64

Other relevant info you may wish to add:

  • Installed or activated gems
    • Reproducible with standard gems
  • Application/framework version (e.g. Rails, Sinatra)
  • Environment variables
    • JAVA_HOME=/Users/rajaravivarma/.asdf/installs/java/openjdk-22

Expected Behavior
JRuby should not allow keyword arguments that are not part of the method signature for any method.

In this instance, the class methods are the one that allows keyword arguments that are not part of the method signature, but has the same name as a local variable.

Minimal example to reproduce the error. All of these test should pass, but they don't.

  • Calling A.test_method_with_default_argument(method_var: 'a') should raise an ArgumentError, but it doesn't
  • Calling A.test_method_three(method_var: 'a') should raise an ArgumentError, but it doesn't
require 'rspec'

class A
  class << self
    # Problematic method
    def test_method_with_default_argument(kw_arg: 1)
      method_var = kw_arg
      puts method_var
    end

    def test_method_two(kw_arg:)
      method_var = kw_arg
      puts method_var
    end
  end

  # Problematic method
  def self.test_method_three(kw_arg: 1)
    method_var = kw_arg
    puts method_var
  end

  def self.test_method_four(kw_arg:)
    method_var = kw_arg
    puts method_var
  end

  def test_instance_method(kw_arg: 1)
    method_var = kw_arg
    puts method_var
  end
end

RSpec.describe A do
  describe '.test_method_with_default_argument' do
    it 'should throw an exception' do
      expect { A.test_method_with_default_argument(method_var: 'a') }.to raise_error(ArgumentError)
    end
  end

  describe '.test_method_two' do
    it 'should throw and exception' do
      expect { A.test_method_two(method_var: 'a') }.to raise_error(ArgumentError)
    end
  end

  describe '.test_method_three' do
    it 'should throw and exception' do
      expect { A.test_method_three(method_var: 'a') }.to raise_error(ArgumentError)
    end
  end

  describe '.test_method_four' do
    it 'should throw and exception' do
      expect { A.test_method_four(method_var: 'a') }.to raise_error(ArgumentError)
    end
  end

  describe '#test_instance_method' do
    it 'should throw and exception' do
      expect { A.test_method_two(method_var: 'a') }.to raise_error(ArgumentError)
    end
  end
end

Actual Behavior
JRuby allows keyword arguments with same name as a local variable name in class methods.

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