dtolnay / no-panic

Attribute macro to require that the compiler prove a function can't ever panic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no-panic finds panics where there are none in barebones project

ZeeQyu opened this issue · comments

commented

Hi,

I wanted to make sure I didn't have any panicking functions in my game project, but it seems like no-panic conflicts with bevy somehow. If I add #[no_panic] to any function that is added as a system to bevy, it always finds a panic in it, even if the function is completely empty.

See attached minimal reproducible example, which doesn't compile on my computer, with stable rustc 1.66.0 (69f9c33d7 2022-12-12). The problem reproduces even without the DefaultPlugins.

no-panic-bevy-bug.zip

The function I pass my empty function to has the signature
pub fn add_system(&mut self, system: impl IntoSystemDescriptor) -> &mut Self {
self.add_system_to_stage(CoreStage::Update, system)
}
and bevy does some macro magic to let you "request" different resources and queries by just adding them as parameters, so that's probably related, but I don't know enough rust to give competent guesses as to why.

This might also be a ticket for bevy, but I figured I'd go here first since this might be a problem that generalizes to more libraries.

commented

Sorry, might have misdiagnosed this. no_panic doesn't seem to work at all on my system, it always finds an error unless the function is unused.
I'm on Manjaro KDE, see screenshot for version numbers.

See the below log for repro, and here's a zip of that project no-panic.zip

[zeeqyu@zeeqyu-manjaro no-panic]$ cargo --version
cargo 1.66.0 (d65d197ad 2022-11-15)
[zeeqyu@zeeqyu-manjaro no-panic]$ rustc --version
rustc 1.66.0 (69f9c33d7 2022-12-12)
[zeeqyu@zeeqyu-manjaro no-panic]$ ls -l
total 16
-rw-r--r-- 1 zeeqyu zeeqyu 1344 24 feb 23.09 Cargo.lock
-rw-r--r-- 1 zeeqyu zeeqyu  197 24 feb 23.09 Cargo.toml
drwxr-xr-x 2 zeeqyu zeeqyu 4096 24 feb 23.09 src
drwxr-xr-x 3 zeeqyu zeeqyu 4096 24 feb 23.09 target
[zeeqyu@zeeqyu-manjaro no-panic]$ cat Cargo.toml
[package]
name = "no-panic"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
no-panic = "0.1.18"
[zeeqyu@zeeqyu-manjaro no-panic]$ cat src/main.rs 
use no_panic::no_panic;
#[no_panic]
fn main() {
    println!("Hello, world!");
}
[zeeqyu@zeeqyu-manjaro no-panic]$ cargo run
   Compiling no-panic v0.1.0 (/home/zeeqyu/Workspace/no-panic)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcXFbLjM/symbols.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.1pn1qlbqitwyn7pv.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.1taan5iukwkjnfix.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.3dsoxlzie01h2o3u.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.3f0qrurzyrzh1zbh.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.4l3sy0bnfdua24j9.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.4ssp9azontj8jhtb.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.bj8z9d0whkxlupl.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.hjq2bldysqx32et.rcgu.o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.4mt45byivokodjh9.rcgu.o" "-Wl,--as-needed" "-L" "/home/zeeqyu/Workspace/no-panic/target/debug/deps" "-L" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-a11e3ca400b3ed09.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-3e82a3fced649488.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-53a4330185981bcb.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-2a8b57667b4852b5.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-9370462deca12c5a.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-7da763b8d3620472.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-5bde27582a7f5af7.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-1204e05b2d47e3d7.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-43987de2766b6923.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-d6499a0705316aa5.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-c9a27c90d8fbf11e.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-8f159929cbfdfaf1.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-d2f1e8f3bb5cba95.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-9862f486269f442f.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-0434381f2f012ae2.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-68549403a59fd02e.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4cefb2045f924a5b.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-272615fc4f10c50d.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-860619b93700e7eb.rlib" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b73e5b4656934876.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/zeeqyu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: /home/zeeqyu/Workspace/no-panic/target/debug/deps/no_panic-c662a79595996694.bj8z9d0whkxlupl.rcgu.o: in function `<no_panic::main::__NoPanic as core::ops::drop::Drop>::drop':
          /home/zeeqyu/Workspace/no-panic/src/main.rs:2: undefined reference to `
          
          ERROR[no-panic]: detected panic in function `main`
          '
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `no-panic` due to previous error
[zeeqyu@zeeqyu-manjaro no-panic]$ 

image

commented

This also happens even if I attach it to a function that isn't main:

use no_panic::no_panic;
fn main() {
    empty()
}
#[no_panic]
fn empty() {
    println!("Hello, world!");
}

You're building in debug mode. The behavior is almost certainly covered by https://github.com/dtolnay/no-panic#caveats.

commented

I get the same behaviour build with cargo run --release, and an empty function shouldn't require optimization to not panic, so either I'm missing something fundamental about what it means to build in debug mode, or there'sa bug that at least affects arch.

But the signal you're sending is that you're not interested in engaging with this bug report, so I'll respect that and leave you be.

Regarding the code in #41 (comment), no-panic is definitely correct triggering in that case. println will panic if the process's stdout has been closed by the parent process or the write system call fails for any other reason.

As a general rule, this crate does not have false positives. It tells you a function can panic if and only if rustc thinks the function can panic. There can obviously be cases where your understanding of whether a function can panic disagrees with rustc's understanding, and those would be between you and rustc (or llvm) to sort out.