diff options
Diffstat (limited to 'tests/init')
-rw-r--r-- | tests/init/t8314.nim | 27 | ||||
-rw-r--r-- | tests/init/tcompiles.nim | 101 | ||||
-rw-r--r-- | tests/init/tinitchecks_v2.nim | 83 | ||||
-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/toutparam_subtype.nim | 24 | ||||
-rw-r--r-- | tests/init/toutparams.nim | 14 | ||||
-rw-r--r-- | tests/init/tproveinit.nim | 18 | ||||
-rw-r--r-- | tests/init/treturns.nim | 106 | ||||
-rw-r--r-- | tests/init/tuninit1.nim | 8 |
12 files changed, 473 insertions, 4 deletions
diff --git a/tests/init/t8314.nim b/tests/init/t8314.nim new file mode 100644 index 000000000..47c8480c2 --- /dev/null +++ b/tests/init/t8314.nim @@ -0,0 +1,27 @@ +discard """ + nimout: ''' +t8314.nim(14, 7) Hint: BEGIN [User] +t8314.nim(25, 7) Hint: END [User] + ''' + +output: ''' +1 +1 +1 +''' +""" + +{.hint: "BEGIN".} +proc foo(x: range[1..10]) = + block: + var (y,) = (x,) + echo y + block: + var (_,y) = (1,x) + echo y + block: + var (y,_,) = (x,1,) + echo y +{.hint: "END".} + +foo(1) 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 new file mode 100644 index 000000000..f7716bcca --- /dev/null +++ b/tests/init/tinitchecks_v2.nim @@ -0,0 +1,83 @@ +discard """ +cmd: "nim check $file" +action: "compile" +""" + +{.experimental: "strictDefs".} + +proc myopen(f: out File; s: string): bool = + f = default(File) + result = false + +proc main = + var f: File + if myopen(f, "aarg"): + f.close + +proc invalid = + var s: seq[string] + s.add "abc" #[tt.Warning + ^ use explicit initialization of 's' for clarity [Uninit] ]# + +proc valid = + var s: seq[string] = @[] + s.add "abc" # valid! + +main() +invalid() +valid() + +proc branchy(cond: bool) = + var s: seq[string] + if cond: + s = @["y"] + else: + s = @[] + s.add "abc" # valid! + +branchy true + +proc p(x: out int; y: out string; cond: bool) = #[tt.Warning + ^ Cannot prove that 'y' is initialized. This will become a compile time error in the future. [ProveInit] ]# + x = 4 + if cond: + y = "abc" + # error: not every path initializes 'y' + +var gl: int +var gs: string +p gl, gs, false + +proc canRaise(x: int): int = + result = x + raise newException(ValueError, "wrong") + +proc currentlyValid(x: out int; y: out string; cond: bool) = + x = canRaise(45) + 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/toutparam_subtype.nim b/tests/init/toutparam_subtype.nim new file mode 100644 index 000000000..3597f1459 --- /dev/null +++ b/tests/init/toutparam_subtype.nim @@ -0,0 +1,24 @@ +discard """ +cmd: "nim check $file" +action: "compile" +errormsg: "type mismatch: got <Subclass[system.int]>" +line: 21 +""" + +{.experimental: "strictDefs".} + +type + Superclass[T] = object of RootObj + a: T + Subclass[T] = object of Superclass[T] + s: string + +proc init[T](x: out Superclass[T]) = + x = Superclass(a: 8) + +proc subtypeCheck = + var v: Subclass[int] + init(v) + echo v.s # the 's' field was never initialized! + +subtypeCheck() 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() + diff --git a/tests/init/tuninit1.nim b/tests/init/tuninit1.nim index 886a1d766..ac3007e8d 100644 --- a/tests/init/tuninit1.nim +++ b/tests/init/tuninit1.nim @@ -1,10 +1,10 @@ discard """ - msg: "Warning: 'y' might not have been initialized [Uninit]" - line:34 + nimout: "tuninit1.nim(34, 11) Warning: use explicit initialization of 'y' for clarity [Uninit]" + action: compile """ import strutils - +{.experimental: "strictDefs".} {.warning[Uninit]:on.} proc p = @@ -22,7 +22,7 @@ proc p = try: z = parseInt("1233") - except E_Base: + except Exception: case x of 34: z = 123 of 13: z = 34 |