diff options
Diffstat (limited to 'tests/init')
-rw-r--r-- | tests/init/tcompiles.nim | 101 | ||||
-rw-r--r-- | tests/init/tinitchecks_v2.nim | 24 | ||||
-rw-r--r-- | tests/init/tlet.nim | 55 | ||||
-rw-r--r-- | tests/init/tlet_uninit2.nim | 5 | ||||
-rw-r--r-- | tests/init/tlet_uninit3.nim | 24 | ||||
-rw-r--r-- | tests/init/tlet_uninit4.nim | 12 | ||||
-rw-r--r-- | tests/init/toutparams.nim | 14 | ||||
-rw-r--r-- | tests/init/tproveinit.nim | 18 | ||||
-rw-r--r-- | tests/init/treturns.nim | 106 |
9 files changed, 359 insertions, 0 deletions
diff --git a/tests/init/tcompiles.nim b/tests/init/tcompiles.nim new file mode 100644 index 000000000..67e17b241 --- /dev/null +++ b/tests/init/tcompiles.nim @@ -0,0 +1,101 @@ +discard """ + matrix: "--warningAsError:ProveInit --warningAsError:Uninit" +""" + +{.experimental: "strictdefs".} + +type Test = object + id: int + +proc foo {.noreturn.} = discard + +block: + proc test(x: bool): Test = + if x: + foo() + else: + foo() + +block: + proc test(x: bool): Test = + if x: + result = Test() + else: + foo() + + discard test(true) + +block: + proc test(x: bool): Test = + if x: + result = Test() + else: + return Test() + + discard test(true) + +block: + proc test(x: bool): Test = + if x: + return Test() + else: + return Test() + + discard test(true) + +block: + proc test(x: bool): Test = + if x: + result = Test() + else: + result = Test() + return + + discard test(true) + +block: + proc test(x: bool): Test = + if x: + result = Test() + return + else: + raise newException(ValueError, "unreachable") + + discard test(true) + +# bug #21615 +# bug #16735 + +block: + type Test {.requiresInit.} = object + id: int + + proc bar(): int = + raise newException(CatchableError, "error") + + proc test(): Test = + raise newException(CatchableError, "") + + template catchError(body) = + var done = false + try: + body + except CatchableError: + done = true + doAssert done + + catchError: + echo test() + + catchError: + echo bar() + +block: + proc foo(x: ptr int) = + discard + + proc main = + var s: int + foo(addr s) + + main() diff --git a/tests/init/tinitchecks_v2.nim b/tests/init/tinitchecks_v2.nim index 4a8cda028..f7716bcca 100644 --- a/tests/init/tinitchecks_v2.nim +++ b/tests/init/tinitchecks_v2.nim @@ -57,3 +57,27 @@ proc currentlyValid(x: out int; y: out string; cond: bool) = y = "abc" # <-- error: not every path initializes 'y' currentlyValid gl, gs, false + +block: # previously effects/toutparam + proc gah[T](x: out T) = + x = 3 + + proc arr1 = + var a: array[2, int] + var x: int + gah(x) + a[0] = 3 + a[x] = 3 + echo x + + arr1() + + proc arr2 = + var a: array[2, int] + var x: int + a[0] = 3 + a[x] = 3 #[tt.Warning + ^ use explicit initialization of 'x' for clarity [Uninit] ]# + echo x + + arr2() diff --git a/tests/init/tlet.nim b/tests/init/tlet.nim new file mode 100644 index 000000000..e32bedb18 --- /dev/null +++ b/tests/init/tlet.nim @@ -0,0 +1,55 @@ +{.experimental: "strictDefs".} + +proc bar(x: out string) = + x = "abc" + +template moe = # bug #21043 + try: + discard + except ValueError as e: + echo(e.msg) + +template moe0 {.dirty.} = # bug #21043 + try: + discard + except ValueError as e: + echo(e.msg) + +proc foo() = + block: + let x: string + if true: + x = "abc" + else: + x = "def" + doAssert x == "abc" + block: + let y: string + bar(y) + doAssert y == "abc" + block: + let x: string + if true: + x = "abc" + discard "abc" + else: + x = "def" + discard "def" + doAssert x == "abc" + block: # + let x {.used.} : int + block: # + let x: float + x = 1.234 + doAssert x == 1.234 + + block: + try: + discard + except ValueError as e: + echo(e.msg) + moe() + moe0() + +static: foo() +foo() diff --git a/tests/init/tlet_uninit2.nim b/tests/init/tlet_uninit2.nim new file mode 100644 index 000000000..174aa28f7 --- /dev/null +++ b/tests/init/tlet_uninit2.nim @@ -0,0 +1,5 @@ +discard """ + errormsg: "'let' symbol requires an initialization" +""" + +let x: int \ No newline at end of file diff --git a/tests/init/tlet_uninit3.nim b/tests/init/tlet_uninit3.nim new file mode 100644 index 000000000..cb786f8c8 --- /dev/null +++ b/tests/init/tlet_uninit3.nim @@ -0,0 +1,24 @@ +discard """ + cmd: "nim check $file" + action: "reject" + nimout: ''' +tlet_uninit3.nim(13, 5) Error: 'let' symbol requires an initialization +tlet_uninit3.nim(19, 5) Error: 'x' cannot be assigned to +tlet_uninit3.nim(23, 11) Error: 'let' symbol requires an initialization +''' +""" + +{.experimental: "strictDefs".} + +let global {.used.}: int + +proc foo() = + block: + let x: int + x = 13 + x = 14 + + block: + let x: int + doAssert x == 0 +foo() diff --git a/tests/init/tlet_uninit4.nim b/tests/init/tlet_uninit4.nim new file mode 100644 index 000000000..c57d15ff8 --- /dev/null +++ b/tests/init/tlet_uninit4.nim @@ -0,0 +1,12 @@ +discard """ + errormsg: "type mismatch: got <string>" +""" + +{.experimental: "strictDefs".} + +proc foo(x: var string) = + echo x + +proc bar() = + let x: string + foo(x) diff --git a/tests/init/toutparams.nim b/tests/init/toutparams.nim new file mode 100644 index 000000000..590768599 --- /dev/null +++ b/tests/init/toutparams.nim @@ -0,0 +1,14 @@ +discard """ + matrix: "--warningAsError:ProveInit" +""" + +{.experimental: "strictdefs".} + +proc foo(x: out int) = + x = 1 + +proc bar(x: out int) = + foo(x) + +var s: int +bar(s) diff --git a/tests/init/tproveinit.nim b/tests/init/tproveinit.nim new file mode 100644 index 000000000..c9f688309 --- /dev/null +++ b/tests/init/tproveinit.nim @@ -0,0 +1,18 @@ +discard """ + joinable: false +""" + +{.warningAsError[ProveInit]:on.} +template main() = + proc fn(): var int = + discard + discard fn() +doAssert not compiles(main()) + +# bug #9901 +import std/[sequtils, times] +proc parseMyDates(line: string): DateTime = + result = parse(line, "yyyy-MM-dd") +var dateStrings = @["2018-12-01", "2018-12-02", "2018-12-03"] +var parsed = dateStrings.map(parseMyDates) +discard parsed diff --git a/tests/init/treturns.nim b/tests/init/treturns.nim new file mode 100644 index 000000000..18cebe0b1 --- /dev/null +++ b/tests/init/treturns.nim @@ -0,0 +1,106 @@ +{.experimental: "strictdefs".} + +type Test = object + id: int + +proc foo {.noreturn.} = discard + +proc test1(): Test = + if true: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return Test() + else: + return + +proc test0(): Test = + if true: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return + else: + foo() + +proc test2(): Test = + if true: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return + else: + return + +proc test3(): Test = + if true: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return + else: + return Test() + +proc test4(): Test = + if true: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return + else: + result = Test() + return + +proc test5(x: bool): Test = + case x: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + of true: + return + else: + return Test() + +proc test6(x: bool): Test = + case x: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + of true: + return + else: + return + +proc test7(x: bool): Test = + case x: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + of true: + return + else: + discard + +proc test8(x: bool): Test = + case x: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + of true: + discard + else: + raise + +proc hasImportStmt(): bool = + if false: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return true + else: + discard + +discard hasImportStmt() + +block: + proc hasImportStmt(): bool = + if false: #[tt.Warning + ^ Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]]# + return true + else: + return + + discard hasImportStmt() + +block: + block: + proc foo(x: var int) = + discard + + proc main = + var s: int + foo(s)#[tt.Warning + ^ use explicit initialization of 's' for clarity [Uninit]]# + + main() + |