Skip to content

Commit

Permalink
[TEST] View type borrow expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Nov 19, 2021
1 parent cbcff7c commit 4c60db7
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
discard """
description: '''
Type inference does not work if rhs is a complex expression
'''
knownIssue: "https://github.com/nim-lang/RFCs/issues/149"
"""

block infer_set_type:
# where this one does work
var y: set[char] = {}

# this one doesn't work (crashes the compiler)
var x: set[char] = (discard 12; {})

# this crashes compiler as well
var q: seq[int] = (discard 1; @[])
1 change: 1 addition & 0 deletions tests/lang/s06_experimental/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Directory contains specifications of the features marked as "experimental".
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
discard """
description: '''
`var openarray` must either generate a proper error message or be allowed,
currently fails with codegen error
'''
knownIssue: "var openarray fails with C codegen errro"
"""

{.experimental: "views".}

## feature-documentation

block view_regular_array:
var simpleArray = @[0, 1, 2, 3, 4, 5]

var view: var openarray[int] = toOpenArray(simpleArray, 1, 3)

doAssert view[0] == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
discard """
description: '''
C codegen error when trying to borrow from the literal. Should be a
nim compilation error.
'''
knownIssue: "codegen-fail on literal value borrow"
"""
{.experimental: "views".}

proc block_trivial_borrow() =
var view: lent int = 12

block_trivial_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory contains tests for the path expression inference.
Each file tests whether original location is sealed correctly and as such
expects compilation error to occur.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
discard """
description: '''
Cannot mutate location of a trivial borrow
'''
errormsg: "'view' borrows from location 'source' which does not live long enough"
"""

{.experimental: "views".}

proc block_trivial_borrow() =
## source itself is a path expression.

var source = 12

var view: var int = source

source = 24

discard view

block_trivial_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
discard """
description: '''
Cannot mutate location of a container access
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
"""

{.experimental: "views".}

proc block_container_borrow() =
## Container access like `e[i]` is a path expression.

var source: seq[int] = @[0, 1, 2]

let idx0 = 1
var view: var int = source[idx0]

let idx1 = 0
source[idx1] = 24

discard view

block_container_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
discard """
description: '''
Cannot mutate location of a container access
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
line: 18
"""

{.experimental: "views".}

proc block_tuple_borrow() =
## Tuple access e[0] is a path expression.

var source: (int, int) = (1, 2)

var view: var int = source[0]

## Location access only tracks single element of a tuple, so it is
## possible to mutate other elements.
source[1] = 200
source[0] = 24

discard view

block_tuple_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
discard """
description: '''
Cannot mutate location of a container access
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
"""

{.experimental: "views".}

type
Object = object
field1: int
field2: int

proc block_trivial_borrow() =
## Object field access e.field is a path expression.

var source = Object()

var view: var int = source.field1

## Path expression tracks only specific fields, so all other ones can
## be modified as needed.
source.field2 = 12
source.field1 = 10

discard view

block_trivial_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
discard """
description: '''
Cannot mutate location of an openarray
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
"""

{.experimental: "views".}

proc block_to_openarray_borrow() =
## `system.toOpenArray(e, ...)` is a path expression.

var source = @[1, 2, 3]

var view: openarray[int] = toOpenArray(source, 0, 1)

source[0] = 12

discard view

block_to_openarray_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
discard """
description: '''
Cannot mutate location created from pointer dereference
'''
errormsg: "'view' borrows from location 'source' which does not live long enough"
"""

{.experimental: "views".}

proc block_pointer_deref_borrow() =
## Pointer dereference `e[]` is a path expression.

var data = 12
var source = addr data

var view: lent int = source[]

source = addr data

discard view

block_pointer_deref_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
discard """
description: '''
addr is not detected as a path expresesion
'''
knownIssue: "addr is not a path expression"
"""


{.experimental: "views".}

proc block_pointer_deref_borrow() =
## An address `addr e`, is a path expression.

var source = 12

var view: ptr lent int = addr source

source = 12

discard view

block_pointer_deref_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
discard """
description: '''
Cannot mutate location of a container access
'''
errormsg: "'view' borrows from location 'source' which does not live long enough"
"""

{.experimental: "views".}

proc block_type_conversion_borrow() =
## Object field access e.field is a path expression.

var source = int16(12)

var view: var int = int32(source)

## Path expression tracks only specific fields, so all other ones can
## be modified as needed.
source = 10

discard view

block_type_conversion_borrow()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
discard """
description: '''
Cannot mutate borrow of the first argument
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
"""

{.experimental: "views".}

proc borrowArg(e: var int): lent int = e

proc block_argument_borrow() =
## source itself is a path expression.

block borrow_arg_call:
## example-explanation

var source = 12

var view: var int = borrowArg(source)

source = 24

discard view

block_argument_borrow()
33 changes: 33 additions & 0 deletions tests/lang/s06_experimental/s01_views/s02_array_views.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
discard """
description: '''
Creating and accessing views for the array types
'''
"""

{.experimental: "views".}

## feature-documentation

proc `sealedBorrow`() =
var simpleArray = [0, 1, 2, 3, 4, 5]

var view: openarray[int] = toOpenArray(simpleArray, 1, 3)

## Openarray can only access a section of the original data, so indexing
## miggh be offset.
doAssert view[0] == 1

## Underlying elements might be modified
simpleArray[1] = 12

## And as a result, view into this section is also affected.
doAssert view[0] == 12

view[2] = 24

doAssert simpleArray[3] == 24

`sealedBorrow`()


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
discard """
description: '''
Cannot mutate sealed location
'''
errormsg: "cannot borrow view; what it borrows from is potentially mutated"
"""

{.experimental: "views".}

proc `sealedBorrow`() =
var simpleArray = [0, 1, 2, 3, 4, 5]

var view: openarray[int] = toOpenArray(simpleArray, 1, 3)

## Openarray can only access a section of the original data, so indexing
## miggh be offset.
doAssert view[0] == 1

## Because `view` is accessed later, it is not possible to mutate original data
## as it would result the change in the borrowed view.
simpleArray[1] = 12

doAssert view[0] == 12


`sealedBorrow`()

0 comments on commit 4c60db7

Please sign in to comment.