Skip to content

Commit

Permalink
static[T] related fixes (nim-lang#15853)
Browse files Browse the repository at this point in the history
* close nim-lang#9679

* close nim-lang#7546

* close nim-lang#9520

* close nim-lang#6177
  • Loading branch information
cooldome authored and PMunch committed Jan 6, 2021
1 parent ed43e70 commit d311133
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ proc semSet(c: PContext, n: PNode, prev: PType): PType =
var base = semTypeNode(c, n[1], nil)
addSonSkipIntLit(result, base, c.idgen)
if base.kind in {tyGenericInst, tyAlias, tySink}: base = lastSon(base)
if base.kind != tyGenericParam:
if base.kind notin {tyGenericParam, tyGenericInvocation}:
if not isOrdinalType(base, allowEnumWithHoles = true):
localError(c.config, n.info, errOrdinalTypeExpected)
elif lengthOrd(c.config, base) > MaxSetElements:
Expand Down
101 changes: 101 additions & 0 deletions tests/statictypes/tstatictypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ b is 2 times a
17
['\x00', '\x00', '\x00', '\x00']
heyho
Val1
Val1
'''
"""

Expand Down Expand Up @@ -239,3 +241,102 @@ let t = T[true](foo: "hey")
let u = U[false](bar: "ho")
echo t.foo, u.bar


#------------------------------------------------------------------------------
# issue #9679

discard """
output: ''''''
"""
type
Foo*[T] = object
bar*: int
dummy: T

proc initFoo(T: type, bar: int): Foo[T] =
result.bar = 1

proc fails[T](x: static Foo[T]) = # Change to non-static and it compiles
doAssert($x == "(bar: 1, dummy: 0)")

block:
const foo = initFoo(int, 2)
fails(foo)


import macros, tables

var foo{.compileTime.} = [
"Foo",
"Bar"
]

var bar{.compileTime.} = {
0: "Foo",
1: "Bar"
}.toTable()

macro fooM(): untyped =
for i, val in foo:
echo i, ": ", val

macro barM(): untyped =
for i, val in bar:
echo i, ": ", val

macro fooParam(x: static array[2, string]): untyped =
for i, val in x:
echo i, ": ", val

macro barParam(x: static Table[int, string]): untyped =
for i, val in x:
echo i, ": ", val

fooM()
barM()
fooParam(foo)
barParam(bar)


#-----------------------------------------------------------------------------------------
# issue #7546
type
rangeB[N: static[int16]] = range[0'i16 .. N]
setB[N: static[int16]] = set[rangeB[N]]

block:
var s : setB[14'i16]


#-----------------------------------------------------------------------------------------
# issue #9520

type
MyEnum = enum
Val1, Val2

proc myproc(a: static[MyEnum], b: int) =
if b < 0:
myproc(a, -b)

echo $a

myproc(Val1, -10)


#------------------------------------------------------------------------------------------
# issue #6177

type
G[N,M:static[int], T] = object
o: T

proc newG[N,M:static[int],T](x:var G[N,M,T], y:T) =
x.o = y+10*N+100*M

proc newG[N,M:static[int],T](x:T):G[N,M,T] = result.newG(x)

var x:G[2,3,int]
x.newG(4)
var y = newG[2,3,int](4)

0 comments on commit d311133

Please sign in to comment.