Repository navigation
[stdlib] Declare std.sys.exit() as returning Never - #7379
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
ef83caa to
448e831
Compare
barcharcraz
left a comment
There was a problem hiding this comment.
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.
| # Unreachable since the call above is noreturn, but we need to satisfy | ||
| # the noreturn checker. | ||
| while True: | ||
| pass |
There was a problem hiding this comment.
this is still required?
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
_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]>
5f67fd1 to
242231e
Compare
NathanSWard
left a comment
There was a problem hiding this comment.
This looks great, thanks!
|
!sync |
|
✅🟣 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. |
…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
|
Landed in 5fc6bac! Thank you for your contribution 🎉 |
Linked issue
Fixes #7297
Type of change
issue
std.sys.exit()never returns, but it is declared as returningNone. A function with a result type that ends in a call toexit()therefore fails to compile withreturn expected at end of function with results, as in the issue'sparse_or_exitexample. The only workaround is to add a deadreturn.fix
exit()overloads instd/sys/terminate.mojonow returnNever.std.sys._libc.exitbinding now returnsNever. As with_abort_base(), an infinite loop after theexternal_callsatisfies the noreturn checker.Existing callers aren't affected: a
Nevercall is still valid as a statement.verification
test/sys/test_exit_0.mojonow contains a function with a result type that ends instd.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.mojostill printsRunning...and exits with status 1.42andnot 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
mojo formaton the changed filesAssisted-by:trailer in my commit message or this PR descriptionAssisted-by: AI
🤖 Generated with Claude Code