diff options
Diffstat (limited to 'tests/views')
-rw-r--r-- | tests/views/t19986.nim | 42 | ||||
-rw-r--r-- | tests/views/tcannot_borrow.nim | 5 | ||||
-rw-r--r-- | tests/views/tconst_views.nim | 11 | ||||
-rw-r--r-- | tests/views/tdont_mutate.nim | 11 | ||||
-rw-r--r-- | tests/views/tviews1.nim | 110 | ||||
-rw-r--r-- | tests/views/tviews2.nim | 90 |
6 files changed, 259 insertions, 10 deletions
diff --git a/tests/views/t19986.nim b/tests/views/t19986.nim new file mode 100644 index 000000000..85a7cf97d --- /dev/null +++ b/tests/views/t19986.nim @@ -0,0 +1,42 @@ +discard """ + cmd: '''nim check --hints:off $file''' + action: reject +nimout: ''' +t19986.nim(19, 7) Error: 'foo' borrows from the immutable location 'a' and attempts to mutate it +t19986.nim(28, 7) Error: 'foo' borrows from the immutable location 'a' and attempts to mutate it +t19986.nim(37, 7) Error: 'foo' borrows from the immutable location 'a' and attempts to mutate it +''' +""" + +{.experimental: "views".} + +type + Object = object + id: int + +proc foo() = + let a = Object(id: 3) + var foo: var Object = a + + foo.id = 777 + echo a + +foo() + +proc bar() = + let a = "123" + var foo: var string = a + + foo[0] = '7' + echo a + +bar() + +proc main() = + let a = 3 + var foo: var int = a + + foo = 777 + echo a + +main() diff --git a/tests/views/tcannot_borrow.nim b/tests/views/tcannot_borrow.nim index d1c194b25..0b8793159 100644 --- a/tests/views/tcannot_borrow.nim +++ b/tests/views/tcannot_borrow.nim @@ -1,8 +1,7 @@ discard """ errormsg: "cannot borrow" - 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 + nimout: '''tcannot_borrow.nim(20, 7) Error: cannot borrow meh; what it borrows from is potentially mutated +tcannot_borrow.nim(21, 3) the mutation is here''' """ diff --git a/tests/views/tconst_views.nim b/tests/views/tconst_views.nim index d7f1fc481..a85b03864 100644 --- a/tests/views/tconst_views.nim +++ b/tests/views/tconst_views.nim @@ -24,3 +24,14 @@ proc `$`(x: openArray[int]): string = echo c echo c2.data + +type MyObj = object + data: openarray[char] + +const + val1 = Foo(data: toOpenArray([1, 2, 3], 1, 1)) + val2 = Foo(data: toOpenArray([1, 2, 3], 0, 2)) + val3 = MyObj(data: "Hello".toOpenArray(0, 2)) +assert val1.data == [2] +assert val2.data == [1, 2, 3] +assert val3.data == "Hel" diff --git a/tests/views/tdont_mutate.nim b/tests/views/tdont_mutate.nim index d243c7c7a..eb5a82cbf 100644 --- a/tests/views/tdont_mutate.nim +++ b/tests/views/tdont_mutate.nim @@ -9,8 +9,9 @@ import tables const Whitespace = {' ', '\t', '\n', '\r'} -proc split*(s: string, seps: set[char] = Whitespace, - maxsplit: int = -1): Table[int, openArray[char]] = +proc split*(s: string, seps: set[char] = Whitespace, maxsplit: int = -1): Table[int, openArray[char]] #[tt.Error +^ 'result' borrows from the immutable location 's' and attempts to mutate it + ]# = var last = 0 var splits = maxsplit result = initTable[int, openArray[char]]() @@ -22,9 +23,7 @@ proc split*(s: string, seps: set[char] = Whitespace, if splits == 0: last = len(s) result[first] = toOpenArray(s, first, last-1) - result[first][0] = 'c' #[tt.Error - attempt to mutate a borrowed location from an immutable view - ]# + result[first][0] = 'c' if splits == 0: break dec(splits) @@ -36,7 +35,7 @@ proc `$`(x: openArray[char]): string = proc otherTest(x: int) = var y: var int = x #[tt.Error - 'y' borrows from the immutable location 'x' and attempts to mutate it + ^ 'y' borrows from the immutable location 'x' and attempts to mutate it ]# y = 3 diff --git a/tests/views/tviews1.nim b/tests/views/tviews1.nim index 51f17b9d6..9785d25e5 100644 --- a/tests/views/tviews1.nim +++ b/tests/views/tviews1.nim @@ -5,7 +5,9 @@ discard """ 3 2 3 -3''' +3 +15 +(oa: [1, 3, 4])''' targets: "c cpp" """ @@ -26,3 +28,109 @@ proc main(s: seq[int]) = take x main(@[11, 22, 33]) + +var x: int + +proc foo(x: var int): var int = + once: x = 42 + return x + +var y: var int = foo(x) +y = 15 +echo foo(x) +# bug #16132 + +# bug #18690 + +type + F = object + oa: openArray[int] + +let s1 = @[1,3,4,5,6] +var test = F(oa: toOpenArray(s1, 0, 2)) +echo test + +type + Foo = object + x: string + y: seq[int] + data: array[10000, byte] + + View[T] = object + x: lent T + +proc mainB = + let f = Foo(y: @[1, 2, 3]) + let foo = View[Foo](x: f) + assert foo.x.x == "" + assert foo.x.y == @[1, 2, 3] + +mainB() + + +# bug #15897 +type Outer = ref object + value: int +type Inner = object + owner: var Outer + +var o = Outer(value: 1234) +var v = Inner(owner: o).owner.value +doAssert v == 1234 + +block: # bug #21674 + type + Lent = object + data: lent int + + proc foo(s: Lent) = + var m = 12 + discard cast[lent int](m) + + proc main = + var m1 = 123 + var x = Lent(data: m1) + foo(x) + + main() + +block: # bug #22117 + proc main: int = + var a = 10 + defer: discard a # extend a's lifetime + + var aref: var int = a + #└──── 'aref' borrows from location 'a' which does not live long enough + + result = aref + + doAssert main() == 10 + +type + Slice*[T] = object + first, last: int + p: ptr UncheckedArray[T] + +var i = 0 + +converter autoToOpenArray*[T](s: Slice[T]): openArray[T] = + inc i + result = toOpenArray(s.p, s.first, s.last) + +proc acceptOpenArray(s: openArray[byte]) = discard + +proc bug22597 = # bug #22597 + acceptOpenArray(Slice[byte]()) + doAssert i == 1 + +bug22597() + +block: # bug #20048 + type + Test = object + tokens: openArray[string] + + func init(Self: typedesc[Test], tokens: openArray[string]): Self = Self(tokens: tokens) + + let data = Test.init(["123"]) + doAssert @(data.tokens) == @["123"] diff --git a/tests/views/tviews2.nim b/tests/views/tviews2.nim new file mode 100644 index 000000000..29aff76df --- /dev/null +++ b/tests/views/tviews2.nim @@ -0,0 +1,90 @@ +discard """ + targets: "c js" +""" + +{.experimental: "views".} + +block: + type + Foo = object + id: openArray[char] + + proc foo(): Foo = + var source = "1245" + result = Foo(id: source.toOpenArray(0, 1)) + + doAssert foo().id == @['1', '2'] + +block: # bug #15778 + type + Reader = object + data: openArray[char] + current: int + + var count = 0 + + proc read(data: var Reader, length: int): openArray[char] = + inc count + let start = data.current + data.current.inc length + return data.data.toOpenArray(start, data.current-1) + + var data = "hello there" + var reader = Reader(data: data.toOpenArray(0, data.len-1), current: 0) + doAssert @(reader.read(2)) == @['h', 'e'] + doAssert @(reader.read(3)) == @['l', 'l', 'o'] + doAssert count == 2 + +block: # bug #16671 + block: + type X = ref object of RootObj + type Y = ref object of X + field: openArray[int] + + var s: seq[X] + proc f() = + s.add(Y(field: [1])) + + f() + + block: + type X = ref object of RootObj + type Y = ref object of X + field: openArray[int] + + var s: seq[X] + proc f() = + s.add(Y(field: toOpenArray([1, 2, 3], 0, 1))) + + f() + +block: # bug #15746 + type + Reader = object + data: openArray[char] + current: int + + proc initReader(data: openArray[char], offset = 0): Reader = + result = Reader(data: data, current: offset) + + let s = "\x01\x00\x00\x00" + doAssert initReader(s).data[0].int == 1 + +block: + proc foo(x: openArray[char]) = + discard x + + foo("12254") + foo(@['a', 'b']) + + var a1 = "12254" + foo(a1) + + var a2 = @['a', 'b'] + foo(a2) + + var s = "138443" + var ooo: openArray[char] = s + var xxx: openArray[char] = ooo + foo(ooo) + foo(xxx) |