[BUG] Unclear semantics when using `return/break/continue` in a `finally` block
Open
Nobody has claimed this yet.
auto_priority_triaged
bug
modular-repo
mojo
- Dominant language
- Mojo
- Stars
- 29.8k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Bug description
As title.
Related:
- PEP 601 – Forbid return/break/continue breaking out of finally
- PEP 765 – Disallow return/break/continue that exit a finally block
CC @dgurchenkov.
Steps to reproduce
- If we run
foo, we are greeted withUnhandled exception caught during execution: bad. barprintsreturnand exits silently.buzdiverges and printscontinueinfinitely.
fn main() raises:
foo()
# bar()
# buz()
fn foo() raises:
try:
raise "bad"
finally:
print("ok")
fn bar() raises:
while True:
try:
raise "bad"
finally:
print("return")
return
fn buz() raises:
while True:
try:
raise "bad"
finally:
print("continue")
continue
I'm not at all sure about what's intended behaviour, and there are many other corner cases in the linked PEPs. Maybe we should ban them from finally blocks.
System information
mojo 25.4.0.dev2025050905 (e3051145)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the foo, bar, and buz examples from the issue with the listed Mojo version. Read PEP 601 and PEP 765, then compare their corner cases with the current behavior. Done requires an explicit semantic decision for return, break, and continue in finally blocks, along with behavior that consistently follows it.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100