Skip to content

Commit

Permalink
fix nil node in sym ast of exported ref objects [backport:2.2] (#24527)
Browse files Browse the repository at this point in the history
fixes #24526, follows up #23101

The `shallowCopy` calls do not keep the original node's children, they
just make a new seq with the same length, so the `Ident "*"` node from
the original postfix nodes was not carried over, making it `nil` and
causing the segfault.
  • Loading branch information
metagn authored Dec 10, 2024
1 parent aeb3fe9 commit b529f69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1698,12 +1698,14 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
obj.ast[0] = a[0].shallowCopy
if a[0][0].kind == nkPostfix:
obj.ast[0][0] = a[0][0].shallowCopy
obj.ast[0][0][0] = a[0][0][0] # ident "*"
obj.ast[0][0][1] = symNode
else:
obj.ast[0][0] = symNode
obj.ast[0][1] = a[0][1]
of nkPostfix:
obj.ast[0] = a[0].shallowCopy
obj.ast[0][0] = a[0][0] # ident "*"
obj.ast[0][1] = symNode
else: assert(false)
obj.ast[1] = a[1]
Expand Down
24 changes: 24 additions & 0 deletions tests/pragmas/tpostfixcustompragma.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# issue #24526

import std/macros

template prag {.pragma.}

type
Foo1* = ref object of RootObj
name*: string
Foo2* {.used.} = ref object of RootObj
name*: string
Foo3* {.prag.} = ref object of RootObj
name*: string
Foo4* {.used, prag.} = ref object of RootObj
name*: string

# needs to have `typedesc` type
proc foo(T: typedesc): bool =
T.hasCustomPragma(prag)

doAssert not foo(typeof(Foo1()[]))
doAssert not foo(typeof(Foo2()[]))
doAssert foo(typeof(Foo3()[]))
doAssert foo(typeof(Foo4()[]))

0 comments on commit b529f69

Please sign in to comment.