From 34e61d5ae6ea6f439848a281f40bed150bc9c916 Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Mon, 29 Dec 2025 02:45:07 +1100 Subject: [PATCH] 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 91d51923b976eb5abdd1f9365958fc66bb351db6) --- compiler/semmagic.nim | 2 +- tests/metatype/ttypetraits.nim | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 0ad6117813..6cf3be014a 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -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": diff --git a/tests/metatype/ttypetraits.nim b/tests/metatype/ttypetraits.nim index 74ace75c3a..0107f6b049 100644 --- a/tests/metatype/ttypetraits.nim +++ b/tests/metatype/ttypetraits.nim @@ -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