Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing more nnkBracketExpr not being checked in hasCustomPragma calling getImpl of nnkBracketExpr #19450

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/core/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,10 @@ proc customPragmaNode(n: NimNode): NimNode =
if typ.kind == nnkBracketExpr and typ.len > 1 and typ[1].kind == nnkProcTy:
return typ[1][1]
elif typ.typeKind == ntyTypeDesc:
let impl = typ[1].getImpl()
let impl = getImpl(
if kind(typ[1]) == nnkBracketExpr: typ[1][0]
else: typ[1]
)
if impl[0].kind == nnkPragmaExpr:
return impl[0][1]
else:
Expand All @@ -1523,7 +1526,12 @@ proc customPragmaNode(n: NimNode): NimNode =
if n.kind in {nnkDotExpr, nnkCheckedFieldExpr}:
let name = $(if n.kind == nnkCheckedFieldExpr: n[0][1] else: n[1])
let typInst = getTypeInst(if n.kind == nnkCheckedFieldExpr or n[0].kind == nnkHiddenDeref: n[0][0] else: n[0])
var typDef = getImpl(if typInst.kind == nnkVarTy: typInst[0] else: typInst)
var typDef = getImpl(
if typInst.kind == nnkVarTy or
typInst.kind == nnkBracketExpr:
typInst[0]
else: typInst
)
while typDef != nil:
typDef.expectKind(nnkTypeDef)
let typ = typDef[2]
Expand Down
13 changes: 13 additions & 0 deletions tests/pragmas/tcustom_pragma.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ block:
MyObj = object
myField1, myField2 {.myAttr: "hi".}: int

MyGenericObj[T] = object
myField1, myField2 {.myAttr: "hi".}: int


var o: MyObj
static:
doAssert o.myField2.hasCustomPragma(myAttr)
doAssert(not o.myField1.hasCustomPragma(myAttr))
doAssert(not o.myField1.hasCustomPragma(MyObj))

var ogen: MyGenericObj[int]

static:
doAssert ogen.myField2.hasCustomPragma(myAttr)
doAssert(not ogen.myField1.hasCustomPragma(myAttr))
doAssert(not ogen.myField1.hasCustomPragma(MyGenericObj))


import custom_pragma
block: # A bit more advanced case
Expand Down