Skip to content

opcCastIntToPtr error when sorting based on const array #25437

@etan-status

Description

@etan-status

Nim Version

Nim Compiler Version 2.2.4 [MacOSX: arm64]
Compiled at 2025-12-29
Copyright (c) 2006-2025 by Andreas Rumpf

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

Description

import std/algorithm

func doCompute(a: ptr UncheckedArray[int], len: int): seq[int] =
  var foo = newSeq[int](len)
  for i in 0 ..< len:
    foo[i] = i
  foo.sort do (x, y: auto) -> int:
    cmp(a[x], a[y])
  foo

func compute(a: openArray[int]): seq[int] =
  doCompute(cast[ptr UncheckedArray[int]](unsafeAddr a[0]), a.len)

const x = [1, 2, 3]
static: echo compute(x)  # Works without `static`

Current Output

stack trace: (most recent call last)
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(15, 21) test
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(12, 13) compute
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(12, 13) Error: opcCastIntToPtr: regs[rb].kind: rkNodeAddr

Expected Output

@[0, 1, 2]

Known Workarounds

Works at runtime

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions