ruby / rbs

Type Signature for Ruby
Other
1.94k stars 211 forks source link

Make it a syntax error when the keys of keyword and record are duplicated. #1883

Closed ksss closed 3 months ago

ksss commented 3 months ago

Ref https://github.com/ruby/rbs/issues/742

I have made it so that a syntax error is raised when required keyword arguments and optional keyword arguments have duplicate keys.

$ bundle exec ruby -r rbs -e 'RBS::Parser.parse_method_type("(a: top, a: top) -> void")'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/parser_aux.rb:15:in `_parse_method_type': a.rbs:1:9...1:10: Syntax error: duplicated keyword argument, token=`a` (tLIDENT) (RBS::ParsingError)

  (a: top, a: top) -> void
           ^

Similarly, for record types, I have made it so that a syntax error is raised when there are duplicate keys between required keys and optional keys.

$ bundle exec ruby -r rbs -e 'RBS::Parser.parse_type("{ a: top, a: top }")'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/parser_aux.rb:10:in `_parse_type': a.rbs:1:10...1:11: Syntax error: duplicated record key, token=`a` (tLIDENT) (RBS::ParsingError)

  { a: top, a: top }
            ^

Benchmark

There appears to be no noticeable deterioration.

$ bundle exec ruby benchmark/benchmark_new_env.rb

# master
ruby 3.3.2 (2024-05-30 revision e5a195edf6) [arm64-darwin22]
Warming up --------------------------------------
             new_env     1.000 i/100ms
       new_rails_env     1.000 i/100ms
Calculating -------------------------------------
             new_env      8.175 (±12.2%) i/s -     81.000 in  10.027816s
       new_rails_env      1.465 (± 0.0%) i/s -     15.000 in  10.281491s

# duplicate-keyword-arguments
ruby 3.3.2 (2024-05-30 revision e5a195edf6) [arm64-darwin22]
Warming up --------------------------------------
             new_env     1.000 i/100ms
       new_rails_env     1.000 i/100ms
Calculating -------------------------------------
             new_env      8.896 (±11.2%) i/s -     88.000 in  10.020704s
       new_rails_env      1.494 (± 0.0%) i/s -     15.000 in  10.076984s