https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-08-09T14:48:15ZRuby Issue Tracking SystemRuby master - Bug #16092: [doc] precedence of modifier-rescuehttps://bugs.ruby-lang.org/issues/16092?journal_id=805232019-08-09T14:48:15Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>The current doc about precedence is correct. The behavior you showed is not caused by precedence, but by the grammer itself.</p>
<p>The point is, that <code><stmt> rescue <stmt></code> is a statement, not an expression. The right side of modifier-if must be an expression, so <code><stmt> rescue <stmt></code> cannot be a right side of modifier-if. So, <code><stmt> if <stmt> rescue <stmt></code> can parse only as <code>(<stmt> if <stmt>) rescue <stmt></code>.</p>
<p><code>defined?</code> also requires an expression as its argument. So <code>defined? expr rescue $!</code> can parse only as <code>defined? (expr rescue $!)</code>.</p>
<p>You can see the precedence by the following code: <code>stmt if v = condition rescue $!</code>. It can parse as both <code>(stmt if v = condition) rescue $!</code> and <code>stmt if v = (condition rescue $!)</code> but the second one is chosen because modifier-rescue has higher precedence than modifier-if.</p> Ruby master - Bug #16092: [doc] precedence of modifier-rescuehttps://bugs.ruby-lang.org/issues/16092?journal_id=805352019-08-09T18:51:23ZDan0042 (Daniel DeLorme)
<ul><li><strong>File</strong> <a href="/attachments/7981">modifier-statements.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/7981/modifier-statements.patch">modifier-statements.patch</a> added</li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/80535/diff?detail_id=54188">diff</a>)</li><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Feedback</i></li></ul><p>Ok, I'm starting to see. The difference between statements and expressions is why we get this</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">puts</span><span class="p">(</span> <span class="mi">1</span> <span class="k">if</span> <span class="mi">2</span> <span class="p">)</span> <span class="c1">#=> SyntaxError</span>
<span class="nb">puts</span><span class="p">((</span><span class="mi">1</span> <span class="k">if</span> <span class="mi">2</span><span class="p">))</span> <span class="c1">#=> 1</span>
<span class="nb">puts</span><span class="p">(</span><span class="k">if</span> <span class="mi">2</span><span class="p">;</span><span class="mi">1</span><span class="p">;</span><span class="k">end</span><span class="p">)</span> <span class="c1">#=> 1 ...interesting</span>
<span class="nb">puts</span><span class="p">(</span> <span class="mi">1</span> <span class="k">rescue</span> <span class="mi">2</span> <span class="p">)</span> <span class="c1">#=> SyntaxError</span>
<span class="nb">puts</span><span class="p">((</span><span class="mi">1</span> <span class="k">rescue</span> <span class="mi">2</span><span class="p">))</span> <span class="c1">#=> 1</span>
<span class="k">defined?</span><span class="p">(</span><span class="mi">1</span> <span class="k">rescue</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#=> SyntaxError</span>
<span class="k">defined?</span> <span class="p">(</span><span class="mi">1</span> <span class="k">rescue</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#=> "expression"</span>
</code></pre>
<p>So <code>a if b rescue c</code> is parsed as <code>‹a if b› rescue c</code> because <code>a if ‹b rescue c›</code> would be a SyntaxError.</p>
<p>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 <code>a if b rescue c</code> is equivalent to <code>a if (b rescue c)</code></p>
<p>Now that I understand the nature of the issue I've tried writing a documentation patch.</p> Ruby master - Bug #16092: [doc] precedence of modifier-rescuehttps://bugs.ruby-lang.org/issues/16092?journal_id=819812019-10-10T21:10:57Zjeremyevans (Jeremy Evans)code@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Document the difference between expressions and statements [ci skip] In the grammar, all express..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/29c1e9a0d4c855781853f0ad41b0125f42cf504d">git|29c1e9a0d4c855781853f0ad41b0125f42cf504d</a>.</p>
<hr>
<p>Document the difference between expressions and statements [ci skip]</p>
<p>In the grammar, all expressions are statements, but not all<br>
statements are expressions. Some parts of the grammar accept<br>
expressions and not other types of statements, which causes<br>
similar looking code to parse differently due to operator<br>
precedence.</p>
<p>Mostly from Dan0042 (Daniel DeLorme).</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: [doc] precedence of modifier-rescue (Closed)" href="https://bugs.ruby-lang.org/issues/16092">#16092</a>]</p>