summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-10-24 21:24:51 +0800
committerGitHub <noreply@github.com>2022-10-24 15:24:51 +0200
commit7c2aa53e4423a56ed1bdb349a1248d0e8eb15b39 (patch)
treece4fd67cc7c9ec509c4ef7b2cdb8753e54b006cc
parent878919a4df8b2c82659b60a87f1eb39efb6782e0 (diff)
downloadNim-7c2aa53e4423a56ed1bdb349a1248d0e8eb15b39.tar.gz
fixes #19278; make `privateAccess` work with generic ref object (#20640)
* fixes #19278; make `privateAccess` work with generic ref object

* fixes
-rw-r--r--compiler/suggest.nim4
-rw-r--r--tests/stdlib/mimportutils.nim6
-rw-r--r--tests/stdlib/timportutils.nim11
3 files changed, 18 insertions, 3 deletions
diff --git a/compiler/suggest.nim b/compiler/suggest.nim
index 829335015..41d39ccab 100644
--- a/compiler/suggest.nim
+++ b/compiler/suggest.nim
@@ -265,8 +265,8 @@ proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
       if fmoduleId == module.id: return true
     if f.kind == skField:
       var symObj = f.owner
-      if symObj.typ.kind in {tyRef, tyPtr}:
-        symObj = symObj.typ[0].sym
+      if symObj.typ.skipTypes({tyGenericBody, tyGenericInst, tyGenericInvocation, tyAlias}).kind in {tyRef, tyPtr}:
+        symObj = symObj.typ.toObjectFromRefPtrGeneric.sym
       for scope in allScopes(c.currentScope):
         for sym in scope.allowPrivateAccess:
           if symObj.id == sym.id: return true
diff --git a/tests/stdlib/mimportutils.nim b/tests/stdlib/mimportutils.nim
index e89d58d27..678d9ec02 100644
--- a/tests/stdlib/mimportutils.nim
+++ b/tests/stdlib/mimportutils.nim
@@ -26,6 +26,12 @@ type
   H1*[T] = ref H2[T]
   H*[T] = H1[T]
 
+  Pity[T] = object
+    a: T
+  PityRef*[T] = ref Pity[T]
+  Hope*[T] = ref object
+    a: T
+
 type BAalias* = typeof(B.default)
   # typeof is not a transparent abstraction, creates a `tyAlias`
 
diff --git a/tests/stdlib/timportutils.nim b/tests/stdlib/timportutils.nim
index be912e702..33afd7def 100644
--- a/tests/stdlib/timportutils.nim
+++ b/tests/stdlib/timportutils.nim
@@ -1,4 +1,4 @@
-import std/importutils
+import std/[importutils, assertions]
 import stdtest/testutils
 import mimportutils
 
@@ -134,5 +134,14 @@ template main =
           privateAccess PtA
           a.ha1 == 0
 
+    block:
+      privateAccess PityRef
+      let x = PityRef[int](a: 1)  # works
+      doAssert x.a == 1
+
+      privateAccess Hope
+      let y = Hope[int](a: 1)
+      doAssert y.a == 1
+
 static: main()
 main()