nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.44k stars 1.47k forks source link

Custom iterator is not found if used in template function, if used from a third file #23833

Closed tsoj closed 2 months ago

tsoj commented 2 months ago

Description

The following code doesn't compile with nim r main.nim:

main.nim:

import piecevalue

getPieceValue[int]()

piecevalue.nim:

import bitboard

func getPieceValue*[T]() =
  for square in 0.uint64:
    discard

bitboard.nim:

iterator items*(bitboard: uint64): int =
  discard

Nim Version

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

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

Current Output

Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
.......................................................................
/tmp/testtest/piecevalue.nim(1, 8) Warning: imported and not used: 'bitboard' [UnusedImport]
/tmp/testtest/main.nim(3, 14) template/generic instantiation of `getPieceValue` from here
/tmp/testtest/piecevalue.nim(4, 18) Error: type mismatch
Expression: items(uint64(0))
  [1] uint64(0): uint64

Expected one of (first mismatch at [position]):
[1] iterator items(a: cstring): char
[1] iterator items(a: string): char
[1] iterator items[IX, T](a: array[IX, T]): T
[1] iterator items[T: Ordinal](s: Slice[T]): T
[1] iterator items[T: char](a: openArray[T]): T
[1] iterator items[T: enum and Ordinal](E: typedesc[T]): T
[1] iterator items[T: not char](a: openArray[T]): lent2 T
[1] iterator items[T](a: seq[T]): lent2 T
[1] iterator items[T](a: set[T]): T

Expected Output

Should compile

Possible Solution

No response

Additional Information

When changing in main.nim the code to:

import piecevalue, bitboard

getPieceValue[int]()

it works.

Araq commented 2 months ago

Very old, known bug that has been reported multiple times by now. The generic prepass doesn't understand the implicit items/pairs iterator invokations and maybe these were a bad idea.