Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Generic type constraints not respected with generic function

Open tersec opened this issue 2 years ago • 2 comments

Description

proc n[T: bool](k: int | int) =
  #static:
  #  doAssert T is bool
  #  doAssert T isnot int

  # And runtime
  block:
    doAssert T is bool
    doAssert T isnot int

n[int](0)

Nim Version

Nim Compiler Version 1.6.17 [Linux: amd64]
Compiled at 2023-12-02
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 2e87171e14d4f28012c53fdc54919b5491f328be
active boot switches: -d:release
Nim Compiler Version 2.0.1 [Linux: amd64]
Compiled at 2023-12-02
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 3ab6012c81a6007fc108950de6a0ae4bfc4252bd
active boot switches: -d:release
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-12-02
Copyright (c) 2006-2023 by Andreas Rumpf

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

Current Output

When using runtime asserts:
/tmp/m.nim(11)           m
/tmp/m.nim(8)            n
NimDevel/lib/std/assertions.nim(41) failedAssertImpl
NimDevel/lib/std/assertions.nim(36) raiseAssert
NimDevel/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: /tmp/m.nim(8, 5) `T is bool`  [AssertionDefect]

When using static asserts:
stack trace: (most recent call last)
/tmp/m.nim(3, 11)        n
NimDevel/lib/std/assertions.nim(41, 14) failedAssertImpl
NimDevel/lib/std/assertions.nim(36, 13) raiseAssert
NimDevel/lib/system/fatal.nim(53, 5) sysFatal
/tmp/m.nim(11, 7) template/generic instantiation of `n` from here
NimDevel/lib/system/fatal.nim(53, 5) Error: unhandled exception: /tmp/m.nim(3, 5) `T is bool`  [AssertionDefect]

Expected Output

/tmp/m.nim(11, 1) Error: cannot instantiate: 'n[int]'

Possible Solution

No response

Additional Information

No response

tersec avatar Dec 02 '23 16:12 tersec