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

Disallow duplicate argument names #742

Closed
pocke opened this issue Aug 16, 2021 · 4 comments
Closed

Disallow duplicate argument names #742

pocke opened this issue Aug 16, 2021 · 4 comments

Comments

@pocke
Copy link
Member

pocke commented Aug 16, 2021

Currently RBS allows duplicate argument names.

# In RBS, all of the following are valid

class C
  def required_positional: (String x, String x) -> void
  def required_keyword:    (x: String, x: String) -> void
  def positional_keyword:  (String x, x: String) -> void
end

It conceals mistakes, like ruby/gem_rbs_collection#50.

I'm 100% sure that RBS should reject duplicate keyword argument names. Kwargs are represented as a Hash, which uses argument names as hash keys, so duplicated kwarg names are completely meaningless.
But RBS can distinguish duplicate names in other cases, and it will introduce language change if we fix the problem. So I'm not 100% sure, but it will be useful if RBS reject duplicate argument names for all cases.

@ksss
Copy link
Collaborator

ksss commented Jun 7, 2024

I agree with you.
In CRuby, it results in a SyntaxError. It seems appropriate for RBS to exhibit the same behavior.

$ ruby -v
ruby 3.3.2 (2024-05-30 revision e5a195edf6) [arm64-darwin22]

## required_positional
$ ruby -we 'def foo(a, a) = 1'
-e:1: duplicated argument name
def foo(a, a) = 1
-e: compile error (SyntaxError)

## required_keyword
$ ruby -we 'def foo(a:, a:) = 1'
-e:1: duplicated argument name
def foo(a:, a:) = 1
-e: compile error (SyntaxError)

## positional_keyword
$ ruby -we 'def foo(a, a:) = 1'
-e:1: duplicated argument name
def foo(a, a:) = 1
-e: compile error (SyntaxError)

It seems that Hash report a warning when keys are duplicated.
How should this be handled for record types in RBS?

## hash
$ ruby -we 'p({a: 1, a: 2})'
-e:1: warning: key :a is duplicated and overwritten on line 1
{:a=>2}

@ParadoxV5
Copy link
Contributor

It seems that Hash report a warning when keys are duplicated. How should this be handled for record types in RBS?

## hash
$ ruby -we 'p({a: 1, a: 2})'
-e:1: warning: key :a is duplicated and overwritten on line 1
{:a=>2}

I was about to suggest Syntax Error, but then I remembered #1639.
Regarding splatted keys, a warning may be more suitable. We should also consider Ruby’s debate on https://bugs.ruby-lang.org/issues/20331.

a = {key: value}
b = {key: a, **a} #=> {key: value}, no warning

@ksss
Copy link
Collaborator

ksss commented Jun 8, 2024

I implemented duplicate argument checking and found duplicates in the following methods in the rbs repository.
However, the names of positional arguments in RBS are like annotations. This might not need to be considered.

  • IO#initialize
  • IO.popen
  • IO.open
  • Time.at
  • Socket.getaddrinfo
  • Socket::AncillaryData#initialize

@ksss
Copy link
Collaborator

ksss commented Jun 17, 2024

@pocke
I implementad #1883
Did you solve your problem?

@pocke pocke closed this as completed Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants