Skip to content

const procs in const array are nil in half of array #16737

@Anuken

Description

@Anuken

Code

type ProcType = proc()

const a: ProcType = proc() = discard
const arr: array[8, ProcType] = [a, a, a, a, a, a, a, a]

for i in 0..<arr.len:
  echo "[", i, "]: ", arr[i].isNil
echo "\nwith constant index:\n[7]: ", arr[7].isNil

Current Output

[0]: false
[1]: false
[2]: false
[3]: false
[4]: true
[5]: true
[6]: true
[7]: true

with constant index:
[7]: false

Expected Output

[0]: false
[1]: false
[2]: false
[3]: false
[4]: false
[5]: false
[6]: false
[7]: false

with constant index:
[7]: false

Workarounds

  • Declaring both the proc and the array as var fixes the problem in this test case. In my codebase, it leads to invalid codegen that I have been unable to isolate.

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-01-16
Copyright (c) 2006-2021 by Andreas Rumpf

44ceefa9fefca58ba32e9638f754d7f4ca8a2b82 (current git head)

Metadata

Metadata

Assignees

No one assigned

    Labels

    const`const x=expr` or `static: stmt`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions