Compiler marks import as unnecessary when it isn't (panic_handler)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
lib.rs:
#![no_std]
#[cfg_attr(not(test), panic_handler)]
pub fn panic_handler(_info: &core::panic::PanicInfo<'_>) -> ! {
loop {}
}
main.rs:
#![no_std]
#![no_main]
use playground;
Cargo.toml:
[package]
name = "playground"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[profile.dev]
panic = "abort"
Upon running cargo check it gives this output:
warning: unused import: `playground`
--> src/main.rs:4:5
|
4 | use playground;
| ^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: `playground` (bin "playground") generated 1 warning (run `cargo fix --bin "playground"` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
This is incorrect, as the import provides panic_handler for main, even though it is not directly imported.
To get a minimal reproduction you can run:
git clone --branch panic_handler_import_bug https://github.com/mysteriouslyseeing/playground.git
Note that cargo build will fail at linkage with that minimal reproduction due to the #![no_main] present. An alternate entry point would have to be specified based on the binaries' target to allow cargo build to work.
Meta
rustc --version --verbose: (stable)
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
rustc --version --verbose: (nightly)
rustc 1.81.0-nightly (032be6f7b 2024-07-16)
binary: rustc
commit-hash: 032be6f7bbe091c7dfa29f115e94b9cc9bae1758
commit-date: 2024-07-16
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
cargo fix:
Checking playground v0.0.1 (/home/wschroeder/Projects/playground)
warning: failed to automatically apply fixes suggested by rustc to crate `playground`
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error: `#[panic_handler]` function required, but not found
error: aborting due to 1 previous error
Original diagnostics will follow.
warning: unused import: `playground`
--> src/main.rs:4:5
|
4 | use playground;
| ^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: `playground` (bin "playground") generated 1 warning (run `cargo fix --bin "playground"` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the warning with the lib.rs, main.rs, and Cargo.toml setup from the issue by running cargo check. Then trace the compiler's unused-import analysis for the panic_handler dependency; done means the import is not marked unnecessary and the panic handler remains available without the warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100