diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-06-02 21:15:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-02 15:15:31 +0200 |
commit | a9a32ca3b8ce96c3761a2961f5997cf9e4234ba5 (patch) | |
tree | 6e2a2c996e86631fa4b040ecc46fbcaaa4d9f6b7 | |
parent | cb0ebecb2045143f71b7be40b853672a987fa4d1 (diff) | |
download | Nim-a9a32ca3b8ce96c3761a2961f5997cf9e4234ba5.tar.gz |
improve view types for jsgen; eliminate unnecessary copies of view types (#23654)
-rw-r--r-- | compiler/jsgen.nim | 4 | ||||
-rw-r--r-- | tests/views/tviews2.nim | 19 |
2 files changed, 21 insertions, 2 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 0cc052b38..382f12a9d 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1268,7 +1268,7 @@ proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) = lineF(p, "$1 = nimCopy(null, $2, $3);$n", [a.rdLoc, b.res, genTypeInfo(p, y.typ)]) of etyObject: - if x.typ.kind in {tyVar, tyLent} or (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded: + if x.typ.kind in {tyVar, tyLent, tyOpenArray, tyVarargs} or (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded: lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc]) else: useMagic(p, "nimCopy") @@ -2018,7 +2018,7 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) = gen(p, n, a) case mapType(p, v.typ) of etyObject, etySeq: - if needsNoCopy(p, n): + if v.typ.kind in {tyOpenArray, tyVarargs} or needsNoCopy(p, n): s = a.res else: useMagic(p, "nimCopy") diff --git a/tests/views/tviews2.nim b/tests/views/tviews2.nim index 56f5a732d..e06cc2d53 100644 --- a/tests/views/tviews2.nim +++ b/tests/views/tviews2.nim @@ -57,3 +57,22 @@ block: # bug #16671 s.add(Y(field: toOpenArray([1, 2, 3], 0, 1))) f() + +block: + proc foo(x: openArray[char]) = + discard x + + foo("12254") + foo(@['a', 'b']) + + var a1 = "12254" + foo(a1) + + var a2 = @['a', 'b'] + foo(a2) + + var s = "138443" + var ooo: openArray[char] = s + var xxx: openArray[char] = ooo + foo(ooo) + foo(xxx) |