rust-analyzer: Incorrect build issue displayed: "found duplicate lang item `panic_impl`"

I am following the writing an OS in Rust tutorials and in the first section it describes creating a no_std binary and you must specify the panic handler. The following code compiles file but rust-analyzer believes there is an error and shows it in VSCode.

#![no_std]
#![no_main]

use core::panic::PanicInfo;

/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

rsz_screenshot_from_2020-05-16_17-46-26

found duplicate lang item `panic_impl`
the lang item is first defined in crate `std` (which `test` depends on)rustc(E0152)

I tried setting the target to one that doesn’t link with the OS, settings.json:

 "rust-analyzer.cargo.target": "thumbv7em-none-eabihf"

Rust-analyzer extension version: 0.2.166

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Sorry to necro this issue, but I had the same issue as the OP and I think I solved it using #[cfg(not(test))]:

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

The function is then greyed-out within VSCode.

You can change the checkOnSave. settings, for example for rustc development:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output"
    ]
}

In your case, maybe something like this will works:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./cargo",
        "xcheck",
        "--json-output"
    ]
}

(Not sure xcheck support --json-output)

Does cargo check --all-targets result in an error? If so you can set rust-analyzer.checkOnSave.allTargets to false.

I’m a bit confused as to the solution. Where is this checkOnSave attribute overridden? Can I do this on a per project basis?

ctrl-shift-p > “Preferences: Open Settings (JSON)” for the global settings. ctrl-shift-p > “Preferences: Open Workspace Settings (JSON)” or .vscode/settings.json for the workspace settings.