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 | 36 | ||||
-rw-r--r-- | tests/init/tuninit2.nim | 54 | ||||
-rw-r--r-- | tests/init/tzeroarray.nim | 18 |
14 files changed, 577 insertions, 0 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 new file mode 100644 index 000000000..ac3007e8d --- /dev/null +++ b/tests/init/tuninit1.nim @@ -0,0 +1,36 @@ +discard """ + 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 = + var x, y, z: int + if stdin.readLine == "true": + x = 34 + + while false: + y = 999 + break + + while true: + if x == 12: break + y = 9999 + + try: + z = parseInt("1233") + except Exception: + case x + of 34: z = 123 + of 13: z = 34 + else: z = 8 + else: + y = 3444 + x = 3111 + z = 0 + echo x, y, z + +p() diff --git a/tests/init/tuninit2.nim b/tests/init/tuninit2.nim new file mode 100644 index 000000000..950895c02 --- /dev/null +++ b/tests/init/tuninit2.nim @@ -0,0 +1,54 @@ +# bug #2316 + +type + EventType = enum + QuitEvent = 5 + AppMain* = ref object of RootObj + width: int + height: int + title: string + running: bool + event_type: EventType + App* = ref object of AppMain + draw_proc: proc(app: AppMain): void {.closure.} + events_proc: proc(app: AppMain): void {.closure.} + update_proc: proc(app: AppMain, dt: float): void {.closure.} + load_proc: proc(app: AppMain): void {.closure.} + + +proc initApp*(t: string, w, h: int): App = + App(width: w, height: h, title: t, event_type: EventType.QuitEvent) + + +method getTitle*(self: AppMain): string = self.title +method getWidth*(self: AppMain): int = self.width +method getHeight*(self: AppMain): int = self.height + + +method draw*(self: App, draw: proc(app: AppMain)): void = + self.draw_proc = draw + +method load*(self: App, load: proc(a: AppMain)): void = + self.load_proc = load + +method events*(self: App, events: proc(app: AppMain)): void = + self.events_proc = events + +method update*(self: App, update: proc(app: AppMain, delta: float)): void = + self.update_proc = update + +method run*(self: App): void = discard + +var mygame = initApp("Example", 800, 600) + +mygame.load(proc(app: AppMain): void = + echo app.getTitle() + echo app.getWidth() + echo app.getHeight() +) + +mygame.events(proc(app: AppMain): void = + discard +) + +mygame.run() diff --git a/tests/init/tzeroarray.nim b/tests/init/tzeroarray.nim new file mode 100644 index 000000000..b784b601e --- /dev/null +++ b/tests/init/tzeroarray.nim @@ -0,0 +1,18 @@ +discard """ + output: done +""" + +for i in 0 .. 1: + var a: array[0..4, int] + if a[0] != 0: quit "bug" + a[0] = 6 + +proc main = + for i in 0 .. 1: + var a: array[0..4, int] + if a[0] != 0: quit "bug" + a[0] = 6 + +main() +echo "done" + |