Skip to content

Avoid to generate RBS with Syntax Error by prototype runtime#483

Merged
soutaro merged 1 commit intoruby:masterfrom
pocke:Avoid-to-generate-RBS-with-Syntax-Error-by-prototype-runtime
Nov 23, 2020
Merged

Avoid to generate RBS with Syntax Error by prototype runtime#483
soutaro merged 1 commit intoruby:masterfrom
pocke:Avoid-to-generate-RBS-with-Syntax-Error-by-prototype-runtime

Conversation

@pocke
Copy link
Member

@pocke pocke commented Nov 23, 2020

This pull request fixes a syntax error that is generated by rbs prototype runtime command on def f(...) end syntax.

Problem

rbs prototype runtime command generates RBS with a syntax error if a method is defined with argument forwarding syntax that is introduced since Ruby 2.7.

For example:

# test.rb

class C
  def f(...) end
end
$ rbs prototype runtime -R ./test.rb C
class C
  public

  def f: (*untyped *) { (*untyped) -> untyped } -> untyped
end

It displays *untyped * as the method arguments, but the trailing * is meaningless.

Cause

RubyVM::AST::Node includes :* as an argument name if the method is defined with argument forwarding syntax.
For example:

irb> pp RubyVM::AbstractSyntaxTree.parse('def f(...) end')
(SCOPE@1:0-1:14
 tbl: []
 args: nil
 body:
   (DEFN@1:0-1:14
    mid: :f
    body:
      (SCOPE@1:0-1:14
       tbl: [:*, :&]
       args:
         (ARGS@1:5-1:10
          pre_num: 0
          pre_init: nil
          opt: nil
          first_post: nil
          post_num: 0
          post_init: nil
          rest: :*        # <- HERE!
          kw: nil
          kwrest: nil
          block: :&)
       body: nil)))

Solution

Treat :* as an unnamed argument.

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@soutaro soutaro merged commit d1353b4 into ruby:master Nov 23, 2020
@pocke pocke deleted the Avoid-to-generate-RBS-with-Syntax-Error-by-prototype-runtime branch November 23, 2020 10:33
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.

2 participants