From e3f9a33dfdab0a0366f6047f815be9fe261d04d3 Mon Sep 17 00:00:00 2001 From: zerbina <100542850+zerbina@users.noreply.github.com> Date: Sat, 15 Jun 2024 23:38:49 +0200 Subject: [PATCH] typetraits: improve `genericParams` (#1347) ## Summary The `genericParams` typetrait now also supports `ref`, `var`, `ptr`, and `range`. In addition, it's fully based on type information, making it work regardless of what expression is used as the argument. For `array` types, the index type is now *always* a `range` type, so this is a **breaking change**. ## Details The main goal is removing the mixed analysis from `genericParams`, where both type and normal AST were inspected. Besides only working for some type expressions, this also relied on `sem` modifying the input AST nodes' types in type AST, making the macro susceptible to breaking when analysis of type AST changes in `sem`. To make the implementation not rely on `sem` implementation details, `genericParams` now only uses the macro type API, also removing the need for an intermediate macro. The syntax convenience of specifying an array's index type with a single integer value does not exist at the type level, and a `StaticParam[x]` is therefore not returned for `array` types anymore. --- lib/pure/typetraits.nim | 96 ++++++++------------- tests/lang_objects/metatype/ttypetraits.nim | 11 ++- 2 files changed, 46 insertions(+), 61 deletions(-) diff --git a/lib/pure/typetraits.nim b/lib/pure/typetraits.nim index 9b3877decfd..d8b93b49d97 100644 --- a/lib/pure/typetraits.nim +++ b/lib/pure/typetraits.nim @@ -236,66 +236,12 @@ macro enumLen*(T: typedesc[enum]): int = expectKind(enumTy, nnkEnumTy) result = newLit(enumTy.len - 1) -macro genericParamsImpl(T: typedesc): untyped = - # auxiliary macro needed, can't do it directly in `genericParams` - result = newNimNode(nnkTupleConstr) - var impl = getTypeImpl(T) - expectKind(impl, nnkBracketExpr) - impl = impl[1] - while true: - case impl.kind - of nnkSym: - impl = impl.getImpl - of nnkTypeDef: - impl = impl[2] - of nnkTypeOfExpr: - impl = getTypeInst(impl[0]) - of nnkBracketExpr: - for i in 1..