nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

Case statement incorrectly reported as not exhaustive #14952

Open valashko opened 4 years ago

valashko commented 4 years ago

Presence of double assignment inside the case statement incorrectly triggers compiler error not all cases are covered.

Example

type State {.pure.} = enum
    First
    Second

proc stateToString(state: State): string =
  var dummy: string
  case state # Error: not all cases are covered; missing: {Second}
  of State.First:
      result = dummy = "one"
  of State.Second:
      result = dummy = "two"

echo stateToString(State.First)

Current Output

/usercode/in.nim(7, 3) Error: not all cases are covered; missing: {Second}

Expected Output

one

Additional Information

Nim Compiler Version 1.2.4 (Nim playground)
Clyybber commented 4 years ago

The code is invalid, there exists no double assignment in nim.

valashko commented 4 years ago

Well, I am quite surprised that a = b is not an l-value like in every other programming language. At the very least I would expect a proper error message.

Araq commented 4 years ago

Well, I am quite surprised that a = b is not an l-value like in every other programming language.

Python lacks it too and is a very successful programming language.

jibal commented 4 years ago

Well, I am quite surprised that a = b is not an l-value like in every other programming language.

There are quite a few languages where it is not an l-value.

At the very least I would expect a proper error message.

That's a very legitimate complaint. And if it's changed to

proc stateToString(state: State): string =
  var dummy: string
  result = dummy = "one"

then the error message is "tst.nim(7, 18) Error: invalid indentation" which is Nim's favorite obscure fallback error message. At least it gives the correct column number for the error.

ghost commented 4 years ago

@jibal your code is invalid anyway, in Nim assignments don't return a value, they're statements, so you can't do "double assignment" like that

jibal commented 4 years ago

@jibal your code is invalid anyway, in Nim assignments don't return a value, they're statements, so you can't do "double assignment" like that

I'm well aware that the code is invalid (which was already discussed above), but that has absolutely nothing to do with the point, which is about the error message.

ghost commented 4 years ago

@jibal well I know, that's why I marked my message as off-topic so you wouldn't reply to it, but you still did :)

jibal commented 4 years ago

@jibal well I know, that's why I marked my message as off-topic so you wouldn't reply to it, but you still did :)

Better to delete it, since it's completely irrelevant and pointless. It doesn't matter how you marked it; that you posted it resulted in sending an annoying message to my mailbox.