Skip to content

Nim 2.0/devel inconsistency between equivalant code building or not #22491

@tersec

Description

@tersec

Description

proc log2trunc(x: uint64 | uint64): int =
  if x == 0: int(0) else: int(0)
template maxChunkIdx(T: type): int64 = 0'i64
template layer(vIdx: int64): int = log2trunc(0'u64)
template int2(x: int): int = x
type HashList[T] = object
  indices: array[int(layer(maxChunkIdx(T))), int]

does not compile (see current output section) but

proc log2trunc(x: uint64 | uint64): int =
  if x == 0: int(0) else: int(0)
template maxChunkIdx(T: type): int64 = 0'i64
template layer(vIdx: int64): int = log2trunc(0'u64)
template int2(x: int): int = x
type HashList[T] = object
  indices: array[int2(layer(maxChunkIdx(T))), int]

does compile, on Nim 2.0 and Nim devel, as does

proc log2trunc(x: uint64 | uint64): int =
  if x == 0: int(0) else: int(0)
template maxChunkIdx(T: type): int64 = 0'i64
template layer(vIdx: int64): int = log2trunc(0'u64)
template int2(x: int): int = x
type HashList[T] = object
  indices: array[layer(maxChunkIdx(T)), int]

There's both

  • a strange error message (maxChunkIdx returns int64 and layer takes an int64) and
  • an inconsistency between whether apparently equivalent (layer() is a function from int64 to int, so int(layer(...)) should be equivalent to layer(...)) code builds or not.

Nim 1.6 has a different problem with this, evidently addressed in 2.0 already:

a.nim(2, 8) Error: cannot generate VM code for ==

Nim Version

Nim Compiler Version 2.0.1 [Linux: amd64]
Compiled at 2023-08-16
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 037f536e7ee25c4baf23dff8a4525825c506442c
active boot switches: -d:release
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-08-16
Copyright (c) 2006-2023 by Andreas Rumpf

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

Current Output

a.nim(7, 27) Error: type mismatch
Expression: layer(maxChunkIdx(T))
  [1] maxChunkIdx(T): typeof(maxChunkIdx(T))

Expected one of (first mismatch at [position]):
[1] template layer(vIdx: int64): int

Expected Output

Consistently build or consistently don't build

Possible Solution

No response

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