diff options
Diffstat (limited to 'tests/lookups')
42 files changed, 554 insertions, 7 deletions
diff --git a/tests/lookups/issue_23032/deep_scope.nim b/tests/lookups/issue_23032/deep_scope.nim new file mode 100644 index 000000000..3e25809a7 --- /dev/null +++ b/tests/lookups/issue_23032/deep_scope.nim @@ -0,0 +1,2 @@ +type A*[T] = object +proc foo*(a: A[int]): bool = false diff --git a/tests/lookups/issue_23172/m23172.nim b/tests/lookups/issue_23172/m23172.nim new file mode 100644 index 000000000..36af48761 --- /dev/null +++ b/tests/lookups/issue_23172/m23172.nim @@ -0,0 +1,6 @@ +type + Foo* = object + Bar* = object + +func `$`*(x: Foo | Bar): string = + "X" diff --git a/tests/lookups/mambsym1.nim b/tests/lookups/mambsym1.nim new file mode 100644 index 000000000..c4902b1b4 --- /dev/null +++ b/tests/lookups/mambsym1.nim @@ -0,0 +1,10 @@ +import mambsym2 # import TExport + +type + TExport* = enum x, y, z + TOtherEnum* = enum mDec, mInc, mAssign + +proc ha() = + var + x: TExport # no error + discard diff --git a/tests/lookups/mambsym2.nim b/tests/lookups/mambsym2.nim new file mode 100644 index 000000000..21d980073 --- /dev/null +++ b/tests/lookups/mambsym2.nim @@ -0,0 +1,3 @@ +type + TExport* = enum a, b, c + diff --git a/tests/lookups/mambsym3.nim b/tests/lookups/mambsym3.nim new file mode 100644 index 000000000..946a5ff29 --- /dev/null +++ b/tests/lookups/mambsym3.nim @@ -0,0 +1,4 @@ +# Module A +var x*: string +proc foo*(a: string) = + echo "A: ", a diff --git a/tests/lookups/mambsym4.nim b/tests/lookups/mambsym4.nim new file mode 100644 index 000000000..ed66cc16d --- /dev/null +++ b/tests/lookups/mambsym4.nim @@ -0,0 +1,4 @@ +# Module B +var x*: int +proc foo*(b: int) = + echo "B: ", b diff --git a/tests/lookups/mambsys1.nim b/tests/lookups/mambsys1.nim new file mode 100644 index 000000000..22e54cb94 --- /dev/null +++ b/tests/lookups/mambsys1.nim @@ -0,0 +1,7 @@ +import mambsys2 # import TExport + +type + TExport* = enum x, y, z + +proc foo*(x: int) = + discard diff --git a/tests/lookups/mambsys2.nim b/tests/lookups/mambsys2.nim new file mode 100644 index 000000000..ef63e4f7e --- /dev/null +++ b/tests/lookups/mambsys2.nim @@ -0,0 +1,4 @@ +type + TExport* = enum x, y, z # exactly the same type! + +proc foo*(x: int) = discard diff --git a/tests/lookups/mambtype1.nim b/tests/lookups/mambtype1.nim new file mode 100644 index 000000000..47046142e --- /dev/null +++ b/tests/lookups/mambtype1.nim @@ -0,0 +1 @@ +type K* = object diff --git a/tests/lookups/mambtype2.nim b/tests/lookups/mambtype2.nim new file mode 100644 index 000000000..cf622466b --- /dev/null +++ b/tests/lookups/mambtype2.nim @@ -0,0 +1,4 @@ +import ./mambtype1 +export mambtype1 +template K*(kind: static int): auto = typedesc[mambtype1.K] +template B*(kind: static int): auto = typedesc[mambtype1.K] diff --git a/tests/lookups/mbind3.nim b/tests/lookups/mbind3.nim new file mode 100644 index 000000000..1a7d3b63b --- /dev/null +++ b/tests/lookups/mbind3.nim @@ -0,0 +1,10 @@ +# Module A +var + lastId = 0 + +template genId*: int = + bind lastId + inc(lastId) + lastId + + diff --git a/tests/lookups/mdisambsym1.nim b/tests/lookups/mdisambsym1.nim new file mode 100644 index 000000000..b8beca035 --- /dev/null +++ b/tests/lookups/mdisambsym1.nim @@ -0,0 +1,2 @@ +proc count*(s: string): int = + s.len diff --git a/tests/lookups/mdisambsym2.nim b/tests/lookups/mdisambsym2.nim new file mode 100644 index 000000000..1e056311d --- /dev/null +++ b/tests/lookups/mdisambsym2.nim @@ -0,0 +1 @@ +var count*: int = 10 diff --git a/tests/lookups/mdisambsym3.nim b/tests/lookups/mdisambsym3.nim new file mode 100644 index 000000000..95bd19702 --- /dev/null +++ b/tests/lookups/mdisambsym3.nim @@ -0,0 +1 @@ +const count* = 3.142 diff --git a/tests/lookups/mmacroamb.nim b/tests/lookups/mmacroamb.nim new file mode 100644 index 000000000..107e51055 --- /dev/null +++ b/tests/lookups/mmacroamb.nim @@ -0,0 +1,10 @@ +# issue #12732 + +import std/macros +const getPrivate3_tmp* = 0 +const foobar1* = 0 # comment this or make private and it'll compile fine +macro foobar4*(): untyped = + newLit "abc" +template currentPkgDir2*: string = foobar4() +macro currentPkgDir2*(dir: string): untyped = + newLit "abc2" diff --git a/tests/lookups/t23032.nim b/tests/lookups/t23032.nim new file mode 100644 index 000000000..144abcb05 --- /dev/null +++ b/tests/lookups/t23032.nim @@ -0,0 +1,13 @@ +discard """ +action: "run" +outputsub: "proc (a: A[system.float]): bool{.noSideEffect, gcsafe.}" +""" + +import issue_23032/deep_scope + +proc foo(a: A[float]):bool = true + +let p: proc = foo +echo p.typeof +doAssert p(A[float]()) == true +doAssert compiles(doAssert p(A[int]()) == true) == false diff --git a/tests/lookups/t23172.nim b/tests/lookups/t23172.nim new file mode 100644 index 000000000..9edf9905a --- /dev/null +++ b/tests/lookups/t23172.nim @@ -0,0 +1,9 @@ +import issue_23172/m23172 + +type FooX = distinct Foo + +func `$`*(x: FooX): string = + $m23172.Foo(x) + +var a: FooX +doAssert $a == "X" diff --git a/tests/lookups/t23749.nim b/tests/lookups/t23749.nim new file mode 100644 index 000000000..650f04ea4 --- /dev/null +++ b/tests/lookups/t23749.nim @@ -0,0 +1,37 @@ +discard """ + action: compile +""" + +{.pragma: callback, gcsafe, raises: [].} + +type + DataProc* = proc(val: openArray[byte]) {.callback.} + GetProc = proc (db: RootRef, key: openArray[byte], onData: DataProc): bool {.nimcall, callback.} + KvStoreRef* = ref object + obj: RootRef + getProc: GetProc + +template get(dbParam: KvStoreRef, key: openArray[byte], onData: untyped): bool = + let db = dbParam + db.getProc(db.obj, key, onData) + +func decode(input: openArray[byte], maxSize = 128): seq[byte] = + @[] + +proc getSnappySSZ(db: KvStoreRef, key: openArray[byte]): string = + var status = "not found" + proc decode(data: openArray[byte]) = + status = + if true: "found" + else: "corrupted" + discard db.get(key, decode) + status + + +var ksr: KvStoreRef +var k = [byte(1), 2, 3, 4, 5] + +proc foo(): string = + getSnappySSZ(ksr, toOpenArray(k, 1, 3)) + +echo foo() diff --git a/tests/lookups/tambiguousemit.nim b/tests/lookups/tambiguousemit.nim new file mode 100644 index 000000000..4f4bacce4 --- /dev/null +++ b/tests/lookups/tambiguousemit.nim @@ -0,0 +1,12 @@ +discard """ + cmd: "nim check $options $file" # use check to assure error is pre-codegen + matrix: "; --backend:js" # backend shouldn't matter but at least check js +""" + +proc foo(x: int) = discard +proc foo(x: float) = discard + +{.emit: ["// ", foo].} #[tt.Error + ^ ambiguous identifier: 'foo' -- use one of the following: + tambiguousemit.foo: proc (x: int){.noSideEffect, gcsafe.} + tambiguousemit.foo: proc (x: float){.noSideEffect, gcsafe.}]# diff --git a/tests/lookups/tambprocvar.nim b/tests/lookups/tambprocvar.nim new file mode 100644 index 000000000..f5c3fde4f --- /dev/null +++ b/tests/lookups/tambprocvar.nim @@ -0,0 +1,19 @@ +discard """ + action: reject + cmd: "nim check $file" + nimout: ''' +tambprocvar.nim(15, 11) Error: ambiguous identifier: 'foo' -- use one of the following: + tambprocvar.foo: proc (x: int){.noSideEffect, gcsafe.} + tambprocvar.foo: proc (x: float){.noSideEffect, gcsafe.} +''' +""" + +block: + proc foo(x: int) = discard + proc foo(x: float) = discard + + let x = foo + +block: + let x = `+` #[tt.Error + ^ ambiguous identifier: '+' -- use one of the following:]# diff --git a/tests/lookups/tambsym.nim b/tests/lookups/tambsym.nim new file mode 100644 index 000000000..bd0f41717 --- /dev/null +++ b/tests/lookups/tambsym.nim @@ -0,0 +1,13 @@ +discard """ + errormsg: "ambiguous identifier" + file: "tambsym.nim" + line: 11 +""" +# Test ambiguous symbols + +import mambsym1, mambsym2 + +var + v: TExport #ERROR_MSG ambiguous identifier + +v = y diff --git a/tests/lookups/tambsym2.nim b/tests/lookups/tambsym2.nim new file mode 100644 index 000000000..747f1a086 --- /dev/null +++ b/tests/lookups/tambsym2.nim @@ -0,0 +1,21 @@ +discard """ + output: "7" +""" +# Test overloading of procs with locals + +type + TMyType = object + len: int + data: string + +proc len(x: TMyType): int {.inline.} = return x.len + +proc x(s: TMyType, len: int) = + writeLine(stdout, len(s)) + +var + m: TMyType +m.len = 7 +m.data = "1234" + +x(m, 5) #OUT 7 diff --git a/tests/lookups/tambsym3.nim b/tests/lookups/tambsym3.nim new file mode 100644 index 000000000..6bbebca10 --- /dev/null +++ b/tests/lookups/tambsym3.nim @@ -0,0 +1,13 @@ +discard """ + errormsg: "ambiguous identifier: 'mDec' -- use one of the following:" + file: "tambsym3.nim" + line: 11 +""" +# Test ambiguous symbols + +import mambsym1, times + +var + v = mDec #ERROR_MSG ambiguous identifier + +writeLine(stdout, ord(v)) diff --git a/tests/lookups/tambsymmanual.nim b/tests/lookups/tambsymmanual.nim new file mode 100644 index 000000000..3ad91b8cc --- /dev/null +++ b/tests/lookups/tambsymmanual.nim @@ -0,0 +1,25 @@ +discard """ + output: ''' +A: abc +B: 123 +A: def +4 +''' +""" +# Module C +import mambsym3, mambsym4 + +foo("abc") # A: abc +foo(123) # B: 123 +let inferred: proc (x: string) = foo +foo("def") # A: def + +doAssert not compiles(write(stdout, x)) # error: x is ambiguous +write(stdout, mambsym3.x) # no error: qualifier used + +proc bar(a: int): int = a + 1 +doAssert bar(x) == x + 1 # no error: only A.x of type int matches + +var x = 4 +write(stdout, x) # not ambiguous: uses the module C's x +echo() # for test output diff --git a/tests/lookups/tambsys.nim b/tests/lookups/tambsys.nim new file mode 100644 index 000000000..aa740c38f --- /dev/null +++ b/tests/lookups/tambsys.nim @@ -0,0 +1,10 @@ +discard """ + output: "" +""" +# Test ambiguous symbols + +import mambsys1, mambsys2 + +var + v: mambsys1.TExport +mambsys2.foo(3) #OUT diff --git a/tests/lookups/tambtype.nim b/tests/lookups/tambtype.nim new file mode 100644 index 000000000..a292db83a --- /dev/null +++ b/tests/lookups/tambtype.nim @@ -0,0 +1,20 @@ +import ./mambtype2 + +block: # issue #23893 + discard default(K(0)) # works + discard default(mambtype2.B(0)) # works + discard default(mambtype2.K(0)) # doesn't work + +block: # issue #23898, in template + template r() = + discard default(B(0)) # compiles + discard default(mambtype2.B(0)) # compiles + discard default(K(0)) # does not compile + r() + +block: # in generics + proc foo[T]() = + discard default(B(0)) # compiles + discard default(mambtype2.B(0)) # compiles + discard default(K(0)) # does not compile + foo[int]() diff --git a/tests/lookups/tbind.nim b/tests/lookups/tbind.nim new file mode 100644 index 000000000..f3fb952e3 --- /dev/null +++ b/tests/lookups/tbind.nim @@ -0,0 +1,78 @@ +discard """ +output: ''' +3 +1 +1 +1 +5 +''' +""" + + +block tbind: +# Test the new ``bind`` keyword for templates + + proc p1(x: int8, y: int): int = return x + y + + template tempBind(x, y): untyped = + bind p1 + p1(x, y) + + proc p1(x: int, y: int8): int = return x - y + + # This is tricky: the call to ``p1(1'i8, 2'i8)`` should not fail in line 6, + # because it is not ambiguous there. But it is ambiguous after line 8. + + echo tempBind(1'i8, 2'i8) #OUT 3 + + +import mbind3 +echo genId() #OUT 1 + + +import strtabs +block tbinoverload: + template t() = + block: + bind newStringTable + discard {"Content-Type": "text/html"}.newStringTable() + + discard {:}.newStringTable + #discard {"Content-Type": "text/html"}.newStringTable() + t() + + +block tmixin: + type + TFoo1 = object of RootObj + v: int + TFoo2 = object of TFoo1 + v2: int + + proc test(f: TFoo1) = + echo "1" + + proc Foo[T](f: T) = + mixin test + test(f) + + var + a: TFoo1 + b: TFoo2 + + + proc test(f: TFoo2) = + echo "2" + + Foo(a) + Foo(b) + +# issue #11811 +proc p(a : int) = + echo a + +proc printVar*[T:int|float|string](a : T) = + bind p + p(a) + +printVar(5) diff --git a/tests/lookups/tbind_for_generics.nim b/tests/lookups/tbind_for_generics.nim new file mode 100644 index 000000000..db5fbebbc --- /dev/null +++ b/tests/lookups/tbind_for_generics.nim @@ -0,0 +1,17 @@ +discard """ + errormsg: "type mismatch: got <Foo, Foo>" + line: 8 +""" +proc g[T](x: T) = + bind `+` + # because we bind `+` here, we must not find the `+` for 'Foo' below: + echo x + x + +type + Foo = object + a: int + +proc `+`(a, b: Foo): Foo = Foo(a: a.a+b.a) + +g(3) +g(Foo(a: 8)) diff --git a/tests/lookups/tdisambsym.nim b/tests/lookups/tdisambsym.nim new file mode 100644 index 000000000..678528ad6 --- /dev/null +++ b/tests/lookups/tdisambsym.nim @@ -0,0 +1,8 @@ +# issue #15247 + +import mdisambsym1, mdisambsym2, mdisambsym3 + +proc twice(n: int): int = + n*2 + +doAssert twice(count) == 20 diff --git a/tests/lookups/tenumlocalsym.nim b/tests/lookups/tenumlocalsym.nim new file mode 100644 index 000000000..575227c07 --- /dev/null +++ b/tests/lookups/tenumlocalsym.nim @@ -0,0 +1,22 @@ +block: + type Enum = enum a, b + + block: + let a = b + let x: Enum = a + doAssert x == b + +block: + type + Enum = enum + a = 2 + b = 10 + + iterator items2(): Enum = + for a in [a, b]: + yield a + + var s = newSeq[Enum]() + for i in items2(): + s.add i + doAssert s == @[a, b] diff --git a/tests/lookups/test.nim b/tests/lookups/test.nim index a17d235a4..dfacaf5b5 100644 --- a/tests/lookups/test.nim +++ b/tests/lookups/test.nim @@ -1,3 +1,10 @@ +discard """ +output: ''' + +[Suite] memoization +''' +""" + # This file needs to be called 'test' nim to provoke a clash # with the unittest.test name. Issue # @@ -5,7 +12,7 @@ import unittest, macros # bug #4555 -macro memo(n: untyped): typed = +macro memo(n: untyped) = result = n proc fastFib(n: int): int {.memo.} = 40 @@ -14,4 +21,3 @@ proc fib(n: int): int = 40 suite "memoization": test "recursive function memoization": check fastFib(40) == fib(40) - diff --git a/tests/lookups/tgensym.nim b/tests/lookups/tgensym.nim new file mode 100644 index 000000000..e33a2783f --- /dev/null +++ b/tests/lookups/tgensym.nim @@ -0,0 +1,16 @@ +discard """ + output: "123100" +""" + +template hygienic(val) = + var x = val + stdout.write x + +var x = 100 + +hygienic 1 +hygienic 2 +hygienic 3 + +echo x + diff --git a/tests/lookups/tgensymgeneric.nim b/tests/lookups/tgensymgeneric.nim new file mode 100644 index 000000000..6e1df4842 --- /dev/null +++ b/tests/lookups/tgensymgeneric.nim @@ -0,0 +1,54 @@ +discard """ + output: '''true''' +""" + +# We need to open the gensym'ed symbol again so that the instantiation +# creates a fresh copy; but this is wrong the very first reason for gensym +# is that scope rules cannot be used! So simply removing 'sfGenSym' does +# not work. Copying the symbol does not work either because we're already +# the owner of the symbol! What we need to do is to copy the symbol +# in the generic instantiation process... + +type + TA = object + x: int + TB = object + x: string + +template genImpl() = + var gensymed: T + when T is TB: + gensymed.x = "abc" + else: + gensymed.x = 123 + result = move gensymed + +proc gen[T](x: T): T = + genImpl() + +var + a: TA + b: TB +let x = gen(a) +let y = gen(b) + +doAssert x.x == 123 +doAssert y.x == "abc" + +# test symbol equality + +import macros + +static: + let sym1 = genSym() + let sym2 = genSym() + let sym3 = sym1 + let nimsym = sym1.symbol + doAssert sym1 == sym1 + doAssert sym2 != sym3 + doAssert sym2.symbol != sym3.symbol + doAssert sym3 == sym1 + doAssert sym1.symbol == sym1.symbol + doAssert nimsym == nimsym + +echo "true" diff --git a/tests/lookups/tinvalidbindtypedesc.nim b/tests/lookups/tinvalidbindtypedesc.nim new file mode 100644 index 000000000..1c71c8daf --- /dev/null +++ b/tests/lookups/tinvalidbindtypedesc.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "type mismatch: got <typedesc[float], string>" + line: 10 +""" + +proc foo(T: typedesc; some: T) = + echo($some) + +foo int, 4 +foo float, "bad" diff --git a/tests/lookups/tmacroamb.nim b/tests/lookups/tmacroamb.nim new file mode 100644 index 000000000..854017e72 --- /dev/null +++ b/tests/lookups/tmacroamb.nim @@ -0,0 +1,5 @@ +# issue #12732 + +import mmacroamb +const s0 = currentPkgDir2 #[tt.Error + ^ ambiguous identifier: 'currentPkgDir2' -- use one of the following:]# diff --git a/tests/lookups/tmoduleclash1.nim b/tests/lookups/tmoduleclash1.nim new file mode 100644 index 000000000..7058f691e --- /dev/null +++ b/tests/lookups/tmoduleclash1.nim @@ -0,0 +1,13 @@ +# issue #23596 + +import std/heapqueue +type Algo = enum heapqueue, quick +when false: + let x = heapqueue +let y: Algo = heapqueue +proc bar*(algo=quick) = + var x: HeapQueue[int] + case algo + of heapqueue: echo 1 # `Algo.heapqueue` works on devel + of quick: echo 2 + echo x.len diff --git a/tests/lookups/tmoduleclash2.nim b/tests/lookups/tmoduleclash2.nim new file mode 100644 index 000000000..958da2299 --- /dev/null +++ b/tests/lookups/tmoduleclash2.nim @@ -0,0 +1,6 @@ +import std/heapqueue +proc heapqueue(x: int) = discard +let x: proc (x: int) = heapqueue +let y: proc = heapqueue +when false: + let z = heapqueue diff --git a/tests/lookups/tnicerrorforsymchoice.nim b/tests/lookups/tnicerrorforsymchoice.nim new file mode 100644 index 000000000..6001b6b09 --- /dev/null +++ b/tests/lookups/tnicerrorforsymchoice.nim @@ -0,0 +1,23 @@ +discard """ + errormsg: "type mismatch: got <proc (s: TScgi: ScgiState or AsyncScgiState) | proc (client: AsyncSocket, headers: StringTableRef, input: string){.noSideEffect, gcsafe.}>" + line: 23 +""" + +# Fake ScgiState objects, from now-deprecated scgi module +type + ScgiState* = object of RootObj ## SCGI state object + AsyncScgiState* = object of RootObj ## SCGI state object + +#bug #442 +import asyncnet, strtabs +proc handleSCGIRequest[TScgi: ScgiState | AsyncScgiState](s: TScgi) = + discard +proc handleSCGIRequest(client: AsyncSocket, headers: StringTableRef, + input: string) = + discard + +proc test(handle: proc (client: AsyncSocket, headers: StringTableRef, + input: string), b: int) = + discard + +test(handleSCGIRequest) diff --git a/tests/lookups/told_bind_expr.nim b/tests/lookups/told_bind_expr.nim new file mode 100644 index 000000000..56389eaf6 --- /dev/null +++ b/tests/lookups/told_bind_expr.nim @@ -0,0 +1,15 @@ +discard """ + errormsg: "ambiguous call" + file: "told_bind_expr.nim" + line: 13 +""" + +# Pre-0.9 deprecated bind expression syntax + +proc p1(x: int8, y: int): int = return x + y +proc p1(x: int, y: int8): int = return x - y + +template tempBind(x, y): untyped = + (bind p1(x, y)) #ERROR_MSG ambiguous call + +echo tempBind(1'i8, 2'i8) diff --git a/tests/lookups/tprefer_proc.nim b/tests/lookups/tprefer_proc.nim deleted file mode 100644 index 57ee8e539..000000000 --- a/tests/lookups/tprefer_proc.nim +++ /dev/null @@ -1,4 +0,0 @@ - -# bug #4353 -import random -echo random[int](low(int) .. high(int)) diff --git a/tests/lookups/tredef.nim b/tests/lookups/tredef.nim index 8f1b38bd1..7c1df238e 100644 --- a/tests/lookups/tredef.nim +++ b/tests/lookups/tredef.nim @@ -4,7 +4,7 @@ foo(1, "test") proc bar(a: int, b: string) = discard bar(1, "test") -template foo(a: int, b: string) = bar(a, b) +template foo(a: int, b: string) {.redefine.} = bar(a, b) foo(1, "test") block: diff --git a/tests/lookups/tundeclaredmixin.nim b/tests/lookups/tundeclaredmixin.nim new file mode 100644 index 000000000..74d16cdde --- /dev/null +++ b/tests/lookups/tundeclaredmixin.nim @@ -0,0 +1,17 @@ +discard """ + nimout: ''' + mixin nothing, add +''' +""" + +# issue #22012 + +import macros + +expandMacros: + proc foo[T](): int = + # `nothing` is undeclared, `add` is declared + mixin nothing, add + 123 + + doAssert foo[int]() == 123 |