Project

General

Profile

Actions

Bug #17495

closed

Make args info for RubyVM::AST to available on endless method without parens

Added by pocke (Masataka Kuwabara) almost 4 years ago. Updated almost 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
$ ruby -v ruby 3.1.0dev (2020-12-31T03:30:13Z master 62450e0acf) [x86_64-linux]
[ruby-core:101837]

Description

Problem

Arguments information is missing for endless method without parens.
For example:

# ok
pp RubyVM::AbstractSyntaxTree.parse(<<~RUBY).children[2]
  def x() = 42
RUBY
# => (DEFN@1:0-1:12
#     mid: :x
#     body:
#       (SCOPE@1:0-1:12
#        tbl: []
#        args:
#          (ARGS@1:5-1:6
#           pre_num: 0
#           pre_init: nil
#           opt: nil
#           first_post: nil
#           post_num: 0
#           post_init: nil
#           rest: nil
#           kw: nil
#           kwrest: nil
#           block: nil)
#        body: (LIT@1:10-1:12 42)))
# ok
pp RubyVM::AbstractSyntaxTree.parse(<<~RUBY).children[2]
  def x() 42 end
RUBY
# => (DEFN@1:0-1:14
#     mid: :x
#     body:
#       (SCOPE@1:0-1:14
#        tbl: []
#        args:
#          (ARGS@1:5-1:6
#           pre_num: 0
#           pre_init: nil
#           opt: nil
#           first_post: nil
#           post_num: 0
#           post_init: nil
#           rest: nil
#           kw: nil
#           kwrest: nil
#           block: nil)
#        body: (LIT@1:8-1:10 42)))
# It has a problem, the `args` is nil
pp RubyVM::AbstractSyntaxTree.parse(<<~RUBY).children[2]
  def x = 42
RUBY
# => (DEFN@1:0-1:10
#     mid: :x
#     body: (SCOPE@1:0-1:10 tbl: [] args: nil body: (LIT@1:8-1:10 42)))

It causes an error if a program expects args node exists.
For example: https://github.com/ruby/rbs/issues/551

Solution Proposal

A pull request is open on GitHub: https://github.com/ruby/ruby/pull/4016

I referred to the normal method definition's implementation. https://github.com/ruby/ruby/blob/48cf548beb8d641d7dd2cb05f8470526eff7661e/parse.y#L5138-L5142

I am a very beginner at parse.y, so I'm not sure this solution is good. If something wrong, please tell me advice 🙏

Actions #1

Updated by pocke (Masataka Kuwabara) almost 4 years ago

  • Subject changed from Make args info for :RubyVM:AST to available on endless method without parens to Make args info for RubyVM::AST to available on endless method without parens
Actions #2

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|ab32e98f984318f1074c6e39b8f2f04266a1224b.


Added AST assertions for method definition arguments [Bug #17495]

Actions

Also available in: Atom PDF

Like0
Like0Like0