Precedence of single-line rescue and assignment

def boom
raise “boom”
end
y = “”
y = boom rescue “fine”
puts y # “fine”

So far so good; the precedence of “rescue” is higher than that of
assignment. But now:

def boom
raise “boom”
end
y = “”
y += boom rescue “fine”
puts y # “”

How does the precedence work here, and should this behavior be
considered a possible bug? m.

matt neuburg [email protected] wrote:

def boom
raise “boom”
end
y = “”
y += boom rescue “fine”
puts y # “”

I think what’s really going on here is that what we often see described
as a single-line use of “rescue” is actually a single-expression use
of “rescue”, since the above can be fixed using parentheses:

y += (boom rescue “fine”)

I hadn’t realized this was even legal. m.

matt neuburg wrote:

[…] since the above can be fixed using parentheses:

y += (boom rescue “fine”)

But if y += boom rescue "fine" indeed translates into y += (boom rescue "fine") that still doesn’t solve the mystery. We’d end up with
y += ("fine"). We’d get `y == “fine”’, which isn’t the case…

That’s strange!

Jens W. [email protected] wrote:

Albert S. [2008-05-07 21:27]:

matt neuburg wrote:

[…] since the above can be fixed using parentheses:

y += (boom rescue “fine”)

But if y += boom rescue "fine" indeed translates into y += (boom rescue "fine") that still doesn’t solve the mystery.
no, it evaluates to ‘(y += boom) rescue “fine”’. the ‘+=’ binds
tighter than/takes precedence over ‘rescue’.

Right, that’s what I said. The mystery is why

y += boom rescue “fine”

translates to

(y += boom) rescue “fine”

but

y = boom rescue “fine”

translates to

y = (boom rescue “fine”)

What I’m saying is: Doesn’t the user reasonably expect that = and +=
will have similar precedence? I don’t really care about this any more,
now that I know that I can disambiguate for myself using parentheses;
but it feels like a bug, somehow. m.

matt neuburg [2008-05-07 22:15]:

What I’m saying is: Doesn’t the user reasonably expect that = and
+= will have similar precedence?
well, maybe yes. the pickaxe even lists them all as having the same
precedence [1]. but as you can easily verify, all operator
assignments in fact have a higher precedence than plain assignment.

maybe it’s because “lhs ‘=’ arg kRESCUE_MOD arg” is a special case
in parse.y [2] over the more generic “stmt kRESCUE_MOD stmt”? i
don’t know. i’m far from claiming that i have grokked ruby’s parsing :wink:

however, whether this is to be considered a bug, i’m not in the
position to judge…

[1]
http://phrogz.net/ProgrammingRuby/language.html#operatorexpressions
[2] http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/parse.y

cheers
jens

Albert S. [2008-05-07 21:27]:

matt neuburg wrote:

[…] since the above can be fixed using parentheses:

y += (boom rescue “fine”)

But if y += boom rescue "fine" indeed translates into y += (boom rescue "fine") that still doesn’t solve the mystery.
no, it evaluates to ‘(y += boom) rescue “fine”’. the ‘+=’ binds
tighter than/takes precedence over ‘rescue’.

def boom; p ‘boom’; raise ‘boom’; end
=>nil
def fine; p ‘fine’; ‘fine’; end
=>nil
y = ‘’
=>""
y += boom rescue fine
“boom”
“fine”
=>“fine”
y
=>""
(y += boom) rescue fine
“boom”
“fine”
=>“fine”
y
=>""
y += (boom rescue fine)
“boom”
“fine”
=>“fine”
y
=>“fine”

cheers
jens

On Wed, May 7, 2008 at 10:15 PM, matt neuburg [email protected] wrote:

no, it evaluates to ‘(y += boom) rescue “fine”’. the ‘+=’ binds
but
but it feels like a bug, somehow. m.
And worse

y += boom rescue fine

is not equivalent to

y = y + boom rescue fine

that is not what I understand under syntactic sugar, but the behavior
is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
Well spotted indeed.

Cheers
Robert


matt neuburg, phd = [email protected], Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - Amazon.com
Read TidBITS! It’s free and smart. http://www.tidbits.com


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Hi,

In message “Re: precedence of single-line rescue and assignment”
on Thu, 8 May 2008 10:10:07 +0900, [email protected] (matt neuburg)
writes:

|> y += boom rescue fine
|>
|> is not equivalent to
|>
|> y = y + boom rescue fine
|>
|> that is not what I understand under syntactic sugar, but the behavior
|> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
|> Well spotted indeed.
|
|If we think this might be a bug, how do I report it? m.

I recognized the problem.

          matz.

Robert D. [email protected] wrote:

On Wed, May 7, 2008 at 10:15 PM, matt neuburg [email protected] wrote:

y = y + boom rescue fine

that is not what I understand under syntactic sugar, but the behavior
is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
Well spotted indeed.

If we think this might be a bug, how do I report it? m.

Yukihiro M. [email protected] wrote:

|>
|> that is not what I understand under syntactic sugar, but the behavior
|> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
|> Well spotted indeed.
|
|If we think this might be a bug, how do I report it? m.

I recognized the problem.

Wonderful, thanks! m.