diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/errmsgs/t5167_1.nim | 17 | ||||
-rw-r--r-- | tests/errmsgs/t5167_2.nim | 12 | ||||
-rw-r--r-- | tests/errmsgs/t5167_3.nim | 25 | ||||
-rw-r--r-- | tests/errmsgs/t5167_4.nim | 20 | ||||
-rw-r--r-- | tests/errmsgs/t5167_5.nim | 25 | ||||
-rw-r--r-- | tests/errmsgs/tnon_concrete_cast.nim | 47 | ||||
-rw-r--r-- | tests/macros/tmacro4.nim | 2 | ||||
-rw-r--r-- | tests/macros/tquotewords.nim | 2 | ||||
-rw-r--r-- | tests/overload/tparam_forwarding.nim | 37 | ||||
-rw-r--r-- | tests/stdlib/tmemfiles2.nim | 2 |
10 files changed, 186 insertions, 3 deletions
diff --git a/tests/errmsgs/t5167_1.nim b/tests/errmsgs/t5167_1.nim new file mode 100644 index 000000000..9f4f208a4 --- /dev/null +++ b/tests/errmsgs/t5167_1.nim @@ -0,0 +1,17 @@ +discard """ +errormsg: "'bar' doesn't have a concrete type, due to unspecified generic parameters." +line: 16 +""" + +proc foo[T]() = + var y1 = foo[string] + var y2 = foo[T] + +proc bar[T]() = + let x = 0 + +let good1 = foo[int] +let good2 = bar[int] + +let err = bar + diff --git a/tests/errmsgs/t5167_2.nim b/tests/errmsgs/t5167_2.nim new file mode 100644 index 000000000..17d96ef47 --- /dev/null +++ b/tests/errmsgs/t5167_2.nim @@ -0,0 +1,12 @@ +discard """ +cmd: "nim c --threads:on $file" +errormsg: "'threadFunc' doesn't have a concrete type, due to unspecified generic parameters." +line: 11 +""" + +proc threadFunc[T]() {.thread.} = + let x = 0 + +var thr: Thread[void] +thr.createThread(threadFunc) + diff --git a/tests/errmsgs/t5167_3.nim b/tests/errmsgs/t5167_3.nim new file mode 100644 index 000000000..2781d3943 --- /dev/null +++ b/tests/errmsgs/t5167_3.nim @@ -0,0 +1,25 @@ +discard """ +cmd: "nim c --threads:on $file" +errormsg: "type mismatch" +line: 24 +""" + +type + TGeneric[T] = object + x: int + +proc foo1[A, B, C, D](x: proc (a: A, b: B, c: C, d: D)) = + echo "foo1" + +proc foo2(x: proc(x: int)) = + echo "foo2" + +# The goal of this test is to verify that none of the generic parameters of the +# proc will be marked as unused. The error message should be "type mismatch" instead +# of "'bar' doesn't have a concrete type, due to unspecified generic parameters". +proc bar[A, B, C, D](x: A, y: seq[B], z: array[4, TGeneric[C]], r: TGeneric[D]) = + echo "bar" + +foo1[int, seq[int], array[4, TGeneric[float]], TGeneric[string]] bar +foo2 bar + diff --git a/tests/errmsgs/t5167_4.nim b/tests/errmsgs/t5167_4.nim new file mode 100644 index 000000000..3d77fae02 --- /dev/null +++ b/tests/errmsgs/t5167_4.nim @@ -0,0 +1,20 @@ +discard """ +errormsg: "type mismatch: got (proc [*missing parameters*](x: int) | proc (x: string){.gcsafe, locks: 0.})" +line: 19 +""" + +type + TGeneric[T] = object + x: int + +proc foo[B](x: int) = + echo "foo1" + +proc foo(x: string) = + echo "foo2" + +proc bar(x: proc (x: int)) = + echo "bar" + +bar foo + diff --git a/tests/errmsgs/t5167_5.nim b/tests/errmsgs/t5167_5.nim new file mode 100644 index 000000000..ab02f29f6 --- /dev/null +++ b/tests/errmsgs/t5167_5.nim @@ -0,0 +1,25 @@ +discard """ +cmd: "nim check $file" +errormsg: "'m' has unspecified generic parameters" +nimout: ''' +t5167_5.nim(20, 9) Error: 't' has unspecified generic parameters +t5167_5.nim(21, 5) Error: 't' has unspecified generic parameters +t5167_5.nim(23, 9) Error: 'm' has unspecified generic parameters +t5167_5.nim(24, 5) Error: 'm' has unspecified generic parameters +''' +""" + +template t[B]() = + echo "foo1" + +macro m[T]: stmt = nil + +proc bar(x: proc (x: int)) = + echo "bar" + +let x = t +bar t + +let y = m +bar m + diff --git a/tests/errmsgs/tnon_concrete_cast.nim b/tests/errmsgs/tnon_concrete_cast.nim new file mode 100644 index 000000000..e4ae890ce --- /dev/null +++ b/tests/errmsgs/tnon_concrete_cast.nim @@ -0,0 +1,47 @@ +discard """ + errormsg: "cannot cast to a non concrete type: 'ptr SomeNumber'" + line: 36 +""" + +# https://github.com/nim-lang/Nim/issues/5428 + +type + MemFile = object + mem: pointer + +proc memfileopen(filename: string, newFileSize: int): MemFile = + # just a memfile mock + return + +type + MyData = object + member1: seq[int] + member2: int + +type + MyReadWrite = object + memfile: MemFile + offset: int + +# Here, SomeNumber is bound to a concrete type, and that's OK +proc write(rw: var MyReadWrite; value: SomeNumber): void = + (cast[ptr SomeNumber](cast[uint](rw.memfile.mem) + rw.offset.uint))[] = value + rw.offset += sizeof(SomeNumber) + +# Here, we try to use SomeNumber without binding it to a type. This should +# produce an error message for now. It's also possible to relax the rules +# and allow for type-class based type inference in such situations. +proc write[T](rw: var MyReadWrite; value: seq[T]): void = + rw.write value.len + let dst = cast[ptr SomeNumber](cast[uint](rw.memfile.mem) + uint(rw.offset)) + let src = cast[pointer](value[0].unsafeAddr) + let size = sizeof(T) * value.len + copyMem(dst, src, size) + rw.offset += size + +proc saveBinFile(arg: var MyData, filename: string): void = + var rw: MyReadWrite + rw.memfile = memfileOpen(filename, newFileSize = rw.offset) + rw.offset = 0 + rw.write arg.member1 + diff --git a/tests/macros/tmacro4.nim b/tests/macros/tmacro4.nim index a56369369..fb07941a9 100644 --- a/tests/macros/tmacro4.nim +++ b/tests/macros/tmacro4.nim @@ -5,7 +5,7 @@ discard """ import macros, strutils -macro test_macro*(n: stmt): stmt {.immediate.} = +macro test_macro*(s: string, n: stmt): stmt {.immediate.} = result = newNimNode(nnkStmtList) var ass : NimNode = newNimNode(nnkAsgn) add(ass, newIdentNode("str")) diff --git a/tests/macros/tquotewords.nim b/tests/macros/tquotewords.nim index 7a575f541..48fcafd62 100644 --- a/tests/macros/tquotewords.nim +++ b/tests/macros/tquotewords.nim @@ -6,7 +6,7 @@ discard """ import macros -macro quoteWords(n: expr): expr {.immediate.} = +macro quoteWords(n: varargs[expr]): expr {.immediate.} = let n = callsite() result = newNimNode(nnkBracket, n) for i in 1..n.len-1: diff --git a/tests/overload/tparam_forwarding.nim b/tests/overload/tparam_forwarding.nim new file mode 100644 index 000000000..c1b276bfc --- /dev/null +++ b/tests/overload/tparam_forwarding.nim @@ -0,0 +1,37 @@ +discard """ +output: '''baz +10 +100 +1000 +a +b +c +''' +""" + +type + Foo = object + x: int + +proc stringVarargs*(strings: varargs[string, `$`]): void = + for s in strings: echo s + +proc fooVarargs*(foos: varargs[Foo]) = + for f in foos: echo f.x + +template templateForwarding*(callable: untyped, + condition: bool, + forwarded: varargs[untyped]): untyped = + if condition: + callable(forwarded) + +proc procForwarding(args: varargs[string]) = + stringVarargs(args) + +templateForwarding stringVarargs, 17 + 4 < 21, "foo", "bar", 100 +templateForwarding stringVarargs, 10 < 21, "baz" + +templateForwarding fooVarargs, "test".len > 3, Foo(x: 10), Foo(x: 100), Foo(x: 1000) + +procForwarding "a", "b", "c" + diff --git a/tests/stdlib/tmemfiles2.nim b/tests/stdlib/tmemfiles2.nim index 026443e93..665e92e8a 100644 --- a/tests/stdlib/tmemfiles2.nim +++ b/tests/stdlib/tmemfiles2.nim @@ -18,7 +18,7 @@ mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20) mm.close() # read, change -mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1) +mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true) echo "Full read size: ",mm_full.size p = mm_full.mapMem(fmReadWrite, 20, 0) var p2 = cast[cstring](p) |