Skip to content

Commit

Permalink
retain postfix node in type section typed AST (#23096)
Browse files Browse the repository at this point in the history
fixes #22933
  • Loading branch information
metagn authored Dec 18, 2023
1 parent 0613537 commit d3b9711
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
22 changes: 19 additions & 3 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,9 @@ proc typeSectionTypeName(c: PContext; n: PNode): PNode =
result = n[0]
else:
result = n
if result.kind == nkPostfix:
if result.len != 2: illFormedAst(n, c.config)
result = result[1]
if result.kind != nkSym: illFormedAst(n, c.config)

proc typeDefLeftSidePass(c: PContext, typeSection: PNode, i: int) =
Expand Down Expand Up @@ -1326,9 +1329,15 @@ proc typeDefLeftSidePass(c: PContext, typeSection: PNode, i: int) =
elif s.owner == nil: s.owner = getCurrOwner(c)

if name.kind == nkPragmaExpr:
typeDef[0][0] = newSymNode(s)
if name[0].kind == nkPostfix:
typeDef[0][0][1] = newSymNode(s)
else:
typeDef[0][0] = newSymNode(s)
else:
typeDef[0] = newSymNode(s)
if name.kind == nkPostfix:
typeDef[0][1] = newSymNode(s)
else:
typeDef[0] = newSymNode(s)

proc typeSectionLeftSidePass(c: PContext, n: PNode) =
# process the symbols on the left side for the whole type section, before
Expand Down Expand Up @@ -1538,8 +1547,15 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
of nkSym: obj.ast[0] = symNode
of nkPragmaExpr:
obj.ast[0] = a[0].shallowCopy
obj.ast[0][0] = symNode
if a[0][0].kind == nkPostfix:
obj.ast[0][0] = a[0][0].shallowCopy
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][1] = symNode
else: assert(false)
obj.ast[1] = a[1]
obj.ast[2] = a[2][0]
Expand Down
3 changes: 2 additions & 1 deletion testament/important_packages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ pkg "memo"
pkg "msgpack4nim", "nim c -r tests/test_spec.nim"
pkg "nake", "nim c nakefile.nim"
pkg "neo", "nim c -d:blas=openblas --mm:refc tests/all.nim", url = "https://github.com/nim-lang/neo"
pkg "nesm", "nimble tests", "https://github.com/nim-lang/NESM", useHead = true
pkg "nesm", "nimble tests", "https://github.com/nim-lang/NESM", useHead = true, allowFailure = true
# inactive, tests not adapted to #23096
pkg "netty"
pkg "nico", allowFailure = true
pkg "nicy", "nim c -r src/nicy.nim"
Expand Down
5 changes: 5 additions & 0 deletions tests/macros/tastrepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ for i, (x, y) in pairs(data):
var
a = 1
b = 2
type
A* = object
var data = @[(1, "one"), (2, "two")]
for (i, d) in pairs(data):
Expand All @@ -20,6 +22,8 @@ for i, d in pairs(data):
for i, (x, y) in pairs(data):
discard
var (a, b) = (1, 2)
type
A* = object
'''
"""

Expand All @@ -44,3 +48,4 @@ echoTypedAndUntypedRepr:
for i, (x,y) in pairs(data):
discard
var (a,b) = (1,2)
type A* = object # issue #22933
4 changes: 3 additions & 1 deletion tests/macros/tgetimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ assert: check_gen_proc(len(a)) == (false, true)
macro check(x: type): untyped =
let z = getType(x)
let y = getImpl(z[1])
let sym = if y[0].kind == nnkSym: y[0] else: y[0][0]
var sym = y[0]
if sym.kind == nnkPragmaExpr: sym = sym[0]
if sym.kind == nnkPostfix: sym = sym[1]
expectKind(z[1], nnkSym)
expectKind(sym, nnkSym)
expectKind(y[2], nnkObjectTy)
Expand Down

0 comments on commit d3b9711

Please sign in to comment.