Skip to content

Commit 6d59680

Browse files
authored
don't try to transform objconstr/cast type nodes (#24636)
fixes #24631 [Object constructors](https://github.com/nim-lang/Nim/blob/793baf34ff72cb8c5485ce209af086e27f656853/compiler/semobjconstr.nim#L462), [casts](https://github.com/nim-lang/Nim/blob/793baf34ff72cb8c5485ce209af086e27f656853/compiler/semexprs.nim#L494) and [type conversions](https://github.com/nim-lang/Nim/blob/793baf34ff72cb8c5485ce209af086e27f656853/compiler/semexprs.nim#L419) copy their type nodes verbatim instead of producing semchecked type nodes. This causes a crash in transf when an untyped expression in the type node has `nil` type. To deal with this, don't try to transform the type node in these expressions at all. I couldn't reproduce the problem with type conversion nodes though so those are unchanged in transf.
1 parent 2f402fc commit 6d59680

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

compiler/transf.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ proc transformSons(c: PTransf, n: PNode, noConstFold = false): PNode =
106106
for i in 0..<n.len:
107107
result[i] = transform(c, n[i], noConstFold)
108108
109+
proc transformSonsAfterType(c: PTransf, n: PNode, noConstFold = false): PNode =
110+
result = newTransNode(n)
111+
assert n.len != 0
112+
result[0] = copyTree(n[0])
113+
for i in 1..<n.len:
114+
result[i] = transform(c, n[i], noConstFold)
115+
109116
proc newAsgnStmt(c: PTransf, kind: TNodeKind, le: PNode, ri: PNode; isFirstWrite: bool): PNode =
110117
result = newTransNode(kind, ri.info, 2)
111118
result[0] = le
@@ -1102,6 +1109,9 @@ proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
11021109
result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})
11031110
of nkHiddenStdConv, nkHiddenSubConv, nkConv:
11041111
result = transformConv(c, n)
1112+
of nkObjConstr, nkCast:
1113+
# don't try to transform type node
1114+
result = transformSonsAfterType(c, n)
11051115
of nkDiscardStmt:
11061116
result = n
11071117
if n[0].kind != nkEmpty:

tests/template/tgenericobjconstr.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
discard """
2+
matrix: "--skipParentCfg"
3+
"""
4+
5+
# issue #24631
6+
7+
type
8+
V[d: static bool] = object
9+
l: int
10+
11+
template y(): V[false] = V[false](l: 0)
12+
discard y()
13+
14+
template z(): V[false] = cast[V[false]](V[false](l: 0))
15+
discard z()

0 commit comments

Comments
 (0)