summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/sempass2.nim2
-rw-r--r--tests/init/tcompiles.nim27
2 files changed, 28 insertions, 1 deletions
diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim
index 823699a8c..f542a1244 100644
--- a/compiler/sempass2.nim
+++ b/compiler/sempass2.nim
@@ -1556,7 +1556,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =
        strictDefs in c.features) and
      s.kind in {skProc, skFunc, skConverter, skMethod} and s.magic == mNone:
     var res = s.ast[resultPos].sym # get result symbol
-    if res.id notin t.init:
+    if res.id notin t.init and breaksBlock(body) != bsNoReturn:
       if tfRequiresInit in s.typ[0].flags:
         localError(g.config, body.info, "'$1' requires explicit initialization" % "result")
       else:
diff --git a/tests/init/tcompiles.nim b/tests/init/tcompiles.nim
index 2072702ad..e86cad1e2 100644
--- a/tests/init/tcompiles.nim
+++ b/tests/init/tcompiles.nim
@@ -62,3 +62,30 @@ block:
       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()