summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorHans Raaf <hara@oderwat.de>2016-08-05 15:17:38 +0200
committerHans Raaf <hara@oderwat.de>2016-08-05 16:01:16 +0200
commite2e4df170281ca663fe023a551e2a97611a9ceca (patch)
tree7d17b005e4f4da516d04ec8d2616a471840a6676 /tests
parent64663387db4a447ee127571b47ef01d937f59785 (diff)
downloadNim-e2e4df170281ca663fe023a551e2a97611a9ceca.tar.gz
Allowing `nil` for distinct types where the base type is nilable
Diffstat (limited to 'tests')
-rw-r--r--tests/distinct/tnil.nim47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/distinct/tnil.nim b/tests/distinct/tnil.nim
new file mode 100644
index 000000000..ed0ac995a
--- /dev/null
+++ b/tests/distinct/tnil.nim
@@ -0,0 +1,47 @@
+discard """
+  file: "tnil.nim"
+  output: '''0x1
+
+nil
+
+nil
+
+'''
+"""
+
+type
+  MyPointer = distinct pointer
+  MyString = distinct string
+  MyStringNotNil = distinct (string not nil)
+  MyInt = distinct int
+
+proc foo(a: MyPointer) =
+  echo a.repr
+
+foo(cast[MyPointer](1))
+foo(cast[MyPointer](nil))
+foo(nil)
+
+var p: MyPointer
+p = cast[MyPointer](1)
+p = cast[MyPointer](nil)
+p = nil.MyPointer
+p = nil
+
+var c: MyString
+c = "Test".MyString
+c = nil.MyString
+c = nil
+
+p = nil
+doAssert(compiles(c = p) == false)
+
+var n: MyStringNotNil = "Test".MyStringNotNil # Cannot prove warning ...
+n = "Test".MyStringNotNil
+doAssert(compiles(n = nil.MyStringNotNil) == false)
+doAssert(compiles(n = nil.MyStringNotNil) == false)
+doAssert(compiles(n = nil) == false)
+
+var i: MyInt
+i = 1.MyInt
+doAssert(compiles(i = nil) == false)