rust-lang / rust-lang/backtrace-rs

`backtrace` brings in `std` even with `std` feature disabled

Open
#564 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
628
Forks
291
PR merge metrics
No merged PRs in 30d

Description

I reproduced this with a minimal #![no_std] executable that does nothing but return an exit code:

main.rs
#![no_std]
#![no_main]

#[no_mangle]
fn main() -> i32 {
    42
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

cargo run works as expected (exits with code 42) with the following Cargo.toml, which includes the backtrace dependency (with default-features = false so it doesn't have the std feature):

Cargo.toml
[package]
name = "example"
version = "0.1.0"
edition = "2021"

[dependencies]
backtrace = { version = "0.3.69", default-features = false }

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

At least in macOS, I needed to add compiler flags for this to run; here is my .cargo/config.toml

.cargo/config.toml
[target.'cfg(target_os = "macos")']
rustflags = [
  "-C",
  "link-arg=-e", # -e _main sets the entrypoint symbol since we're #[no_main]
  "-C",
  "link-arg=_main",
  "-C",
  "link-arg=-lSystem", # all macOS apps need -lSystem
  "-C", # Note: force-unwind-tables is not needed to reproduce
  "force-unwind-tables",
]

At this point, if I actually try to use backtrace - doing nothing else but adding use to main.rs like so:

use backtrace::trace_unsynchronized;

...now cargo check fails, saying that backtrace is bringing in the std crate which introduces a conflicting panic_impl implementation:

warning: unused import: `backtrace::trace_unsynchronized`
 --> src/main.rs:4:5
  |
4 | use backtrace::trace_unsynchronized;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0152]: found duplicate lang item `panic_impl`
  --> src/main.rs:12:1
   |
12 | fn panic(_info: &core::panic::PanicInfo) -> ! {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the lang item is first defined in crate `std` (which `backtrace` depends on)
   = note: first definition in `std` loaded from /Users/rtfeldman/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-1990072ee01a7130.rlib
   = note: second definition in the local crate (`example`)

For more information about this error, try `rustc --explain E0152`.

Note that the error[E0152]: found duplicate lang item 'panic_impl' explicitly says the lang item is first defined in crate 'std' (which 'backtrace' depends on), and that everything worked until I added the (unused) use backtrace::trace_unsynchronized; declaration. Also, I know the std feature is off, because if I try to import something that needs it (e.g. backtrace::Backtrace), I get an error.

This is with stable rustc 1.68.0 (2c8cc3432 2023-03-06) and cargo 1.68.0 (115f34552 2023-02-26).

Unfortunately, this seems to make it impossible to use backtrace in a no_std executable - unless I'm missing something!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal no_std main.rs and Cargo.toml in the report, then inspect how importing backtrace::trace_unsynchronized enables the dependency graph despite default-features = false. Reproduce with the stated Rust and Cargo versions; done means cargo check succeeds with the import without a duplicate panic_impl from std.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.