[ruby-core:104852] [Ruby master Bug#15404] Endless range has inconsistent chaining behaviour
From:
merch-redmine@...
Date:
2021-08-09 19:40:34 UTC
List:
ruby-core #104852
Issue #15404 has been updated by jeremyevans0 (Jeremy Evans).
I don't think we should make a range of literal ranges a parse error. While a range of ranges is not useful by default, a developer could define `Range#<=>` and `Range#succ` such that a range of ranges could be useful:
```ruby
class Range
def <=>(other)
[self.begin, self.end] <=> [other.begin, other.end]
end
def succ
(self.begin.succ)..(self.end.succ)
end
end
```
Therefore, making a range of literal ranges a parse error seems wrong to me.
----------------------------------------
Bug #15404: Endless range has inconsistent chaining behaviour
https://bugs.ruby-lang.org/issues/15404#change-93201
* Author: valich (Valentin Fondaratov)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.6.0rc1 (2018-12-06 trunk 66253) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
Everything below is tested on `Ruby 2.6.0-rc1`. Particular sexp column coordinates are wrong because I've had some leading spaces in the file, sorry.
## The essence of the bug
Syntactically, chaining normal ranges is prohibited. For example,
`(1..1)..1` produces the following sexp output:
```
[:program,
[[:dot2,
[:paren, [[:dot2, [:@int, "1", [1, 16]], [:@int, "1", [1, 19]]]]],
[:@int, "1", [1, 23]]]]]
```
while
`1..1..1` is a syntax error (compiler output: `syntax error, unexpected ..`)
New endless ranges break this behaviour and allow chaining.
There are two bugs.
1.
Chaining is possible on one line:
`1.. ..1` is parsed as
```
[:program,
[[:dot2, [:dot2, [:@int, "1", [1, 15]], nil], [:@int, "1", [1, 21]]]]]
```
I think this is inconsistent compared to the previous case.
2.
Chaining works even with newline between two parts:
```
1..
..1
```
```
[:program,
[[:dot2, [:dot2, [:@int, "1", [1, 15]], nil], [:@int, "1", [2, 17]]]]]
```
This behaviour is completely counterintuitive because `1..` on the first line is a complete statement. Even if it continues to the next line with the search for the right part of expression (end range), it should break because `..1` is not a syntactically valid range end. So, in the search for the end range parser decides to complete the first range and use it as a beginning. It contradicts older
```
1
..2
```
behaviour which effectively meant that a range could not be continued to the next line.
## Why it's important
All the code above will break on runtime because it leads to `bad value for range (ArgumentError)`. However, if the code is located in some method (or branch) which is executed rarely, developer might miss the problem.
--
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>