https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-07-06T07:34:54ZRuby Issue Tracking SystemRuby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=791562019-07-06T07:34:54Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I guess it should be possible to distinguish between "else if" and "elsif"<br>
by the ruby parser already, since it does fail if the 'e ' part is there<br>
in the first case, as otherwise the parser would accept the syntax. So I<br>
think it would be possible to change the error message without too much<br>
difficulty. Whether that ought to happen or not is, in my opinion, up to<br>
matz and the core team e. g. whether they want to have a different message<br>
show up here. I personally don't mind either way.</p>
<p>What I think has not yet been described is: what message would you recommend<br>
to appear other than the current one? This may help matz and/or the ruby<br>
core team to decide how useful the proposed change may be. Newcomers are of<br>
course one target audience, but so are other ruby users; so the new error<br>
message, if decided to make a change, should cover useful cases for both<br>
groups of ruby users.</p> Ruby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=791642019-07-07T00:18:21Znicholaslyang (Nicholas Yang)
<ul></ul><p>The problem is that this is valid Ruby:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="kp">false</span>
<span class="nb">puts</span> <span class="s1">'false'</span>
<span class="k">else</span> <span class="k">if</span> <span class="kp">true</span>
<span class="nb">puts</span> <span class="s1">'true'</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>So we can't really tell if <code>else if</code> is invalid until we parse the second <code>end</code>.</p> Ruby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=791652019-07-07T04:27:14Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Warning only after a syntax error?</p>
<pre><code>$ ./ruby -w
if false
else if true
end
-:3: syntax error, unexpected end-of-input, expecting `end'
-:3: `end' of `if' matched `else' before `if'
-:2: maybe `elsif'?
</code></pre> Ruby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=791772019-07-07T06:46:14Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/79177/diff?detail_id=52499">diff</a>)</li><li><strong>ruby -v</strong> deleted (<del><i>ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]</i></del>)</li><li><strong>Backport</strong> deleted (<del><i>2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN</i></del>)</li></ul> Ruby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=791782019-07-07T06:51:00Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Enable indentation warning against `if` just after `else` ```ruby if false puts 'false' else i..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/d548073f68ae719933c328686df224f74a60d366">git|d548073f68ae719933c328686df224f74a60d366</a>.</p>
<hr>
<p>Enable indentation warning against <code>if</code> just after <code>else</code></p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="kp">false</span>
<span class="nb">puts</span> <span class="s1">'false'</span>
<span class="k">else</span> <span class="k">if</span> <span class="kp">true</span>
<span class="nb">puts</span> <span class="s1">'true'</span>
<span class="k">end</span> <span class="c1"># -:5: warning: mismatched indentations at 'end' with 'if' at 3</span>
<span class="k">end</span>
</code></pre>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: "else if" causes confusing syntax error (Closed)" href="https://bugs.ruby-lang.org/issues/15990">#15990</a>]</p> Ruby master - Feature #15990: "else if" causes confusing syntax errorhttps://bugs.ruby-lang.org/issues/15990?journal_id=800562019-07-26T03:47:04Zviko (Viko Viko)
<ul></ul><p>Sorry for the delay, but that isn't actually a helpful error message in this case. Certainly it's an improvement, but it still doesn't actually explain the issue. Is there some reason your proposal in #2 isn't possible?</p>