Skip to content

[stdlib] Declare std.sys.exit() as returning Never - #7379

Merged
modularbot merged 3 commits into
modular:mainfrom
Alex-Wengg:fix/7297-exit-never
Nov 16, 2026
Merged

modularbot merged 3 commits into
modular:mainfrom
Alex-Wengg:fix/7297-exit-never

Conversation

@Alex-Wengg

@Alex-Wengg Alex-Wengg commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #7297

Type of change

  • Bug fix (non-breaking change that fixes an issue)

issue

std.sys.exit() never returns, but it is declared as returning None. A function with a result type that ends in a call to exit() therefore fails to compile with return expected at end of function with results, as in the issue's parse_or_exit example. The only workaround is to add a dead return.

fix

  • Both exit() overloads in std/sys/terminate.mojo now return Never.
  • The underlying std.sys._libc.exit binding now returns Never. As with _abort_base(), an infinite loop after the external_call satisfies the noreturn checker.

Existing callers aren't affected: a Never call is still valid as a statement.

verification

  • test/sys/test_exit_0.mojo now contains a function with a result type that ends in std.sys.exit(0). With the current stdlib it fails to compile with the error from the issue; with this change it compiles, runs, and exits with status 0.
  • test_exit_1.mojo still prints Running... and exits with status 1.
  • The issue's repro prints 42 and not a number: x, then exits with status 2.
  • ./bazelw test //Mojo/stdlib/test/sys/...: 23 pass, 1 skipped (Linux-only target) on macOS arm64. I have not tested on Linux.

Checklist

  • The linked issue above has been reviewed by a maintainer and is agreed-upon, or this is a trivial fix that does not need prior approval
  • PR is small and focused
  • I ran mojo format on the changed files
  • I added or updated tests to cover my changes
  • If AI tools assisted with this contribution, I have included an Assisted-by: trailer in my commit message or this PR description

Assisted-by: AI

🤖 Generated with Claude Code

@Alex-Wengg
Alex-Wengg requested review from a team as code owners October 23, 2026 05:20
@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review mojo-docs labels Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Alex-Wengg

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

modular-cla-bot Bot added a commit to modular/cla that referenced this pull request Oct 23, 2026
@Alex-Wengg
Alex-Wengg marked this pull request as draft October 23, 2026 05:24
@Alex-Wengg
Alex-Wengg marked this pull request as ready for review October 23, 2026 05:49

@barcharcraz barcharcraz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to use the "noreturn" attribute on the call instead of a loop to address the noreturn checker.

I'm OK with the loop if we're sure it's not UB.

Comment thread Mojo/stdlib/std/sys/_libc.mojo Resolved
Comment thread Mojo/stdlib/std/sys/_libc.mojo Outdated
Comment on lines +73 to +76
# Unreachable since the call above is noreturn, but we need to satisfy
# the noreturn checker.
while True:
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on my testings & local r&d, it's still needed. The noreturn check runs in the Mojo frontend, before LLVM, and it doesn't count a noreturn call as ending the function. Without the loop, current nightly fails with:

std/sys/_libc.mojo:65:5: error: return expected at end of function with results
def exit(status: c_int) -> Never:
 ^
mojo: error: errors occurred during compilation

i had tried to add llvm.unreachable after the call but it doesn't satisfy the checker either. _abort_base in os.mojo uses the same loop after llvm.intr.trap.

The loop never runs. The IR is declare void @EXIT(i32) noreturn, and LLVM drops everything after the call, so the function ends at bl _exit.

I also found a way to compile without the loop: return the call with a Never result type:

mojo
return __mlir_op.`pop.external_call`[
   func=__mlir_attr[`"exit" : !kgen.string`],
   _type=Never,
   funcAttrs=__mlir_attr.`["noreturn"]`,
](status)

It behaves the same: test_exit_0 and test_exit_1 pass, and it also ends at bl _exit. The catch is that !kgen.never lowers to i32, so this declares i32 @EXIT(i32) instead of void. the call never returns, but it's a mismatched signature/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the version returning never. Also, it should lower to the same as None. You can probably use the normal external_call if you do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_libc.exit is now just external_call"exit", Never there's no hand-built pop.external_call, no noreturn & no loop.

`exit()` never returns, but it was declared as returning `None`, so a
function with a result type that ended in `exit()` failed to compile with
"return expected at end of function with results". Mark both `exit()`
overloads and the underlying `_libc.exit` binding as `-> Never`, matching
`abort()`.

Fixes modular#7297

Assisted-by: AI
Signed-off-by: Alex-Wengg <[email protected]>
Build the call with `pop.external_call` directly, as `free` does, so it
carries the `noreturn` function attribute. LLVM then knows the call does
not return and drops the infinite loop that follows it, which is kept
only for the Mojo noreturn checker.

Assisted-by: AI
Signed-off-by: Alex-Wengg <[email protected]>
Declare the libc `exit` call with a `Never` result instead of hand-building
a `noreturn` call followed by a loop. The frontend accepts the call as the
end of the function, so the loop is no longer needed.

Assisted-by: AI
Signed-off-by: Alex-Wengg <[email protected]>

@NathanSWard NathanSWard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

@NathanSWard

Copy link
Copy Markdown
Contributor

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Nov 15, 2026
@modularbot

Copy link
Copy Markdown
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally labels Nov 15, 2026
modularbot pushed a commit that referenced this pull request Nov 16, 2026
…12094)

[External] [stdlib] Declare `std.sys.exit()` as returning `Never`

## Linked issue

Associated issue: #7297

## Type of change

- [x] Bug fix (non-breaking change that fixes an issue)

## issue

`std.sys.exit()` never returns, but it is declared as returning `None`.
A function with a result type that ends in a call to `exit()` therefore
fails to compile with `return expected at end of function with results`,
as in the issue's `parse_or_exit` example. The only workaround is to add
a dead `return`.

## fix

- Both `exit()` overloads in `std/sys/terminate.mojo` now return
`Never`.
- The underlying `std.sys._libc.exit` binding now returns `Never`. As
with `_abort_base()`, an infinite loop after the `external_call`
satisfies the noreturn checker.

Existing callers aren't affected: a `Never` call is still valid as a
statement.

ORIGINAL_AUTHOR=A&W <[email protected]>
ORIGINAL_USER=@Alex-Wengg
PUBLIC_PR_HEAD=242231ecebe720ea1604946b7dcbe84d6dd1b332

Co-authored-by: A&W <[email protected]>
Co-authored-by: NathanSWard <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Pull-Request: #7379
MODULAR_ORIG_COMMIT_REV_ID: e1ca940ec353f25dbb2335d07830bd6d21bba30e
@modularbot

Copy link
Copy Markdown
Collaborator

Landed in 5fc6bac! Thank you for your contribution 🎉

@modularbot
modularbot merged commit 2896002 into modular:main Nov 16, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally mojo-docs mojo-stdlib Tag for issues related to standard library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] stdlib: exit isn't declared -> Never

4 participants