summary refs log tree commit diff stats
path: root/tests/notnil
diff options
context:
space:
mode:
Diffstat (limited to 'tests/notnil')
-rw-r--r--tests/notnil/tmust_compile.nim75
-rw-r--r--tests/notnil/tnotnil.nim13
-rw-r--r--tests/notnil/tnotnil1.nim27
-rw-r--r--tests/notnil/tnotnil2.nim24
-rw-r--r--tests/notnil/tnotnil3.nim35
-rw-r--r--tests/notnil/tnotnil4.nim23
-rw-r--r--tests/notnil/tnotnil5.nim28
-rw-r--r--tests/notnil/tnotnil_in_generic.nim28
-rw-r--r--tests/notnil/tnotnil_in_objconstr.nim18
-rw-r--r--tests/notnil/tparse.nim18
10 files changed, 289 insertions, 0 deletions
diff --git a/tests/notnil/tmust_compile.nim b/tests/notnil/tmust_compile.nim
new file mode 100644
index 000000000..3a013e9ed
--- /dev/null
+++ b/tests/notnil/tmust_compile.nim
@@ -0,0 +1,75 @@
+discard """
+  output: '''success'''
+"""
+
+# bug #6682
+{.experimental: "notnil".}
+
+type
+    Fields = enum
+        A=1, B, C
+
+    Obj = object
+        fld: array[Fields, int]
+
+    AsGeneric[T] = array[Fields, T]
+    Obj2[T] = object
+      fld: AsGeneric[T]
+
+var a: Obj # this works
+
+var arr: array[Fields, int]
+
+var b = Obj() # this doesn't (also doesn't works with additional fields)
+
+var z = Obj2[int]()
+
+echo "success"
+
+# bug #6555
+
+import tables
+
+type
+  TaskOrNil = ref object
+  Task = TaskOrNil not nil
+
+let table = newTable[string, Task]()
+table.del("task")
+
+# bug #6121
+
+import json
+
+type
+
+  foo = object
+    thing: ptr int not nil
+
+  CTS = ref object
+    subs_by_sid: Table[int, foo]
+
+
+proc parse(cts: CTS, jn: JsonNode) =
+  var y = jn.getInt(4523)
+  let ces = foo(
+    thing: addr y
+  )
+
+  cts.subs_by_sid[0] = ces
+
+
+# bug #6489
+
+proc p(x: proc(){.closure.} not nil) = discard
+p(proc(){.closure.} = discard)
+
+# bug #6490
+
+proc p2(a: proc()) =
+    if a.isNil:
+        raise newException(ValueError, "a is nil")
+    else:
+        let b: proc() not nil = a
+
+p2(writeStackTrace)
diff --git a/tests/notnil/tnotnil.nim b/tests/notnil/tnotnil.nim
new file mode 100644
index 000000000..c33b6fcac
--- /dev/null
+++ b/tests/notnil/tnotnil.nim
@@ -0,0 +1,13 @@
+discard """
+  errormsg: "type mismatch"
+  line: 13
+"""
+{.experimental: "notnil".}
+type
+  PObj = ref TObj not nil
+  TObj = object
+    x: int
+
+proc q2(x: string) = discard
+
+q2(nil)
diff --git a/tests/notnil/tnotnil1.nim b/tests/notnil/tnotnil1.nim
new file mode 100644
index 000000000..60666d64d
--- /dev/null
+++ b/tests/notnil/tnotnil1.nim
@@ -0,0 +1,27 @@
+discard """
+  errormsg: "'y' is provably nil"
+  line:25
+"""
+
+import strutils
+{.experimental: "notnil".}
+
+type
+  TObj = object
+    x, y: int
+
+proc q(x: pointer not nil) =
+  discard
+
+proc p() =
+  var x: pointer
+  if not x.isNil:
+    q(x)
+
+  let y = x
+  if not y.isNil:
+    q(y)
+  else:
+    q(y)
+
+p()
diff --git a/tests/notnil/tnotnil2.nim b/tests/notnil/tnotnil2.nim
new file mode 100644
index 000000000..6cd08de73
--- /dev/null
+++ b/tests/notnil/tnotnil2.nim
@@ -0,0 +1,24 @@
+discard """
+  errormsg: "cannot prove 'y' is not nil"
+  line:20
+"""
+
+import strutils
+{.experimental: "notnil".}
+
+type
+  TObj = object
+    x, y: int
+
+proc q(x: pointer not nil) =
+  discard
+
+proc p() =
+  var x: pointer
+  let y = x
+  if not y.isNil or y != x:
+    q(y)
+  else:
+    q(y)
+
+p()
diff --git a/tests/notnil/tnotnil3.nim b/tests/notnil/tnotnil3.nim
new file mode 100644
index 000000000..31a4efef7
--- /dev/null
+++ b/tests/notnil/tnotnil3.nim
@@ -0,0 +1,35 @@
+discard """
+  errormsg: "cannot prove 'variable' is not nil"
+  line: 31
+"""
+
+# bug #584
+# Testprogram for 'not nil' check
+{.experimental: "notnil".}
+const testWithResult = true
+
+type
+  A = object
+  B = object
+  C = object
+    a: ref A
+    b: ref B
+
+
+proc testNotNil(c: ref C not nil) =
+  discard
+
+
+when testWithResult:
+  proc testNotNilOnResult(): ref C =
+    new(result)
+    #result.testNotNil() # Here 'not nil' can't be proved
+
+
+var variable: ref C
+new(variable)
+variable.testNotNil() # Here 'not nil' is proved
+
+when testWithResult:
+  discard testNotNilOnResult()
+
diff --git a/tests/notnil/tnotnil4.nim b/tests/notnil/tnotnil4.nim
new file mode 100644
index 000000000..c5178f71b
--- /dev/null
+++ b/tests/notnil/tnotnil4.nim
@@ -0,0 +1,23 @@
+discard ""
+type
+   TObj = ref object
+
+{.experimental: "notnil".}
+
+proc check(a: TObj not nil) =
+  echo repr(a)
+
+proc doit() =
+   var x : array[0..1, TObj]
+
+   let y = x[0]
+   if y != nil:
+      check(y)
+
+doit()
+
+# bug #2352
+
+proc p(x: proc() {.noconv.} not nil) = discard
+p(proc() {.noconv.} = discard)
+# Error: cannot prove 'proc () {.noconv.} = discard ' is not nil
diff --git a/tests/notnil/tnotnil5.nim b/tests/notnil/tnotnil5.nim
new file mode 100644
index 000000000..2dcb7f7c3
--- /dev/null
+++ b/tests/notnil/tnotnil5.nim
@@ -0,0 +1,28 @@
+discard """
+  matrix: "--threads:on"
+"""
+
+{.experimental: "parallel".}
+{.experimental: "notnil".}
+import threadpool
+
+type
+  AO = object
+    x: int
+
+  A = ref AO not nil
+
+proc process(a: A): A =
+  return A(x: a.x+1)
+
+proc processMany(ayys: openArray[A]): seq[A] =
+  var newAs: seq[FlowVar[A]]
+
+  parallel:
+    for a in ayys:
+      newAs.add(spawn process(a))
+  for newAflow in newAs:
+    let newA = ^newAflow
+    if isNil(newA):
+      return @[]
+    result.add(newA)
diff --git a/tests/notnil/tnotnil_in_generic.nim b/tests/notnil/tnotnil_in_generic.nim
new file mode 100644
index 000000000..89d20f182
--- /dev/null
+++ b/tests/notnil/tnotnil_in_generic.nim
@@ -0,0 +1,28 @@
+discard """
+  errormsg: "cannot prove 'x' is not nil"
+"""
+
+# bug #2216
+{.experimental: "notnil".}
+
+type
+    A[T] = ref object
+        x: int
+        ud: T
+
+proc good[T](p: A[T]) =
+    discard
+
+proc bad[T](p: A[T] not nil) =
+    discard
+
+
+proc go() =
+    let s = A[int](x: 1)
+
+    good(s)
+    bad(s)
+    var x: A[int]
+    bad(x)
+
+go()
diff --git a/tests/notnil/tnotnil_in_objconstr.nim b/tests/notnil/tnotnil_in_objconstr.nim
new file mode 100644
index 000000000..471150f44
--- /dev/null
+++ b/tests/notnil/tnotnil_in_objconstr.nim
@@ -0,0 +1,18 @@
+discard """
+  errormsg: "The Foo type requires the following fields to be initialized: bar, baz"
+  line: "17"
+"""
+{.experimental: "notnil".}
+# bug #2355
+type
+  Base = object of RootObj
+    baz: ref int not nil
+
+  Foo = object of Base
+    foo: ref int
+    bar: ref int not nil
+
+var x: ref int = new(int)
+# Create instance without initializing the `bar` field
+var f = Foo(foo: x)
+echo f.bar.isNil # true
diff --git a/tests/notnil/tparse.nim b/tests/notnil/tparse.nim
new file mode 100644
index 000000000..5c938ff04
--- /dev/null
+++ b/tests/notnil/tparse.nim
@@ -0,0 +1,18 @@
+# issue #16324
+
+{.push experimental: "notnil".}
+
+block:
+  type Foo = ref object
+    value: int
+    
+  proc newFoo1(): Foo not nil =               # This compiles
+    return Foo(value: 1)
+    
+  proc newFoo2(): Foo not nil {.inline.} =    # This does not
+    return Foo(value: 1)
+
+  doAssert newFoo1().value == 1
+  doAssert newFoo2().value == 1
+
+{.pop.}