Skip to content

Wrong exception raised wrapped in finally in closure iterator; Regression in devel/version-2-2 #25202

@nitely

Description

@nitely

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-09-25
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 9f74712ec6ec346f8f1366c10f752d0a9aa85a70
active boot switches: -d:release
Nim Compiler Version 2.2.5 [Linux: amd64]
Compiled at 2025-10-02
Copyright (c) 2006-2025 by Andreas Rumpf

active boot switches: -d:release

git hash: cfe163795f3a8ac8ecf11dc0ab788a3758b2a32e

Description

This raises ValueError in 2.2.5 and devel. Works fine in Nim 2.2.4 and 2.0.16/2.0.17.

proc p() =
  iterator p_1073741828(): int {.
      closure, gcsafe, raises: [].} =
    var closureSucceeded_1073741827 = true
    try:
      try:
        try:
          yield 0
          raise newException(ValueError, "value error")
        except ValueError:
          raise newException(IOError, "io error")
      finally:
        yield 0
    except IOError as exc:
      closureSucceeded_1073741827 = false
      echo "io raised"
    finally:
      if closureSucceeded_1073741827:
        discard
  
  var internalClosure: iterator(): int = p_1073741828
  var internalClosure2: iterator(): int = p_1073741828

  while true:
    if not internalClosure.finished():
      discard internalClosure()
    if not internalClosure2.finished():
      discard internalClosure2()
    if internalClosure.finished() and internalClosure2.finished():
      break

p()
echo "ok"

Current Output

io raised
Error: unhandled exception: value error [ValueError]

Expected Output

io raised
io raised
ok

Known Workarounds

No response

Additional Information

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions