summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJake Leahy <jake@leahy.dev>2023-11-28 19:38:10 +1100
committerGitHub <noreply@github.com>2023-11-28 09:38:10 +0100
commit8cad6ac0487800f7d41e38303e52129777e6c22e (patch)
treef91a0a0164d2576a9859103cee32d321b205f075
parentc31bbb07fb2d1ea8f69b47de50631442154bd3de (diff)
downloadNim-8cad6ac0487800f7d41e38303e52129777e6c22e.tar.gz
Don't try and get enum value if its invalid (#22997)
Currently running `nimsuggest`/`check` on this code causes the compiler
to raise an exception

```nim
type
  Test = enum
    A = 9.0 
```

```
assertions.nim(34)       raiseAssert
Error: unhandled exception: int128.nim(69, 11) `arg.sdata(3) == 0` out of range [AssertionDefect]
```

Issue was the compiler still trying to get the ordinal value even if it
wasn't an ordinal
-rw-r--r--compiler/semtypes.nim7
-rw-r--r--tests/enum/tenum_invalid.nim8
2 files changed, 12 insertions, 3 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index afc06234d..db7ce9d73 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -113,10 +113,11 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
         strVal = v
         x = counter
       else:
-        if not isOrdinalType(v.typ, allowEnumWithHoles=true):
+        if isOrdinalType(v.typ, allowEnumWithHoles=true):
+          x = toInt64(getOrdValue(v))
+          n[i][1] = newIntTypeNode(x, getSysType(c.graph, unknownLineInfo, tyInt))
+        else:
           localError(c.config, v.info, errOrdinalTypeExpected % typeToString(v.typ, preferDesc))
-        x = toInt64(getOrdValue(v))
-        n[i][1] = newIntTypeNode(x, getSysType(c.graph, unknownLineInfo, tyInt))
       if i != 1:
         if x != counter: incl(result.flags, tfEnumHasHoles)
         if x < counter:
diff --git a/tests/enum/tenum_invalid.nim b/tests/enum/tenum_invalid.nim
new file mode 100644
index 000000000..8ae0a1057
--- /dev/null
+++ b/tests/enum/tenum_invalid.nim
@@ -0,0 +1,8 @@
+discard """
+cmd: "nim check $file"
+"""
+
+type
+  Test = enum
+    A = 9.0 #[tt.Error
+        ^ ordinal type expected; given: float]#