From 598696b5a824ff8ece36f34eaab9de131c0e1e3e Mon Sep 17 00:00:00 2001
From: Clonkk <rf.clonk@linuxmail.org>
Date: Thu, 20 Jan 2022 15:18:53 +0100
Subject: [PATCH 1/3] Apply commit
 https://github.com/nim-lang/Nim/commit/5da931fe811717a45f2dd272ea6281979c3e8f0b
 that was never merged (was part of a bigger PR). Should fix issue #11932

---
 lib/core/macros.nim | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 8ab893d216afa..28106493f2bbb 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -1523,7 +1523,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]

From 37a8870878737460b58664204da027999f1c1120 Mon Sep 17 00:00:00 2001
From: Clonkk <rf.clonk@linuxmail.org>
Date: Thu, 20 Jan 2022 15:37:27 +0100
Subject: [PATCH 2/3] add a generic object for custom pragma

---
 tests/pragmas/tcustom_pragma.nim | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim
index b197a7c55180f..1c3709b269c43 100644
--- a/tests/pragmas/tcustom_pragma.nim
+++ b/tests/pragmas/tcustom_pragma.nim
@@ -17,11 +17,21 @@ 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))
 
+  var ogen: MyGenericObj[int]
+  static:
+    doAssert ogen.myField2.hasCustomPragma(myAttr)
+    doAssert(not ogen.myField1.hasCustomPragma(myAttr))
+
+
 import custom_pragma
 block: # A bit more advanced case
   type

From 67dbf5e8964e5b16b03c61566621b0a7b25f0df2 Mon Sep 17 00:00:00 2001
From: Clonkk <rf.clonk@linuxmail.org>
Date: Tue, 25 Jan 2022 12:04:33 +0100
Subject: [PATCH 3/3] take into account hasCustomPragma with a typedef as input
 which had the same issue in getImpl (not taking nnkBracketExpr into account);
 added tests demonstrating the issue

---
 lib/core/macros.nim              | 5 ++++-
 tests/pragmas/tcustom_pragma.nim | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 28106493f2bbb..fc39499c68f51 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -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:
diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim
index 1c3709b269c43..9f54801b61beb 100644
--- a/tests/pragmas/tcustom_pragma.nim
+++ b/tests/pragmas/tcustom_pragma.nim
@@ -25,11 +25,14 @@ block:
   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