Project

General

Profile

Actions

Feature #15990

closed

"else if" causes confusing syntax error

Added by viko (Viko Viko) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:93573]

Description

if false
  puts 'false'
else if true
  puts 'true'
end

This fails with "syntax error: unexpected end-of-input, expecting end". else if isn't elsif, of course, but if a file contains else if (especially on one line, though I don't know how easy that would be to track) and fails to parse, it is probably worth noting, especially since this kind of syntax error can't easily be traced back to a particular line.

Updated by shevegen (Robert A. Heiler) over 4 years ago

I guess it should be possible to distinguish between "else if" and "elsif"
by the ruby parser already, since it does fail if the 'e ' part is there
in the first case, as otherwise the parser would accept the syntax. So I
think it would be possible to change the error message without too much
difficulty. Whether that ought to happen or not is, in my opinion, up to
matz and the core team e. g. whether they want to have a different message
show up here. I personally don't mind either way.

What I think has not yet been described is: what message would you recommend
to appear other than the current one? This may help matz and/or the ruby
core team to decide how useful the proposed change may be. Newcomers are of
course one target audience, but so are other ruby users; so the new error
message, if decided to make a change, should cover useful cases for both
groups of ruby users.

Updated by nicholaslyang (Nicholas Yang) over 4 years ago

The problem is that this is valid Ruby:

if false
  puts 'false'
else if true
  puts 'true'
end
end

So we can't really tell if else if is invalid until we parse the second end.

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

Warning only after a syntax error?

$ ./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'?
Actions #4

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Tracker changed from Bug to Feature
  • Description updated (diff)
  • ruby -v deleted (ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18])
  • Backport deleted (2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN)
Actions #5

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|d548073f68ae719933c328686df224f74a60d366.


Enable indentation warning against if just after else

if false
  puts 'false'
else if true
  puts 'true'
end # -:5: warning: mismatched indentations at 'end' with 'if' at 3
end

[Feature #15990]

Updated by viko (Viko Viko) over 4 years ago

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?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0