Skip to content

Commit

Permalink
implement semgnrc for tuple and object type nodes (#22709)
Browse files Browse the repository at this point in the history
fixes #22699
  • Loading branch information
metagn committed Sep 16, 2023
1 parent cd0d0ca commit 8836207
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
43 changes: 41 additions & 2 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,47 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkIdent: a = n[i]
else: illFormedAst(n, c.config)
addDecl(c, newSymS(skUnknown, getIdentNode(c, a), c))
of nkObjectTy, nkTupleTy, nkTupleClassTy:
discard
of nkTupleTy:
for i in 0..<n.len:
var a = n[i]
case a.kind:
of nkCommentStmt, nkNilLit, nkSym, nkEmpty: continue
of nkIdentDefs:
checkMinSonsLen(a, 3, c.config)
a[^2] = semGenericStmt(c, a[^2], flags+{withinTypeDesc}, ctx)
a[^1] = semGenericStmt(c, a[^1], flags, ctx)
for j in 0..<a.len-2:
addTempDecl(c, getIdentNode(c, a[j]), skField)
else:
illFormedAst(a, c.config)
of nkObjectTy:
if n.len > 0:
openScope(c)
for i in 0..<n.len:
result[i] = semGenericStmt(c, n[i], flags, ctx)
closeScope(c)
of nkRecList:
for i in 0..<n.len:
var a = n[i]
case a.kind:
of nkCommentStmt, nkNilLit, nkSym, nkEmpty: continue
of nkIdentDefs:
checkMinSonsLen(a, 3, c.config)
a[^2] = semGenericStmt(c, a[^2], flags+{withinTypeDesc}, ctx)
a[^1] = semGenericStmt(c, a[^1], flags, ctx)
for j in 0..<a.len-2:
addTempDecl(c, getIdentNode(c, a[j]), skField)
of nkRecCase, nkRecWhen:
n[i] = semGenericStmt(c, a, flags, ctx)
else:
illFormedAst(a, c.config)
of nkRecCase:
checkSonsLen(n[0], 3, c.config)
n[0][^2] = semGenericStmt(c, n[0][^2], flags+{withinTypeDesc}, ctx)
n[0][^1] = semGenericStmt(c, n[0][^1], flags, ctx)
addTempDecl(c, getIdentNode(c, n[0][0]), skField)
for i in 1..<n.len:
n[i] = semGenericStmt(c, n[i], flags, ctx)
of nkFormalParams:
checkMinSonsLen(n, 1, c.config)
for i in 1..<n.len:
Expand Down
6 changes: 6 additions & 0 deletions tests/generics/mtypenodes.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# issue #22699

type Private = distinct int

proc chop*[T](x: int): int =
cast[int](cast[tuple[field: Private]](x))
6 changes: 5 additions & 1 deletion tests/generics/timports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ false
'''
"""

import mbind_bracket, mclosed_sym, mdotlookup, mmodule_same_as_proc
import mbind_bracket, mclosed_sym, mdotlookup, mmodule_same_as_proc, mtypenodes


block tbind_bracket:
Expand Down Expand Up @@ -57,3 +57,7 @@ block tmodule_same_as_proc:
proc test[T](t: T) =
mmodule_same_as_proc"a"
test(0)

block ttypenodes:
# issue #22699
doAssert chop[bool](42) == 42

0 comments on commit 8836207

Please sign in to comment.