From f052e251be9194be3e1a9a9a003a3718954360c5 Mon Sep 17 00:00:00 2001 From: cooldome Date: Fri, 20 Nov 2020 13:58:16 +0000 Subject: [PATCH] fix #15958 (#15970) [backport:1.4] * fix #15958 * also cover openArray and VarArgs * more tests * cover even more types * cover even more types * Trigger build * Trigger build * cover sets passed as arrays (cherry picked from commit fcb2ec4ed69fe924e7e3899e0084f534124255d4) --- compiler/ccgtypes.nim | 3 ++- tests/lent/tbasic_lent_check.nim | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 6c82a140beb1..eb22d79e0d4f 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -282,7 +282,8 @@ proc ccgIntroducedPtr(conf: ConfigRef; s: PSym, retType: PType): bool = result = false # first parameter and return type is 'lent T'? --> use pass by pointer if s.position == 0 and retType != nil and retType.kind == tyLent: - result = pt.kind != tyVar + result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or + pt.kind == tySet and mapSetType(conf, pt) == ctArray) proc fillResult(conf: ConfigRef; param: PNode) = fillLoc(param.sym.loc, locParam, param, ~"Result", diff --git a/tests/lent/tbasic_lent_check.nim b/tests/lent/tbasic_lent_check.nim index 4389cbc6e5e6..e22f7bb5088e 100644 --- a/tests/lent/tbasic_lent_check.nim +++ b/tests/lent/tbasic_lent_check.nim @@ -1,4 +1,5 @@ discard """ + targets: "c cpp" output: "1" """ @@ -14,3 +15,41 @@ proc main = doAssert(not compiles(passToVar(viewInto(x)))) main() + + +#------------------------------------------------------------------------------ +# issue #15958 + +block: + proc byLent[T](a: T): lent T = a + let a = [11,12] + let b = @[21,23] + let ss = {1, 2, 3, 5} + doAssert byLent(a) == [11,12] + doAssert byLent(a).unsafeAddr == a.unsafeAddr + doAssert byLent(b) == @[21,23] + doAssert byLent(b).unsafeAddr == b.unsafeAddr + doAssert byLent(ss) == {1, 2, 3, 5} + doAssert byLent(ss).unsafeAddr == ss.unsafeAddr + + let r = new(float) + r[] = 10.0 + doAssert byLent(r)[] == 10.0 + + let p = create(float) + p[] = 20.0 + doAssert byLent(p)[] == 20.0 + + proc byLent2[T](a: openarray[T]): lent T = a[0] + doAssert byLent2(a) == 11 + doAssert byLent2(a).unsafeAddr == a[0].unsafeAddr + doAssert byLent2(b) == 21 + doAssert byLent2(b).unsafeAddr == b[0].unsafeAddr + + proc byLent3[T](a: varargs[T]): lent T = a[1] + let + x = 10 + y = 20 + z = 30 + doAssert byLent3(x, y, z) == 20 +