Skip to content

Commit

Permalink
fix arraymancer, enable static conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 28, 2024
1 parent c0ada05 commit 7679d4a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
6 changes: 5 additions & 1 deletion compiler/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ proc evalTemplateArgs(n: PNode, s: PSym; conf: ConfigRef; fromHlo: bool): PNode

result = newNodeI(nkArgList, n.info)
for i in 1..givenRegularParams:
result.add n[i]
let arg = n[i]
if arg.typ != nil and arg.typ.kind == tyStatic and arg.typ.n != nil:
result.add arg.typ.n
else:
result.add arg

# handle parameters with default values, which were
# not supplied by the user
Expand Down
3 changes: 2 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isGeneric
if result != isNone: put(c, f, aOrig)
elif aOrig.n != nil and aOrig.n.typ != nil:
# XXX this should use paramTypesMatch
result = if f.base.kind != tyNone:
typeRel(c, f.last, aOrig.n.typ, flags)
else: isGeneric
Expand Down Expand Up @@ -2328,7 +2329,7 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
elif arg.kind != nkEmpty:
var evaluated = c.semTryConstExpr(c, arg)
if evaluated != nil:
if false and f.kind == tyStatic:
if f.kind == tyStatic:
let converted = paramTypesMatch(m, f.base, evaluated.typ, evaluated, argOrig)
# if for some reason `evaluated` doesn't match `f.base`:
if converted == nil: return nil
Expand Down
11 changes: 11 additions & 0 deletions tests/proc/tdefaultvaluestatic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ block:
doAssert foo(true) == "a"
doAssert foo(false) == "b"
doAssert foo() == "b"

block:
proc foo(x: uint) = discard
proc bar(x: static int = 123) =
foo(x)
bar(123)
bar()
template baz(x: static int = 123) =
foo(x)
baz(123)
baz()
70 changes: 70 additions & 0 deletions tests/statictypes/tparamconv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# test that static conversions work

block: # issue #12559
type T = ref object
x: int
proc f(arg: static T) =
static:
assert arg == nil or arg.x > 0
discard arg.x
f nil

block: # issue #16969
proc foo(n: static[1..50]) = discard

foo(1)
doAssert not compiles(foo(0))
doAssert not compiles(foo(999))

block: # issue #7611
type Foo[N: static[int8]] = object

proc `$`[N: static[int8]](f: Foo[N]): string =
"Success"

let a = Foo[10]()
doAssert $a == "Success"

block: # issue #17423
type NaturalArray[N: static[Natural]] = array[N, int]

doAssert not (compiles do:
var a: NaturalArray[-1000])

block: # test from https://github.com/nim-works/nimskull/pull/1433
proc foo(x: static pointer): pointer = x
proc foo(x: static array[0, int]): array[0, int] = x
proc foo(x: static seq[int]): seq[int] = x
proc foo(x: static set[char]): set[char] = x

# simple case: empty-container typed expression is passed directly
doAssert foo(nil) == nil
doAssert foo([]) == []
doAssert foo(@[]) == @[]
doAssert foo({}) == {}

block: # generic version
proc foo[T](x: static[ptr T]): ptr T = x
proc foo[T](x: static array[0, T]): array[0, T] = x
proc foo[T](x: static seq[T]): seq[T] = x
proc foo[T](x: static set[T]): set[T] = x
doAssert foo[int8](nil) == nil
doAssert foo[int8]([]) == []
doAssert foo[int8](@[]) == @[]
doAssert foo[int8]({}) == {}

converter toInt(x: float): int = int(x)
block: # converter
proc test(x: static int) =
doAssert x == 1
test(1.5)

block: # subtype
type
A = ref object of RootObj
B = ref object of A

proc test(x: static A) = discard
test(B())

# proc explicit generic params don't work yet because typeRel doesn't use paramTypesMatch, #23343

0 comments on commit 7679d4a

Please sign in to comment.