diff options
Diffstat (limited to 'tests/global')
-rw-r--r-- | tests/global/a_module.nim | 6 | ||||
-rw-r--r-- | tests/global/globalaux.nim | 15 | ||||
-rw-r--r-- | tests/global/globalaux2.nim | 4 | ||||
-rw-r--r-- | tests/global/t15005.nim | 18 | ||||
-rw-r--r-- | tests/global/t21896.nim | 9 | ||||
-rw-r--r-- | tests/global/t3505.nim | 41 | ||||
-rw-r--r-- | tests/global/t5958.nim | 9 | ||||
-rw-r--r-- | tests/global/tglobal.nim | 16 | ||||
-rw-r--r-- | tests/global/tglobal2.nim | 9 | ||||
-rw-r--r-- | tests/global/tglobalforvar.nim | 9 |
10 files changed, 136 insertions, 0 deletions
diff --git a/tests/global/a_module.nim b/tests/global/a_module.nim new file mode 100644 index 000000000..1d3f4848c --- /dev/null +++ b/tests/global/a_module.nim @@ -0,0 +1,6 @@ +# a.nim +{.push stackTrace: off.} +proc foo*(): int = + var a {.global.} = 0 + result = a +{.pop.} \ No newline at end of file diff --git a/tests/global/globalaux.nim b/tests/global/globalaux.nim new file mode 100644 index 000000000..951472695 --- /dev/null +++ b/tests/global/globalaux.nim @@ -0,0 +1,15 @@ +type + TObj*[T] = object + val*: T + +var + totalGlobals* = 0 + +proc makeObj[T](x: T): TObj[T] = + totalGlobals += 1 + result.val = x + +proc globalInstance*[T]: var TObj[T] = + var g {.global.} = when T is int: makeObj(10) else: makeObj("hello") + result = g + diff --git a/tests/global/globalaux2.nim b/tests/global/globalaux2.nim new file mode 100644 index 000000000..6c77f1f48 --- /dev/null +++ b/tests/global/globalaux2.nim @@ -0,0 +1,4 @@ +import globalaux + +echo "in globalaux2: ", globalInstance[int]().val + diff --git a/tests/global/t15005.nim b/tests/global/t15005.nim new file mode 100644 index 000000000..6395b1dde --- /dev/null +++ b/tests/global/t15005.nim @@ -0,0 +1,18 @@ +type + T = ref object + data: string + +template foo(): T = + var a15005 {.global.}: T + once: + a15005 = T(data: "hi") + + a15005 + +proc test() = + var b15005 = foo() + + doAssert b15005.data == "hi" + +test() +test() diff --git a/tests/global/t21896.nim b/tests/global/t21896.nim new file mode 100644 index 000000000..c7765c4dd --- /dev/null +++ b/tests/global/t21896.nim @@ -0,0 +1,9 @@ +discard """ + errormsg: "cannot assign local to global variable" + line: 7 +""" + +proc example(a:int) = + let b {.global.} = a + +example(1) diff --git a/tests/global/t3505.nim b/tests/global/t3505.nim new file mode 100644 index 000000000..437a02ae6 --- /dev/null +++ b/tests/global/t3505.nim @@ -0,0 +1,41 @@ +discard """ +cmd: "nim check $options --hints:off $file" +action: "reject" +nimout: ''' +t3505.nim(22, 22) Error: cannot assign local to global variable +t3505.nim(31, 28) Error: cannot assign local to global variable +t3505.nim(39, 29) Error: cannot assign local to global variable + + + + +''' +""" + + + + + + +proc foo = + let a = 0 + var b {.global.} = a +foo() + +# issue #5132 +proc initX(it: float): int = 8 +proc initX2(it: int): int = it + +proc main() = + var f: float + var x {.global.} = initX2(initX(f)) + +main() + +# issue #20866 +proc foo2() = + iterator bar() {.closure.} = + discard + var g {.global.} = rawProc(bar) + +foo2() \ No newline at end of file diff --git a/tests/global/t5958.nim b/tests/global/t5958.nim new file mode 100644 index 000000000..9b7642363 --- /dev/null +++ b/tests/global/t5958.nim @@ -0,0 +1,9 @@ +discard """ + errormsg: "undeclared identifier: 'a'" + line: 9 +""" + +static: + var a = 1 + +echo a diff --git a/tests/global/tglobal.nim b/tests/global/tglobal.nim new file mode 100644 index 000000000..d44a62afc --- /dev/null +++ b/tests/global/tglobal.nim @@ -0,0 +1,16 @@ +discard """ + output: "in globalaux2: 10\ntotal globals: 2\nint value: 100\nstring value: second" + disabled: "true" +""" + +import globalaux, globalaux2 + +echo "total globals: ", totalGlobals + +globalInstance[int]().val = 100 +echo "int value: ", globalInstance[int]().val + +globalInstance[string]().val = "first" +globalInstance[string]().val = "second" +echo "string value: ", globalInstance[string]().val + diff --git a/tests/global/tglobal2.nim b/tests/global/tglobal2.nim new file mode 100644 index 000000000..73a575bbd --- /dev/null +++ b/tests/global/tglobal2.nim @@ -0,0 +1,9 @@ +# b.nim +import a_module +doAssert foo() == 0 + +proc hello(x: type) = + var s {.global.} = default(x) + doAssert s == 0 + +hello(int) diff --git a/tests/global/tglobalforvar.nim b/tests/global/tglobalforvar.nim new file mode 100644 index 000000000..bc18f33f2 --- /dev/null +++ b/tests/global/tglobalforvar.nim @@ -0,0 +1,9 @@ +discard """ +output: 100 +""" + +var funcs: seq[proc (): int {.nimcall.}] = @[] +for i in 0..10: + funcs.add((proc (): int = return i * i)) + +echo(funcs[3]()) |