From 34029263725ef931863ea73966dc08588758dada Mon Sep 17 00:00:00 2001 From: RSDuck Date: Thu, 19 Apr 2018 11:07:45 +0200 Subject: [PATCH] hasCustomPragma/getCustomPragmaVal: small fix (#7650) * fix hasCustomPragma/getCustomPragmaVal for types without pragma * fix pragma on pointer test * removed trailing spaces --- lib/core/macros.nim | 6 +++++- tests/pragmas/tcustom_pragma.nim | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index b050eb6c88..1dc067e1ac 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -1288,7 +1288,11 @@ proc customPragmaNode(n: NimNode): NimNode = typ = n.getTypeInst() if typ.typeKind == ntyTypeDesc: - return typ[1].getImpl()[0][1] + let impl = typ[1].getImpl() + if impl[0].kind == nnkPragmaExpr: + return impl[0][1] + else: + return impl[0] # handle types which don't have macro at all if n.kind == nnkSym: # either an variable or a proc let impl = n.getImpl() diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim index 28a8713ce6..33a4a7e65e 100644 --- a/tests/pragmas/tcustom_pragma.nim +++ b/tests/pragmas/tcustom_pragma.nim @@ -51,6 +51,9 @@ block: # A bit more advanced case static: assert(hasCustomPragma(myproc, alternativeKey)) + const hasFieldCustomPragma = s.field.hasCustomPragma(defaultValue) + static: assert(hasFieldCustomPragma == false) + # pragma on an object static: assert Subfield.hasCustomPragma(defaultValue) @@ -71,6 +74,8 @@ block: # ref types MyFile {.defaultValue: "closed".} = ref object path {.defaultValue: "invalid".}: string + TypeWithoutPragma = object + var s = NodeRef() const @@ -91,7 +96,7 @@ block: # ref types var ptrS = NodePtr(nil) const - ptrRightSerKey = getCustomPragmaVal(s.right, serializationKey) + ptrRightSerKey = getCustomPragmaVal(ptrS.right, serializationKey) static: assert ptrRightSerKey == "r" @@ -103,6 +108,9 @@ block: # ref types assert fileDefVal == "closed" assert filePathDefVal == "invalid" + static: + assert TypeWithoutPragma.hasCustomPragma(defaultValue) == false + block: type VariantKind = enum