fixes #19278; make privateAccess work with generic ref object (#20640)

* fixes #19278; make `privateAccess` work with generic ref object

* fixes
This commit is contained in:
ringabout 2022-10-24 21:24:51 +08:00 • committed by GitHub
commit 7c2aa53e44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -265,8 +265,8 @@ proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
if fmoduleId == module.id: return true if fmoduleId == module.id: return true
if f.kind == skField: if f.kind == skField:
var symObj = f.owner var symObj = f.owner
if symObj.typ.kind in {tyRef, tyPtr}: if symObj.typ.skipTypes({tyGenericBody, tyGenericInst, tyGenericInvocation, tyAlias}).kind in {tyRef, tyPtr}:
symObj = symObj.typ[0].sym symObj = symObj.typ.toObjectFromRefPtrGeneric.sym
for scope in allScopes(c.currentScope): for scope in allScopes(c.currentScope):
for sym in scope.allowPrivateAccess: for sym in scope.allowPrivateAccess:
if symObj.id == sym.id: return true if symObj.id == sym.id: return true

View file

@ -26,6 +26,12 @@ type
H1*[T] = ref H2[T] H1*[T] = ref H2[T]
H*[T] = H1[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) type BAalias* = typeof(B.default)
# typeof is not a transparent abstraction, creates a `tyAlias` # typeof is not a transparent abstraction, creates a `tyAlias`

View file

@ -1,4 +1,4 @@
import std/importutils import std/[importutils, assertions]
import stdtest/testutils import stdtest/testutils
import mimportutils import mimportutils
@ -134,5 +134,14 @@ template main =
privateAccess PtA privateAccess PtA
a.ha1 == 0 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() static: main()
main() main()