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.44k stars 1.47k forks source link

Overloaded enum with {.pure.} causes nim check to segfault #21257

Closed auxym closed 1 year ago

auxym commented 1 year ago

Description

Running nim check on the following code causes a segfault:

type AC_WINCTRL_Fields* = distinct uint8

type AC_STATUSA_WSTATE0* {.pure.} = enum
  ABOVE = 0x0,
  INSIDE = 0x1,
  BELOW = 0x2,

type AC_WINCTRL_WINTSEL0* {.pure.} = enum
  ABOVE = 0x0,
  INSIDE = 0x1,
  BELOW = 0x2,
  OUTSIDE = 0x3,

proc write*(WINTSEL0: AC_WINCTRL_WINTSEL0 = ABOVE) =
  discard

I'm aware using pure with overloading is pointless, I forgot to remove the pragma as I was converting from pure enums to overloaded, which are much nicer to use. In any case, I don't believe nim should segfault.

Nim Version

Nim Compiler Version 1.9.1 [Linux: amd64]
Compiled at 2023-01-13
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 1e52423774e81f71d10a712575fd65231705362e
active boot switches: -d:release

I also experience the same issue with 1.6.10

Current Output

Hint: used config file '/home/francis/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/home/francis/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
Hint: used config file '/home/francis/source/svd2nim/nim.cfg' [Conf]
......................................................................
/home/francis/source/svd2nim/atsamd21g18a_new.nim(14, 45) Error: ambiguous identifier: 'ABOVE' -- use one of the following:
  AC_WINCTRL_WINTSEL0.ABOVE: AC_WINCTRL_WINTSEL0
  AC_STATUSA_WSTATE0.ABOVE: AC_STATUSA_WSTATE0
/home/francis/source/svd2nim/atsamd21g18a_new.nim(14, 45) Error: ambiguous identifier: 'ABOVE'
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

### Expected Output

```text
No segfault :)

Possible Solution

No idea, sorry.

Additional Information

No response

metagn commented 1 year ago

Unrelated, but if we add , typ to the semExprWithType call here the ambiguous identifier error shouldn't happen:

https://github.com/nim-lang/Nim/blob/71c60bf6f760d3a1dfc12135866cd46f817ee439/compiler/semtypes.nim#L1319

auxym commented 1 year ago

Awesome to see this resolved so quickly, thanks @metagn @Araq