mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 02:12:11 +00:00
static[T] related fixes (#15853)
* close #9679 * close #7546 * close #9520 * close #6177
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -10,6 +10,8 @@ b is 2 times a
|
||||
17
|
||||
['\x00', '\x00', '\x00', '\x00']
|
||||
heyho
|
||||
Val1
|
||||
Val1
|
||||
'''
|
||||
"""
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user