Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM generates wrong cast for negative enum values #23925

Closed
autumngray opened this issue Aug 6, 2024 · 2 comments
Closed

VM generates wrong cast for negative enum values #23925

autumngray opened this issue Aug 6, 2024 · 2 comments
Labels
const `const x=expr` or `static: stmt` VM see also `const` label

Comments

@autumngray
Copy link
Contributor

Description

Minimal example:

static:
  type Foo = enum A = -1
  echo (cast[Foo](-1) == A)
  echo cast[Foo](-1)

Nim Version

Nim Compiler Version 2.1.9 [Linux: amd64]
Compiled at 2024-08-05
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: c97a20c
active boot switches: -d:release

Current Output

false
Foo 4294967295

Expected Output

true
A

Possible Solution

In vmgen.nim -> genCastIntFloat: tyEnum is listed in unsignedIntegers, I guess it should be signed instead?

Additional Information

The issue came up with array indexing by enum, this code works at RT but fails at CT:

static: 
  type Foo = enum A = -1
  const a: array[Foo, int] = [42]
  doAssert a[A] == 42 # works
  for f in items(Foo):
    discard a[f] # Error: index 4294967296 not in 0 .. 0
@juancarlospaco
Copy link
Collaborator

!nim c

static:
  type Foo = enum A = -1
  doAssert (cast[Foo](-1) == A), $( cast[Foo](-1) )

Copy link
Contributor

github-actions bot commented Aug 6, 2024

🐧 Linux bisect by @juancarlospaco (collaborator)
devel 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
stack trace: (most recent call last)
/home/runner/work/Nim/Nim/temp.nim(3, 11) temp
/home/runner/.choosenim/toolchains/nim-#devel/lib/std/assertions.nim(41, 14) failedAssertImpl
/home/runner/.choosenim/toolchains/nim-#devel/lib/std/assertions.nim(36, 13) raiseAssert
/home/runner/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53, 5) sysFatal
/home/runner/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53, 5) Error: unhandled exception: /home/runner/work/Nim/Nim/temp.nim(3, 3) `(cast[Foo](-1) == A)` Foo 4294967295 [AssertionDefect]
assertions.nim(34)       raiseAssert
Error: unhandled exception: errGenerated [AssertionDefect]

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:13
  • Finished 2024-08-06T16:50:14
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              newLit(-1)
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            )
          )
        )
      )
    )
  )
)
stable 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
stack trace: (most recent call last)
/home/runner/work/Nim/Nim/temp.nim(3, 11) temp
/home/runner/.choosenim/toolchains/nim-2.0.8/lib/std/assertions.nim(41, 14) failedAssertImpl
/home/runner/.choosenim/toolchains/nim-2.0.8/lib/std/assertions.nim(36, 13) raiseAssert
/home/runner/.choosenim/toolchains/nim-2.0.8/lib/system/fatal.nim(53, 5) sysFatal
/home/runner/.choosenim/toolchains/nim-2.0.8/lib/system/fatal.nim(53, 5) Error: unhandled exception: /home/runner/work/Nim/Nim/temp.nim(3, 3) `(cast[Foo](-1) == A)` Foo 4294967295 [AssertionDefect]
assertions.nim(34)       raiseAssert
Error: unhandled exception: options.nim(682, 5) `false` errGenerated [AssertionDefect]

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:17
  • Finished 2024-08-06T16:50:17
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              newLit(-1)
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            )
          )
        )
      )
    )
  )
)
2.0.4 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM does not support 'cast' from tyInt to tyEnum
assertions.nim(34)       raiseAssert
Error: unhandled exception: options.nim(681, 5) `false` errGenerated [AssertionDefect]

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:20
  • Finished 2024-08-06T16:50:21
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              newLit(-1)
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            )
          )
        )
      )
    )
  )
)
2.0.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM does not support 'cast' from tyInt to tyEnum
assertions.nim(34)       raiseAssert
Error: unhandled exception: options.nim(664, 5) `false` errGenerated [AssertionDefect]

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:24
  • Finished 2024-08-06T16:50:24
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              newLit(-1)
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            )
          )
        )
      )
    )
  )
)
1.6.20 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM does not support 'cast' from tyInt to tyEnum
fatal.nim(54)            sysFatal
Error: unhandled exception: options.nim(662, 14) `false` errGenerated [AssertionDefect]

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:26
  • Finished 2024-08-06T16:50:27
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              newLit(-1)
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              newLit(-1)
            )
          )
        )
      )
    )
  )
)
1.4.8 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM does not support 'cast' from tyInt to tyEnum

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:29
  • Finished 2024-08-06T16:50:29
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      )
    )
  )
)
1.2.18 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM does not support 'cast' from tyInt to tyEnum

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:31
  • Finished 2024-08-06T16:50:31
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      )
    )
  )
)
1.0.10 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
/home/runner/work/Nim/Nim/temp.nim(3, 11) Error: VM is only allowed to 'cast' between integers and/or floats of same size

IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-08-06T16:50:33
  • Finished 2024-08-06T16:50:34
  • Duration

AST

nnkStmtList.newTree(
  nnkStaticStmt.newTree(
    nnkStmtList.newTree(
      nnkTypeSection.newTree(
        nnkTypeDef.newTree(
          newIdentNode("Foo"),
          newEmptyNode(),
          nnkEnumTy.newTree(
            newEmptyNode(),
            nnkEnumFieldDef.newTree(
              newIdentNode("A"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      ),
      nnkCommand.newTree(
        newIdentNode("doAssert"),
        nnkPar.newTree(
          nnkInfix.newTree(
            newIdentNode("=="),
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            ),
            newIdentNode("A")
          )
        ),
        nnkPrefix.newTree(
          newIdentNode("$"),
          nnkPar.newTree(
            nnkCast.newTree(
              newIdentNode("Foo"),
              nnkPrefix.newTree(
                newIdentNode("-"),
                newLit(1)
              )
            )
          )
        )
      )
    )
  )
)
Stats
  • GCC 11.4.0
  • Clang 14.0.0
  • NodeJS 20.4
  • Created 2024-08-06T16:49:49Z
  • Comments 1
  • Commands nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

🤖 Bug found in 23 minutes bisecting 8 commits at 0 commits per second

@juancarlospaco juancarlospaco added VM see also `const` label const `const x=expr` or `static: stmt` labels Aug 6, 2024
@ringabout ringabout self-assigned this Aug 7, 2024
@ringabout ringabout removed their assignment Aug 12, 2024
@Araq Araq closed this as completed in 540b414 Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
const `const x=expr` or `static: stmt` VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants