@@ -1493,6 +1493,22 @@ macro expandMacros*(body: typed): untyped =
14931493 echo body.toStrLit
14941494 result = body
14951495
1496+ proc extractTypeImpl (n: NimNode ): NimNode =
1497+ # # attempts to extract the type definition of the given symbol
1498+ case n.kind
1499+ of nnkSym: # can extract an impl
1500+ result = n.getImpl.extractTypeImpl ()
1501+ of nnkObjectTy, nnkRefTy, nnkPtrTy: result = n
1502+ of nnkBracketExpr:
1503+ if n.typeKind == ntyTypeDesc:
1504+ result = n[1 ].extractTypeImpl ()
1505+ else :
1506+ doAssert n.typeKind == ntyGenericInst
1507+ result = n[0 ].getImpl ()
1508+ of nnkTypeDef:
1509+ result = n[2 ]
1510+ else : error (" Invalid node to retrieve type implementation of: " & $ n.kind)
1511+
14961512proc customPragmaNode (n: NimNode ): NimNode =
14971513 expectKind (n, {nnkSym, nnkDotExpr, nnkBracketExpr, nnkTypeOfExpr, nnkCheckedFieldExpr})
14981514 let
@@ -1501,7 +1517,10 @@ proc customPragmaNode(n: NimNode): NimNode =
15011517 if typ.kind == nnkBracketExpr and typ.len > 1 and typ[1 ].kind == nnkProcTy:
15021518 return typ[1 ][1 ]
15031519 elif typ.typeKind == ntyTypeDesc:
1504- let impl = typ[1 ].getImpl ()
1520+ let impl = getImpl (
1521+ if kind (typ[1 ]) == nnkBracketExpr: typ[1 ][0 ]
1522+ else : typ[1 ]
1523+ )
15051524 if impl[0 ].kind == nnkPragmaExpr:
15061525 return impl[0 ][1 ]
15071526 else :
@@ -1524,14 +1543,12 @@ proc customPragmaNode(n: NimNode): NimNode =
15241543 let name = $ (if n.kind == nnkCheckedFieldExpr: n[0 ][1 ] else : n[1 ])
15251544 let typInst = getTypeInst (if n.kind == nnkCheckedFieldExpr or n[0 ].kind == nnkHiddenDeref: n[0 ][0 ] else : n[0 ])
15261545 var typDef = getImpl (
1527- if typInst.kind == nnkVarTy or
1528- typInst.kind == nnkBracketExpr:
1529- typInst[0 ]
1546+ if typInst.kind in {nnkVarTy, nnkBracketExpr}: typInst[0 ]
15301547 else : typInst
15311548 )
15321549 while typDef != nil :
15331550 typDef.expectKind (nnkTypeDef)
1534- let typ = typDef[2 ]
1551+ let typ = typDef[2 ]. extractTypeImpl ()
15351552 typ.expectKind ({nnkRefTy, nnkPtrTy, nnkObjectTy})
15361553 let isRef = typ.kind in {nnkRefTy, nnkPtrTy}
15371554 if isRef and typ[0 ].kind in {nnkSym, nnkBracketExpr}: # defines ref type for another object(e.g. X = ref X)
0 commit comments