Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #15958 #15970

Merged
merged 9 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ 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 = pt.kind notin {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer}

proc fillResult(conf: ConfigRef; param: PNode) =
fillLoc(param.sym.loc, locParam, param, ~"Result",
Expand Down
36 changes: 36 additions & 0 deletions tests/lent/tbasic_lent_check.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
discard """
targets: "c cpp"
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
output: "1"
"""

Expand All @@ -14,3 +15,38 @@ proc main =
doAssert(not compiles(passToVar(viewInto(x))))

main()

timotheecour marked this conversation as resolved.
Show resolved Hide resolved

#------------------------------------------------------------------------------
# issue #15958

block:
proc byLent[T](a: T): lent T = a
let a = [11,12]
let b = @[21,23]
doAssert byLent(a) == [11,12]
doAssert byLent(a).unsafeAddr == a.unsafeAddr
doAssert byLent(b) == @[21,23]
doAssert byLent(b).unsafeAddr == b.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
timotheecour marked this conversation as resolved.
Show resolved Hide resolved