Fix tupleLen not skipping aliases (#25392)

This code was failing to compile with `Error: unhandled exception:
semmagic.nim(247, 5) operand.kind == tyTuple tyAlias [AssertionDefect]`
```nim
import std/typetraits

type
  Bar[T] = T
  Foo = Bar[tuple[a: int]]

echo Foo.tupleLen
```

Fix was just making `tupleLen` skip alias types also

(cherry picked from commit 91d51923b9)
This commit is contained in:
Jake Leahy
2025-12-29 02:45:07 +11:00
committed by narimiran
parent 1dafcbd2a7
commit 34e61d5ae6
2 changed files with 6 additions and 1 deletions

View File

@@ -243,7 +243,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
let cond = operand.kind == tyTuple and operand.n != nil
result = newIntNodeT(toInt128(ord(cond)), traitCall, c.idgen, c.graph)
of "tupleLen":
var operand = operand.skipTypes({tyGenericInst})
var operand = operand.skipTypes({tyGenericInst, tyAlias})
assert operand.kind == tyTuple, $operand.kind
result = newIntNodeT(toInt128(operand.len), traitCall, c.idgen, c.graph)
of "distinctBase":

View File

@@ -194,6 +194,11 @@ block: # tupleLen
MyGenericTuple2Alias2 = MyGenericTuple2Alias[float]
static: doAssert MyGenericTuple2Alias2.tupleLen == 3
type
MyGenericTuple3[T] = T
MyGenericTuple3Alias = MyGenericTuple3[(string, int)]
static: doAssert MyGenericTuple3Alias.tupleLen == 2
static: doAssert (int, float).tupleLen == 2
static: doAssert (1, ).tupleLen == 1
static: doAssert ().tupleLen == 0