diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2021-06-19 11:24:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-19 11:24:46 -0700 |
commit | 7714ab468a665e772c6d37d39038974cbcea75dc (patch) | |
tree | 3e55551784b56c975d22c3c307301b4abca68110 /lib/std | |
parent | 5d15bd7b618cda06e3ae88883914467f89d38112 (diff) | |
download | Nim-7714ab468a665e772c6d37d39038974cbcea75dc.tar.gz |
make privateAccess work with generic types and generic instantiations; fix a SIGSEGV (#18260)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/std')
-rw-r--r-- | lib/std/importutils.nim | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/lib/std/importutils.nim b/lib/std/importutils.nim index 0c0f546b9..d2da76ea8 100644 --- a/lib/std/importutils.nim +++ b/lib/std/importutils.nim @@ -13,22 +13,32 @@ Possible future APIs: ]# when defined(nimImportutilsExample): - type Foo = object - x1: int # private + type + Foo = object + f0: int # private + Goo*[T] = object + g0: int # private proc initFoo*(): auto = Foo() -proc privateAccess*(t: typedesc[object|(ref object)|(ptr object)]) {.magic: "PrivateAccess".} = +proc privateAccess*(t: typedesc) {.magic: "PrivateAccess".} = ## Enables access to private fields of `t` in current scope. runnableExamples("-d:nimImportutilsExample"): # here we're importing a module containing: - # type Foo = object - # x1: int # private + # type + # Foo = object + # f0: int # private + # Goo*[T] = object + # g0: int # private # proc initFoo*(): auto = Foo() - var a = initFoo() + var f = initFoo() block: - assert not compiles(a.x1) - privateAccess(a.type) - a.x1 = 1 # accessible in this scope + assert not compiles(f.f0) + privateAccess(f.type) + f.f0 = 1 # accessible in this scope block: - assert a.x1 == 1 # still in scope - assert not compiles(a.x1) + assert f.f0 == 1 # still in scope + assert not compiles(f.f0) + + # this also works with generics + privateAccess(Goo) + assert Goo[float](g0: 1).g0 == 1 |