mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 19:52:36 +00:00
Fix unsound transform pass (#8633)
When a `var openArray[T]` function parameter goes trough the `transformAddrDeref` pass we may lose the `var` specifier, leading to nasty crashes at runtime.
This commit is contained in:
@@ -1674,6 +1674,14 @@ proc skipStmtList*(n: PNode): PNode =
|
||||
else:
|
||||
result = n
|
||||
|
||||
proc toVar*(typ: PType): PType =
|
||||
## If ``typ`` is not a tyVar then it is converted into a `var <typ>` and
|
||||
## returned. Otherwise ``typ`` is simply returned as-is.
|
||||
result = typ
|
||||
if typ.kind != tyVar:
|
||||
result = newType(tyVar, typ.owner)
|
||||
rawAddSon(result, typ)
|
||||
|
||||
proc toRef*(typ: PType): PType =
|
||||
## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
|
||||
## returned. Otherwise ``typ`` is simply returned as-is.
|
||||
|
||||
@@ -412,6 +412,8 @@ proc debugTree(conf: ConfigRef; n: PNode, indent: int, maxRecDepth: int;
|
||||
else:
|
||||
addf(result, ",$N$1\"ident\": null", [istr])
|
||||
else:
|
||||
if renderType and n.typ != nil:
|
||||
addf(result, ",$N$1\"typ\": $2", [istr, debugType(conf, n.typ, 2)])
|
||||
if sonsLen(n) > 0:
|
||||
addf(result, ",$N$1\"sons\": [", [istr])
|
||||
for i in countup(0, sonsLen(n) - 1):
|
||||
|
||||
@@ -369,6 +369,8 @@ proc transformAddrDeref(c: PTransf, n: PNode, a, b: TNodeKind): PTransNode =
|
||||
result = PTransNode(n.sons[0])
|
||||
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
|
||||
PNode(result).typ = n.typ
|
||||
elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
|
||||
PNode(result).typ = toVar(PNode(result).typ)
|
||||
of nkHiddenStdConv, nkHiddenSubConv, nkConv:
|
||||
var m = n.sons[0].sons[1]
|
||||
if m.kind == a or m.kind == b:
|
||||
@@ -377,6 +379,8 @@ proc transformAddrDeref(c: PTransf, n: PNode, a, b: TNodeKind): PTransNode =
|
||||
result = PTransNode(n.sons[0])
|
||||
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
|
||||
PNode(result).typ = n.typ
|
||||
elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
|
||||
PNode(result).typ = toVar(PNode(result).typ)
|
||||
else:
|
||||
if n.sons[0].kind == a or n.sons[0].kind == b:
|
||||
# addr ( deref ( x )) --> x
|
||||
|
||||
8
tests/ccgbugs/topenarraycast.nim
Normal file
8
tests/ccgbugs/topenarraycast.nim
Normal file
@@ -0,0 +1,8 @@
|
||||
proc foo[T](s: var openArray[T]): T =
|
||||
for x in s: result += x
|
||||
|
||||
proc bar(xyz: var seq[int]) =
|
||||
doAssert 6 == (seq[int](xyz)).foo()
|
||||
|
||||
var t = @[1,2,3]
|
||||
bar(t)
|
||||
Reference in New Issue
Block a user