forked from nim-works/nimskull
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbcff7c
commit 4c60db7
Showing
17 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
16 changes: 16 additions & 0 deletions
16
tests/lang/s01_basics/s00_atoms/t04_type_inference_fail_colon_expr.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; @[]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Directory contains specifications of the features marked as "experimental". |
19 changes: 19 additions & 0 deletions
19
tests/lang/s06_experimental/s01_views/s00_bugs/t01_var_openarray.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
14 changes: 14 additions & 0 deletions
14
tests/lang/s06_experimental/s01_views/s00_bugs/t02_borrow_literal.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
3 changes: 3 additions & 0 deletions
3
tests/lang/s06_experimental/s01_views/s01_path_expressions/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
21 changes: 21 additions & 0 deletions
21
tests/lang/s06_experimental/s01_views/s01_path_expressions/t01_trivial_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
23 changes: 23 additions & 0 deletions
23
tests/lang/s06_experimental/s01_views/s01_path_expressions/t02_container_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
25 changes: 25 additions & 0 deletions
25
tests/lang/s06_experimental/s01_views/s01_path_expressions/t03_tuple_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
29 changes: 29 additions & 0 deletions
29
tests/lang/s06_experimental/s01_views/s01_path_expressions/t04_field_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
21 changes: 21 additions & 0 deletions
21
tests/lang/s06_experimental/s01_views/s01_path_expressions/t05_to_openarray_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
22 changes: 22 additions & 0 deletions
22
tests/lang/s06_experimental/s01_views/s01_path_expressions/t06_pointer_deref_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
23 changes: 23 additions & 0 deletions
23
tests/lang/s06_experimental/s01_views/s01_path_expressions/t07_addr_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
23 changes: 23 additions & 0 deletions
23
.../lang/s06_experimental/s01_views/s01_path_expressions/t08_type_conversion_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
26 changes: 26 additions & 0 deletions
26
tests/lang/s06_experimental/s01_views/s01_path_expressions/t09_proc_param_borrow_fail.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`() | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
tests/lang/s06_experimental/s01_views/t01_array_fail_sealed_borrow.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`() |