DAA and 'out' parameters (#20506)
* DAA and 'out' parameters * progress * documented strictDefs and out parameters * docs, tests and a bugfix * fixes silly regression
This commit is contained in:
parent
e323b91a32
commit
0d23419e68
17 changed files with 289 additions and 53 deletions
59
tests/init/tinitchecks_v2.nim
Normal file
59
tests/init/tinitchecks_v2.nim
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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
|
||||
24
tests/init/toutparam_subtype.nim
Normal file
24
tests/init/toutparam_subtype.nim
Normal file
|
|
@ -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()
|
||||
|
|
@ -4,7 +4,7 @@ discard """
|
|||
"""
|
||||
|
||||
import strutils
|
||||
|
||||
{.experimental: "strictDefs".}
|
||||
{.warning[Uninit]:on.}
|
||||
|
||||
proc p =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue