Skip to content

Commit 6729ab9

Browse files
committed
fix incorrect lenTuple implementation
1 parent d1f9f11 commit 6729ab9

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

compiler/semmagic.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
187187
var operand = operand.skipTypes({tyGenericInst})
188188
let cond = operand.kind == tyTuple and operand.n != nil
189189
result = newIntNodeT(toInt128(ord(cond)), traitCall, c.graph)
190+
of "lenTuple":
191+
var operand = operand.skipTypes({tyGenericInst})
192+
assert operand.kind == tyTuple, $operand.kind
193+
result = newIntNodeT(toInt128(operand.len), traitCall, c.graph)
190194
of "distinctBase":
191195
var arg = operand.skipTypes({tyGenericInst})
192196
if arg.kind == tyDistinct:

lib/pure/typetraits.nim

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,23 @@ proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
7171
## Returns base type for distinct types, works only for distinct types.
7272
## compile time error otherwise
7373

74-
import std/macros
75-
76-
macro lenTuple*(t: tuple): int {.since: (1, 1).} =
77-
## Return number of elements of `t`
78-
newLit t.len
7974

80-
macro lenTuple*(t: typedesc[tuple]): int {.since: (1, 1).} =
75+
proc lenTuple*(T: typedesc[tuple]): int {.magic: "TypeTrait", since: (1, 1).}
8176
## Return number of elements of `T`
82-
newLit t.len
77+
78+
since (1, 1):
79+
template lenTuple*(t: tuple): int =
80+
## Return number of elements of `t`
81+
lenTuple(type(t))
8382

8483
since (1, 1):
8584
template get*(T: typedesc[tuple], i: static int): untyped =
8685
## Return `i`th element of `T`
8786
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
8887
type(default(T)[i])
8988

89+
import std/macros
90+
9091
macro genericParams*(T: typedesc): untyped {.since: (1, 1).} =
9192
## return tuple of generic params for generic `T`
9293
runnableExamples:

tests/metatype/ttypetraits.nim

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,46 @@ block distinctBase:
9292
doAssert($distinctBase(typeof(b2)) == "string")
9393
doAssert($distinctBase(typeof(c2)) == "int")
9494

95+
block lenTuple:
96+
type
97+
VectorElementType = SomeNumber | bool
98+
Vec[N : static[int], T: VectorElementType] = object
99+
arr*: array[N, T]
100+
Vec4[T: VectorElementType] = Vec[4,T]
101+
Vec4f = Vec4[float32]
102+
103+
MyTupleType = (int,float,string)
104+
static: doAssert MyTupleType.lenTuple == 3
105+
106+
type
107+
MyGenericTuple[T] = (T,int,float)
108+
MyGenericAlias = MyGenericTuple[string]
109+
static: doAssert MyGenericAlias.lenTuple == 3
110+
111+
type
112+
MyGenericTuple2[T,U] = (T,U,string)
113+
MyGenericTuple2Alias[T] = MyGenericTuple2[T,int]
114+
115+
MyGenericTuple2Alias2 = MyGenericTuple2Alias[float]
116+
static: doAssert MyGenericTuple2Alias2.lenTuple == 3
117+
118+
static: doAssert (int, float).lenTuple == 2
119+
static: doAssert (1, ).lenTuple == 1
120+
static: doAssert ().lenTuple == 0
121+
122+
let x = (1,2,)
123+
doAssert x.lenTuple == 2
124+
doAssert ().lenTuple == 0
125+
doAssert (1,).lenTuple == 1
126+
doAssert (int,).lenTuple == 1
127+
doAssert type(x).lenTuple == 2
128+
doAssert type(x).default.lenTuple == 2
129+
type T1 = (int,float)
130+
type T2 = T1
131+
doAssert T2.lenTuple == 2
132+
95133
block genericParams:
134+
96135
type Foo[T1, T2]=object
97136
doAssert genericParams(Foo[float, string]) is (float, string)
98137
type Foo1 = Foo[float, int]
@@ -103,10 +142,6 @@ block genericParams:
103142
doAssert genericParams(Foo2).get(1) is Foo1
104143
doAssert (int,).get(0) is int
105144
doAssert (int, float).get(1) is float
106-
static: doAssert (int, float).lenTuple == 2
107-
static: doAssert (1, ).lenTuple == 1
108-
static: doAssert ().lenTuple == 0
109-
110145

111146
##############################################
112147
# bug 13095

0 commit comments

Comments
 (0)