[ruby-core:95718] [Ruby master Bug#16293] Numbered parameter confirmation in Ruby 2.7
From:
nobu@...
Date:
2019-11-06 03:45:37 UTC
List:
ruby-core #95718
Issue #16293 has been updated by nobu (Nobuyoshi Nakada).
Backport set to 2.5: UNKNOWN, 2.6: UNKNOWN
Tracker changed from Feature to Bug
> ```ruby
> def _1; end
>
> # expected: warning: `_1' is used as numbered parameter
> # actual: No warning
> # reason: Because "`x = _0 # expect _0()` outside block. → force warning on Ruby 2.7 and syntax error on Ruby 3" is written in the log
> x = _1
The "outside block" warning is shown at assignment to a local variable looks like a numbered parameter.
There is no assignment.
> ```ruby
> # expected: warning: `_1' is used as numbered parameter
> # and call to _1()
> # actual: Error: numbered parameter outside block (SyntaxError)
> # reason: Because hoge() method is called with `eval("hoge")`
> eval("_1")
> ```
This is a bug.
As numbered parameter looks like an ordinary variable now, the warning doesn't make sense.
> ```ruby
> def _1; end
>
> # expected: warning: `_1' is used as numbered parameter
> # and call to _1()
> # actual: Error: ordinary parameter is defined
> # reason: Because "`1.times{|i| _0 }` → force warning on Ruby 2.7 and syntax error on Ruby 3." is written in the log
> proc { |i| _1 }.call 42
> ```
The method is irrelevant, Ruby doesn't know if it is defined or not until calling it.
And `_1` in a block is considered as a numbered parameter, it conflicts with `|i|`.
> ```ruby
> # expected: warning: `_1' is used as numbered parameter
> # actual: No warning
> # reason: Because define local variable _1 is warning
> def hgoe(_1)
> end
> ```
It is outside block.
> ```ruby
> proc {
> # Warning
> _1 = 42
> }
>
> proc {
> _1
> # expected: warning: `_1' is used as numbered parameter
> # actual: No warning
> # reason: Because define local variable _1 is warning
> _1 = 42
> }
> ```
This is a bug.
It must be a syntax error.
It was a syntax error in old versions actually.
----------------------------------------
Bug #16293: Numbered parameter confirmation in Ruby 2.7
https://bugs.ruby-lang.org/issues/16293#change-82505
* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v:
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
## Overview
I want to make a final check on the behavior of Numbered parameter( No warning or Warning or Error).
There is a difference in [DevelopersMeeting20190829Japan logs](https://docs.google.com/document/d/1XypDO1crRV9uNg1_ajxkljVdN8Vdyl5hnz462bDQw34/edit) and behavior.
## Ruby version
```ruby
p RUBY_VERSION
# => "2.7.0"
p RUBY_DESCRIPTION
# => "ruby 2.7.0dev (2019-11-02T06:32:49Z trunk 772b0613c5) [x86_64-linux]"
p RUBY_RELEASE_DATE
# => "2019-11-02"
p RUBY_REVISION
# => "772b0613c583773cd2eda23bce8275926a351e79"
```
## [DevelopersMeeting20190829Japan logs](https://docs.google.com/document/d/1XypDO1crRV9uNg1_ajxkljVdN8Vdyl5hnz462bDQw34/edit)
* local variables (parameters and assigned variable) → force warning (Don’t use) on Ruby 2.7 and syntax error on Ruby 3.
* method invocation
* vcall: `x = _0 # expect _0()` outside block. → force warning on Ruby 2.7 and syntax error on Ruby 3.
* vcall: `1.times{ _0 }` → block parameter (incompatibility)
* vcall: `1.times{|i| _0 }` → force warning on Ruby 2.7 and syntax error on Ruby 3.
* method invocation (`x = _0(), x = foo._0`) → no warning
* method name (`def _0(); end`) → no warning
## Proposal
```ruby
def _1; end
# expected: warning: `_1' is used as numbered parameter
# actual: No warning
# reason: Because "`x = _0 # expect _0()` outside block. → force warning on Ruby 2.7 and syntax error on Ruby 3" is written in the log
x = _1
# expected: warning: `_1' is used as numbered parameter
# and call to _1()
# actual: Error: numbered parameter outside block (SyntaxError)
# reason: Because hoge() method is called with `eval("hoge")`
eval("_1")
```
```ruby
def _1; end
# expected: warning: `_1' is used as numbered parameter
# and call to _1()
# actual: Error: ordinary parameter is defined
# reason: Because "`1.times{|i| _0 }` → force warning on Ruby 2.7 and syntax error on Ruby 3." is written in the log
proc { |i| _1 }.call 42
```
```ruby
# expected: warning: `_1' is used as numbered parameter
# actual: No warning
# reason: Because define local variable _1 is warning
def hgoe(_1)
end
```
```ruby
proc {
# Warning
_1 = 42
}
proc {
_1
# expected: warning: `_1' is used as numbered parameter
# actual: No warning
# reason: Because define local variable _1 is warning
_1 = 42
}
```
## :MEMO: Other behavior
* Other current behavior : https://gist.github.com/osyo-manga/f332ba1f31dbc3a437acd4d86d7986dc
* Is there any other syntax to change `No warning` `Warning` `Error` ?
* Considering compatibility, make it Warning instead of Error ?
Are there other edge cases for Numbered parameter?
Thank you :)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>