fixes #22132; hoisted openArray params result in erroneous code (#22224)

This commit is contained in:
ringabout
2023-07-05 17:21:57 +08:00
committed by GitHub
parent 86ff37fab8
commit 145e002c74
2 changed files with 22 additions and 1 deletions

View File

@@ -1072,7 +1072,10 @@ proc moveOrCopy(dest, ri: PNode; c: var Con; s: var Scope, flags: set[MoveOrCopy
if sameLocation(dest, ri):
# rule (self-assignment-removal):
result = newNodeI(nkEmpty, dest.info)
elif isCursor(dest):
elif isCursor(dest) or dest.typ.kind in {tyOpenArray, tyVarargs}:
# hoisted openArray parameters might end up here
# openArray types don't have a lifted assignment operation (it's empty)
# bug #22132
case ri.kind:
of nkStmtListExpr, nkBlockExpr, nkIfExpr, nkCaseStmt, nkTryStmt:
template process(child, s): untyped = moveOrCopy(dest, child, c, s, flags)

View File

@@ -50,3 +50,21 @@ proc f(a: var string) =
var a = "Hello"
f(a)
doAssert a == "Hallo"
# bug #22132
block:
func foo[T](arr: openArray[T], idx: int = arr.low): string =
doAssert idx == 0
return $arr
let bug = ["0", "c", "a"]
let str = foo(bug)
const expected = """["0", "c", "a"]"""
doAssert str == expected
const noBugConst = ["0", "c", "a"]
doAssert foo(noBugConst) == expected
let noBugSeq = @["0", "c", "a"]
doAssert foo(noBugSeq) == expected