[前][次][番号順一覧][スレッド一覧]

ruby-changes:64696

From: Masataka <ko1@a...>
Date: Fri, 1 Jan 2021 14:25:34 +0900 (JST)
Subject: [ruby-changes:64696] de5f8a92d5 (master): Make args info for RubyVM::AST to available on endless method without parens

https://git.ruby-lang.org/ruby.git/commit/?id=de5f8a92d5

From de5f8a92d5001799bedb3b1a271a2d9b23c6c8fb Mon Sep 17 00:00:00 2001
From: Masataka Pocke Kuwabara <kuwabara@p...>
Date: Fri, 1 Jan 2021 14:25:08 +0900
Subject: Make args info for RubyVM::AST to available on endless method without
 parens

Problem
===

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

```ruby
# 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
====

Call `new_args` on this case.

diff --git a/parse.y b/parse.y
index fa70b1b..bd1717d 100644
--- a/parse.y
+++ b/parse.y
@@ -5003,7 +5003,13 @@ superclass	: '<' https://github.com/ruby/ruby/blob/trunk/parse.y#L5003
 		    }
 		;
 
-f_opt_paren_args: f_paren_args | none;
+f_opt_paren_args: f_paren_args
+		| none
+		    {
+			$$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
+			$$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
+		    }
+		;
 
 f_paren_args	: '(' f_args rparen
 		    {
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]