Skip to content

refactor: Add pass for converting exit and abort#1587

Merged
randomPoison merged 10 commits intomasterfrom
legare/convert-exits
Feb 20, 2026
Merged

refactor: Add pass for converting exit and abort#1587
randomPoison merged 10 commits intomasterfrom
legare/convert-exits

Conversation

@randomPoison
Copy link
Copy Markdown
Contributor

Convert calls to libc abort and exit to std::process::abort and std::process::exit. Part of #1564.

This comment was marked as resolved.

@randomPoison randomPoison changed the title Add refactor pass for converting exit and abort to their std equivalents Add refactor pass for converting exit and abort Feb 4, 2026
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
match func_name {
"abort" => {
*e = mk().span(e.span).call_expr(
mk().path_expr(vec!["", "std", "process", "abort"]),
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.

Could we go further and call panic!? It's not exactly equivalent when panic unwinds, but we might want that anyway.

panic! will even take a message, and show a backtrace.

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 could be enabled with a CLI arg maybe?

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.

Hm, I like this idea, panic! seems like a more idiomatic error exit than abort. But maybe we should do that as a separate pass, maybe one that turns std abort into panic!? At least it feels to me like those should be separate transforms for a couple of reasons:

  • The libc -> std translations is semantics-preserving and guaranteed to be valid. Going to panic! introduces unwinding that wasn't in the original (though I suspect that's fine, and is likely the direction later lifting would go in anyway).
  • There might be some amount of judgement we want to apply in deciding which aborts should become panic!s. abort is still valid in Rust, and there are some cases where you might use that instead of a panic, so blindly transforming all aborts into panics might not be right in all cases (though off the top of my head I can't think of an example where we'd want to keep abort over panic).
  • When we introduce panic! we may also want to make changes to surrounding code, e.g. if the code is printing out an error immediately before aborting we might want to merge that into the panic! invocation. It's unclear to me if that's something sensible to do in c2rust-refactor or if that needs to be an LLM-directed lifting.

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.

It makes sense to separate them, I'm even wondering if the current version should be two separate commands.

Comment thread c2rust-refactor/src/transform/exits.rs
Comment on lines +11 to +32
unsafe {
::std::process::abort();
}
}

fn test_exit() {
unsafe {
::std::process::exit(0);
}
}

fn test_exit_error() {
unsafe {
::std::process::exit(1);
}
}

fn test_exit_variable() {
let code = 42;
unsafe {
::std::process::exit(code);
}
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.

+1, I was going to comment this exact thing. Could we remove the unsafe?

Alternatively, I think we have a transform elsewhere for that, but I don't know if it still works. cargo fix might also handle it.

Comment thread c2rust-refactor/src/transform/exits.rs Outdated
@kkysen kkysen changed the title Add refactor pass for converting exit and abort refactor: Add pass for converting exit and abort Feb 6, 2026
@randomPoison randomPoison added the refactorer This issue relates to the refactoring tool label Feb 17, 2026
Copy link
Copy Markdown
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

LGTM now! Just some small nits.

Comment thread c2rust-refactor/src/ast_manip/visit_node.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Comment thread c2rust-refactor/src/transform/exits.rs
Comment thread c2rust-refactor/src/transform/exits.rs Outdated
Copy link
Copy Markdown
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

Oh wait, actually can this be updated to use the new snapshot tests now?

@randomPoison
Copy link
Copy Markdown
Contributor Author

Ah yeah, if the snapshot PR already landed I can rebase and update the tests.

@randomPoison randomPoison merged commit d016d67 into master Feb 20, 2026
11 checks passed
@randomPoison randomPoison deleted the legare/convert-exits branch February 20, 2026 22:14
}

#[test]
fn test_convert_exits() {
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.

Can you keep these alphabetically sorted? e < m

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.

Sure, let me throw up another PR for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactorer This issue relates to the refactoring tool

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants