From b87fd80b0f1a4c3c3961e61fbea177ded22b34e0 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 3 Mar 2022 17:36:16 +0200 Subject: [PATCH] Add tests for recordFields being used with empty objects --- stew/shims/macros.nim | 10 +++++++--- tests/test_macros.nim | 10 +++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/stew/shims/macros.nim b/stew/shims/macros.nim index c26439c..4958bc6 100644 --- a/stew/shims/macros.nim +++ b/stew/shims/macros.nim @@ -76,9 +76,13 @@ macro isTuple*(T: type): untyped = proc skipRef*(T: NimNode): NimNode = result = T - if T.kind == nnkBracketExpr: - if eqIdent(T[0], bindSym"ref"): - result = T[1] + if T.kind == nnkBracketExpr and eqIdent(T[0], "ref"): + result = T[1] + +proc skipPtr*(T: NimNode): NimNode = + result = T + if T.kind == nnkBracketExpr and eqIdent(T[0], "ptr"): + result = T[1] template readPragma*(field: FieldDescription, pragmaName: static string): NimNode = let p = findPragma(field.pragmas, bindSym(pragmaName)) diff --git a/tests/test_macros.nim b/tests/test_macros.nim index 1565194..70166a2 100644 --- a/tests/test_macros.nim +++ b/tests/test_macros.nim @@ -36,10 +36,14 @@ type DerivedFromRefType = ref object of DerivedType anotherDerivedField: string + EmptyObject = object + EmptyRefObject = ref object + EmptyPtrObject = ptr object + macro getFieldsLists(T: type): untyped = result = newTree(nnkBracket) - var resolvedType = skipRef getType(T)[1] + var resolvedType = skipPtr skipRef getType(T)[1] doAssert resolvedType.kind == nnkSym var objectType = getImpl(resolvedType) doAssert objectType.kind == nnkTypeDef @@ -56,6 +60,10 @@ static: "anotherDerivedField" ] + doAssert getFieldsLists(EmptyObject).len == 0 + doAssert getFieldsLists(EmptyRefObject).len == 0 + doAssert getFieldsLists(EmptyPtrObject).len == 0 + let myType = MyType[string](myField: "test", myGeneric: "test", kind: true, first: "test") suite "Macros":