dtolnay / dtolnay/no-panic

False positive when annotating (non-tail?)-recursive functions

Open
#56 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
1.2k
Forks
20
PR merge metrics
No merged PRs in 30d

Description

When annotating a function that calls itself recursively, a `no-panic` linker error is reported no matter if the function actually does panic or not.

## Steps to reproduce

Consider the following rust program:

```rust
#[no_panic::no_panic]
#[inline(never)]
// Attempt to make a nontrivial recursive function; on high opt levels this still gets compiled to
// a tail recursive function in many cases... improvement suggestions welcome!
pub fn collatz(n: usize) -> usize {
if n == 1 {
0
} else if n % 2 == 0 {
collatz(n/2) + 2
} else {
collatz(3*n + 1) + 1
}
}

pub fn main() {
let n = std::hint::black_box(collatz(std::hint::black_box(42)));
println!("{}", n);
}

```

This produces a linker error:

```
ERROR[no-panic]: detected panic in function `collatz`
```

The behavior doesn't change for higher optimization levels, LTO, etc.

This might only happen for functions that are not tail recursive? Tricky to know for sure if this plays a role or not; I haven't been able to conclusively tell from the codegen behavior.

Maybe the drop guard detection pattern behaves differently for recursive functions?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.