[ruby-core:94230] [Ruby master Bug#16092] [doc] precedence of modifier-rescue

From: daniel@...42.com
Date: 2019-08-09 18:51:23 UTC
List: ruby-core #94230
Issue #16092 has been updated by Dan0042 (Daniel DeLorme).

Status changed from Rejected to Feedback
Description updated
File modifier-statements.patch added

Ok, I'm starting to see. The difference between statements and expressions is why we get this

```ruby
puts( 1 if 2 )   #=> SyntaxError
puts((1 if 2))   #=> 1
puts(if 2;1;end) #=> 1   ...interesting
puts( 1 rescue 2 ) #=> SyntaxError
puts((1 rescue 2)) #=> 1
defined?(1 rescue 2)  #=> SyntaxError
defined? (1 rescue 2) #=> "expression"
```

So `a if b rescue c` is parsed as `窶ケa if b窶コ rescue c` because `a if 窶ケb rescue c窶コ` would be a SyntaxError.


In any case, even if the issue is not precedence itself, I think the documentation should be updated somehow, because it leads one to think that `a if b rescue c` is equivalent to `a if (b rescue c)`

Now that I understand the nature of the issue I've tried writing a documentation patch.

----------------------------------------
Bug #16092: [doc] precedence of modifier-rescue
https://bugs.ruby-lang.org/issues/16092#change-80535

* Author: Dan0042 (Daniel DeLorme)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
The docs state that modifier-rescue has higher precedence than assignments which have higher precedence than modifier-if. This is true for
`v = expr rescue $! if condition` but not for
`v = expr if condition rescue $!` which is treated as
`(v = expr if condition) rescue $!` rather than
`v = expr if (condition rescue $!)`

This goes similarly for
`defined? expr rescue $!` 
`not expr rescue $!`
`expr1 and expr2 rescue $!`
`expr1 or expr2 rescue $!`

So maybe the documentation should state that modifier-rescue has **equal** precedence to modifier-if & others, with an **exception** made for assignments? I'm not entirely sure how to describe that exception though.

---Files--------------------------------
modifier-statements.patch (3.74 KB)


-- 
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>

In This Thread

Prev Next