Skip to content

Commit c8456ea

Browse files
authored
fixes #25117; requiresInit not checked for result if it has been used (#25151)
fixes #25117 errors on `requiresInit` of `result` if it is used before initialization. Otherwise ```nim # prevent superfluous warnings about the same variable: a.init.add s.id ``` It produces a warning, and this line prevents it from being recognized by the `requiresInit` check in `trackProc`
1 parent 34bb37d commit c8456ea

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

compiler/sempass2.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ proc useVar(a: PEffects, n: PNode) =
379379
# If the variable is explicitly marked as .noinit. do not emit any error
380380
a.init.add s.id
381381
elif s.id notin a.init:
382-
if s.typ.requiresInit:
382+
if s.kind == skResult and tfRequiresInit in s.typ.flags:
383+
localError(a.config, n.info, "'result' requires explicit initialization")
384+
elif s.typ.requiresInit:
383385
message(a.config, n.info, warnProveInit, s.name.s)
384386
elif a.leftPartOfAsgn <= 0:
385387
if strictDefs in a.c.features:

koch.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const
1313
# examples of possible values for repos: Head, ea82b54
1414
NimbleStableCommit = "9207e8b2bbdf66b5a4d1020214cff44d2d30df92" # 0.20.1
1515
AtlasStableCommit = "26cecf4d0cc038d5422fc1aa737eec9c8803a82b" # 0.9
16-
ChecksumsStableCommit = "f8f6bd34bfa3fe12c64b919059ad856a96efcba0" # 2.0.1
16+
ChecksumsStableCommit = "0b8e46379c5bc1bf73d8b3011908389c60fb9b98" # 2.0.1
1717
SatStableCommit = "faf1617f44d7632ee9601ebc13887644925dcc01"
1818

1919
NimonyStableCommit = "1dbabac403ae32e185ee4c29f006d04e04b50c6d" # unversioned \

tests/errmsgs/t25117.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
discard """
2+
errormsg: "'result' requires explicit initialization"
3+
"""
4+
5+
type RI {.requiresInit.} = object
6+
v: int
7+
8+
proc xxx(v: var RI) = discard
9+
10+
proc f(T: type): T =
11+
xxx(result) # Should fail
12+
13+
discard f(RI)

0 commit comments

Comments
 (0)