diff --git a/compiler/transf.nim b/compiler/transf.nim index 3db861481a7f..bd1a9cc4ca1f 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -637,6 +637,12 @@ proc putArgInto(arg: PNode, formal: PType): TPutArgInto = case arg.kind of nkStmtListExpr: return paComplexOpenarray + of nkCall: + if skipTypes(arg.typ, abstractInst).kind in {tyOpenArray, tyVarargs}: + # XXX incorrect, causes #13417 when `arg` has side effects. + return paDirectMapping + else: + return paComplexOpenarray of nkBracket: return paFastAsgnTakeTypeFromArg else: @@ -803,7 +809,7 @@ proc transformFor(c: PTransf, n: PNode): PNode = stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, addrExp, true)) newC.mapping[formal.itemId] = newDeref(temp) of paComplexOpenarray: - # arrays will deep copy here (pretty bad). + # XXX arrays will deep copy here (pretty bad). var temp = newTemp(c, arg.typ, formal.info) addVar(v, temp) stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, arg, true)) diff --git a/tests/iter/titeropenarray.nim b/tests/iter/titeropenarray.nim new file mode 100644 index 000000000000..918195b16168 --- /dev/null +++ b/tests/iter/titeropenarray.nim @@ -0,0 +1,32 @@ +block: # issue #13417 + var s: seq[int] = @[] + proc p1(): seq[int] = + s.add(3) + @[1,2] + + iterator ip1(v: openArray[int]): auto = + for x in v: + yield x + + for x in ip1(p1()): + s.add(x) + + doAssert s == @[3, 1, 2] + +import std / sequtils + +block: # issue #19703 + iterator combinations[T](s: seq[T], r: Positive): seq[T] = + yield @[s[0], s[1]] + + iterator pairwise[T](s: openArray[T]): seq[T] = + yield @[s[0], s[0]] + + proc checkSpecialSubset5(s: seq[int]): bool = + toSeq( + toSeq( + s.combinations(2) + ).map(proc(a: auto): int = a[0]).pairwise() + ).any(proc(a: auto): bool = a == @[s[0], s[0]]) + + doAssert checkSpecialSubset5 @[1, 2]