Skip to content

Commit 486cb09

Browse files
authored
Clonkk fix2 11923 (#19451)
* fix nnkBracketExpr not compiling for getImpl on customPragmaNode * fix test import * fix alias not working with hasCustomPragmas
1 parent 1830a3b commit 486cb09

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/core/macros.nim

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
14961512
proc 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)

tests/pragmas/tcustom_pragma.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ block:
2020
MyGenericObj[T] = object
2121
myField1, myField2 {.myAttr: "hi".}: int
2222

23+
MyOtherObj = MyObj
24+
2325

2426
var o: MyObj
2527
static:
2628
doAssert o.myField2.hasCustomPragma(myAttr)
2729
doAssert(not o.myField1.hasCustomPragma(myAttr))
30+
doAssert(not o.myField1.hasCustomPragma(MyObj))
31+
doAssert(not o.myField1.hasCustomPragma(MyOtherObj))
2832

2933
var ogen: MyGenericObj[int]
3034
static:
3135
doAssert ogen.myField2.hasCustomPragma(myAttr)
3236
doAssert(not ogen.myField1.hasCustomPragma(myAttr))
37+
doAssert(not ogen.myField1.hasCustomPragma(MyGenericObj))
38+
doAssert(not ogen.myField1.hasCustomPragma(MyGenericObj))
3339

3440

3541
import custom_pragma

0 commit comments

Comments
 (0)