spec for view types (#15424)
* spec for view types * spec additions * refactoring; there are two different kinds of views * refactorings and spec additions * enforce that view types are initialized * enforce borrowing from the first formal parameter * enforce lifetimes for borrowing of locals * typo in the manual * clarify in the implementation what a borrow operation really is
This commit is contained in:
parent
f8866598e7
commit
4058801607
11 changed files with 446 additions and 66 deletions
|
|
@ -1,17 +1,22 @@
|
|||
discard """
|
||||
errormsg: "cannot borrow"
|
||||
nimout: '''tcannot_borrow.nim(16, 7) Error: cannot borrow meh; what it borrows from is potentially mutated
|
||||
tcannot_borrow.nim(17, 3) the mutation is here
|
||||
tcannot_borrow.nim(16, 7) is the statement that connected the mutation to the parameter'''
|
||||
line: 16
|
||||
nimout: '''tcannot_borrow.nim(21, 7) Error: cannot borrow meh; what it borrows from is potentially mutated
|
||||
tcannot_borrow.nim(22, 3) the mutation is here'''
|
||||
line: 21
|
||||
"""
|
||||
|
||||
|
||||
{.experimental: "views".}
|
||||
|
||||
type
|
||||
Foo = object
|
||||
field: string
|
||||
|
||||
proc valid(s: var seq[Foo]) =
|
||||
let v: lent Foo = s[0] # begin of borrow
|
||||
echo v.field # end of borrow
|
||||
s.setLen 0 # valid because 'v' isn't used afterwards
|
||||
|
||||
proc dangerous(s: var seq[Foo]) =
|
||||
let meh: lent Foo = s[0]
|
||||
s.setLen 0
|
||||
|
|
|
|||
9
tests/views/tmust_borrow_from_first_parameter.nim
Normal file
9
tests/views/tmust_borrow_from_first_parameter.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
discard """
|
||||
errormsg: "'result' must borrow from the first parameter"
|
||||
line: 9
|
||||
"""
|
||||
|
||||
{.experimental: "views".}
|
||||
|
||||
proc p(a, b: openArray[char]): openArray[char] =
|
||||
result = b
|
||||
|
|
@ -23,9 +23,8 @@ proc split*(s: string, seps: set[char] = Whitespace,
|
|||
while last < len(s) and s[last] notin seps:
|
||||
inc(last)
|
||||
if splits == 0: last = len(s)
|
||||
{.noSideEffect.}:
|
||||
result.add toOpenArray(s, first, last-1)
|
||||
result.add toOpenArray(s, first, last-1)
|
||||
result.add toOpenArray(s, first, last-1)
|
||||
result.add toOpenArray(s, first, last-1)
|
||||
if splits == 0: break
|
||||
dec(splits)
|
||||
inc(last)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ discard """
|
|||
33
|
||||
3
|
||||
2
|
||||
3
|
||||
3'''
|
||||
targets: "c cpp"
|
||||
"""
|
||||
|
|
@ -22,6 +23,6 @@ proc main(s: seq[int]) =
|
|||
take(x.toOpenArray(0, 1))
|
||||
let y = x
|
||||
take y
|
||||
|
||||
take x
|
||||
|
||||
main(@[11, 22, 33])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue