mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
```nim
{.experimental: "strictdefs".}
type Test = object
id: int
proc test(): Test =
if true:
return Test()
else:
return
echo test()
```
I will tackle https://github.com/nim-lang/Nim/issues/16735 and #21615 in
the following PR.
The old code just premises that in branches ended with returns, raise
statements etc. , all variables including the result variable are
initialized for that branch. It's true for noreturn statements. But it
is false for the result variable in a branch tailing with a return
statement, in which the result variable is not initialized. The solution
is not perfect for usages below branch statements with the result
variable uninitialized, but it should suffice for now, which gives a
proper warning.
It also fixes
```nim
{.experimental: "strictdefs".}
type Test = object
id: int
proc foo {.noreturn.} = discard
proc test9(x: bool): Test =
if x:
foo()
else:
foo()
```
which gives a warning, but shouldn't
65 lines
910 B
Nim
65 lines
910 B
Nim
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)
|