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.55k stars 1.47k forks source link

Procedure with a static range generic parameter cause type mismatch #23343

Open demotomohiro opened 8 months ago

demotomohiro commented 8 months ago

Description

When I define a generic type and a proc with a static range generic parameter and call it, it always cause type mismatch compile error.

type
  Foo[N: static int] = object
    x: array[N, int]
  Bar[N: static range[1 .. 8]] = object
    x: array[N, int]

# I can call Procedures with static int generic parameter
proc testFoo[N: static int](x: Foo[N]) =
  discard

testFoo(Foo[1]())

proc testBar[N: static int](x: Bar[N]) =
  discard

testBar(Bar[1]())

proc testFooRange[N: static range[1 .. 8]](x: Foo[N]) =
  discard

# Expression: testFooRange(Foo[1]())
#   [1] Foo[1](): Foo[1]
#
# Expected one of (first mismatch at [position]):
# [1] proc testFooRange[N: static range[1 .. 8]](x: Foo[N])
testFooRange(Foo[1]())

proc testBarRange[N: static range[1 .. 8]](x: Bar[N]) =
  discard

# Error: type mismatch
# Expression: testBarRange(Bar[1]())
#   [1] Bar[1](): Bar[1]
#
# Expected one of (first mismatch at [position]):
# [1] proc testBarRange[N: static range[1 .. 8]](x: Bar[N])
testBarRange(Bar[1]())

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64] Compiled at 2024-02-22 Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 59c65009a57ba7f9677590c5066496bb85864ab8 active boot switches: -d:release

Current Output

testrange.nim(26, 13) Error: type mismatch
Expression: testFooRange(Foo[1]())
  [1] Foo[1](): Foo[1]

Expected one of (first mismatch at [position]):
[1] proc testFooRange[N: static range[1 .. 8]](x: Foo[N])

Expected Output

Above example code is compiled without errors.

Possible Solution

No response

Additional Information

No response

metagn commented 1 month ago

Kind of related to #4858 etc, Nim accepts that 1 matches range[1..8] so it passes it directly as a static type, but its actual type remains as int. Passing range[1..8](1) instead works.