Skip to content

+ ruby31.y: parse anonymous block argument. #833

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

Merged
merged 1 commit into from
Nov 29, 2021

Conversation

iliabylich
Copy link
Collaborator

This commit tracks upstream commit ruby/ruby@4adb012.

Closes #826

@iliabylich iliabylich merged commit e160eeb into whitequark:master Nov 29, 2021
@iliabylich iliabylich deleted the anonymous-blockarg branch November 29, 2021 16:02
@iliabylich
Copy link
Collaborator Author

That was the last 3.1 change, feel free to ping me if you need a release.

@palkan
Copy link
Contributor

palkan commented Nov 29, 2021

feel free to ping me if you need a release.

Yes, please)

@iliabylich
Copy link
Collaborator Author

3.0.3.1 has been released.

koic added a commit to koic/rubocop-ast that referenced this pull request Nov 30, 2021
Follow up to whitequark/parser#833.

Here is a traditional block argument.

```console
% ruby-parse -e 'def foo(&b) = bar(&b)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg :b))
  (send nil :bar
    (block-pass
      (lvar :b))))
```

The anonymous block argument introduced in Ruby 3.1, the value of `block-pass` is nil.
So `block-pass` will have the possibility of having nil.

```console
% ruby-parse -e 'def foo(&) = bar(&)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg nil))
  (send nil :bar
    (block-pass nil)))
```

This PR prevents the following error that occur when anonymous block argument is used:

```console
% ruby -rrubocop-ast -e 'include RuboCop::AST::Traversal; walk(RuboCop::AST::ProcessedSource.new("def foo(&) = bar(&)", 3.1).ast)'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:131:in
`on_block_pass': undefined method `type' for nil:NilClass (NoMethodError)
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:159:in
        `block in on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each_with_index'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:154:in
        `on_def'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:20:in
        `walk'
        from -e:1:in `<main>'
```
koic added a commit to koic/rubocop-ast that referenced this pull request Nov 30, 2021
Follow up to whitequark/parser#833.

Here is a traditional block forwarding.

```console
% ruby-parse -e 'def foo(&b) = bar(&b)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg :b))
  (send nil :bar
    (block-pass
      (lvar :b))))
```

The anonymous block argument introduced in Ruby 3.1, the value of `block-pass` is nil.
So `block-pass` will have the possibility of having nil.

```console
% ruby-parse -e 'def foo(&) = bar(&)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg nil))
  (send nil :bar
    (block-pass nil)))
```

This PR prevents the following error that occur when anonymous block forwarding is used:

```console
% ruby -rrubocop-ast -e 'include RuboCop::AST::Traversal; walk(RuboCop::AST::ProcessedSource.new("def foo(&) = bar(&)", 3.1).ast)'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:131:in
`on_block_pass': undefined method `type' for nil:NilClass (NoMethodError)
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:159:in
        `block in on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each_with_index'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:154:in
        `on_def'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:20:in
        `walk'
        from -e:1:in `<main>'
```
marcandre pushed a commit to rubocop/rubocop-ast that referenced this pull request Dec 2, 2021
Follow up to whitequark/parser#833.

Here is a traditional block forwarding.

```console
% ruby-parse -e 'def foo(&b) = bar(&b)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg :b))
  (send nil :bar
    (block-pass
      (lvar :b))))
```

The anonymous block argument introduced in Ruby 3.1, the value of `block-pass` is nil.
So `block-pass` will have the possibility of having nil.

```console
% ruby-parse -e 'def foo(&) = bar(&)'
warning: parser/current is loading parser/ruby31, which recognizes
warning: 3.1.0-dev-compliant syntax, but you are running 3.1.0.
warning: please see
https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(def :foo
  (args
    (blockarg nil))
  (send nil :bar
    (block-pass nil)))
```

This PR prevents the following error that occur when anonymous block forwarding is used:

```console
% ruby -rrubocop-ast -e 'include RuboCop::AST::Traversal; walk(RuboCop::AST::ProcessedSource.new("def foo(&) = bar(&)", 3.1).ast)'
/Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:131:in
`on_block_pass': undefined method `type' for nil:NilClass (NoMethodError)
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:159:in
        `block in on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `each_with_index'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:156:in
        `on_send'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:154:in
        `on_def'
        from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.13.0/lib/rubocop/ast/traversal.rb:20:in
        `walk'
        from -e:1:in `<main>'
```
koic added a commit to koic/rubocop-ast that referenced this pull request Dec 17, 2021
Follow up to whitequark/parser#833.

This PR adds `anonymous_block_argument?` method to `AST::ParameterizedNode` for
Ruby 3.1's anonymous block argument syntax.

`AST::ParameterizedNode` already has `block_argument?`. So, make it responsible
for having `anonymous_block_argument` as well.
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.

ruby/ruby@4adb012
2 participants