Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unconditional_recursion lint false positive on abort-on-drop type. #138897

Open
chorman0773 opened this issue Mar 24, 2025 · 5 comments
Open

unconditional_recursion lint false positive on abort-on-drop type. #138897

chorman0773 opened this issue Mar 24, 2025 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints L-unconditional_recursion Lint: unconditional_recursion T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chorman0773
Copy link
Contributor

Code

struct DropBomb;

impl Drop for DropBomb {
    fn drop(&mut self) {
        let _this = DropBomb;
        panic!()
    }
}

Current output

warning: function cannot return without recursing
 --> src/lib.rs:5:5
  |
5 |     fn drop(&mut self) {
  |     ^^^^^^^^^^^^^^^^^^ cannot return without recursing
...
8 |     }
  |     - recursive call site
  |
  = help: a `loop` may express intention better if this is on purpose
  = note: `#[warn(unconditional_recursion)]` on by default

Desired output

No output; compiles as intended.

Rationale and extra context

The lint is semantically incorrect, in that the recursion occurs at most once (after that, the entire program is aborted due to double-panic). The help message is also not useful, because replacing the panic!() with a loop {} changes the program from aborting when dropping DropBomb, to hanging indefinitely.

This is a common construction in no_std for an AbortOnDrop type, usually to stop a program from unwinding out of a piece of unsafe code and leaving broken invariants.

Other cases

Rust Version

rustc 1.87.0-nightly (aa8f0fd71 2025-03-23)
binary: rustc
commit-hash: aa8f0fd7163a2f23aa958faed30c9c2b77b934a5
commit-date: 2025-03-23
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1

Anything else?

Playground Link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=8dbe0e6fb92d8f66787d7c3f14c386a5

@chorman0773 chorman0773 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2025
@chorman0773
Copy link
Contributor Author

@rustbot label +L-unconditional_recursion

@rustbot rustbot added the L-unconditional_recursion Lint: unconditional_recursion label Mar 24, 2025
@tmiasko
Copy link
Contributor

tmiasko commented Mar 25, 2025

The warning is correct about recursion with -Cpanic=unwind. Nested panics are allowed since 1.71.

@Jules-Bertholet
Copy link
Contributor

Yes, testing on playground shows that this does in fact recurse forever.

@chorman0773
Copy link
Contributor Author

Wasn't that defined to abort since forever?
Is there no way to abort on unwind from no_std then? I'm pretty sure that breaks just about any crate that relies on no-unwind for soundness.

@tmiasko
Copy link
Contributor

tmiasko commented Mar 30, 2025

It is possible to abort by panicking in a function that cannot unwind:

pub extern "C" fn abort() {
  panic!()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints L-unconditional_recursion Lint: unconditional_recursion T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants