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 {}
}

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
- dev: analyzer "duplicate lang item `panic_impl`" https://github.com/rust-analyzer/rust-analyzer/issues/4490 — committed to setros/bootcom by abdes 4 years ago
- Work around the VSCode rust-analyzer error. It was displaying a "duplicate lang item" error on the panic handler. This was also displayed when running "cargo check --all-targets". The workaround to a... — committed to mstange/samply by mstange 2 years ago
Sorry to necro this issue, but I had the same issue as the OP and I think I solved it using
#[cfg(not(test))]:The function is then greyed-out within VSCode.
You can change the
checkOnSave.settings, for example for rustc development:In your case, maybe something like this will works:
(Not sure xcheck support --json-output)
Does
cargo check --all-targetsresult in an error? If so you can setrust-analyzer.checkOnSave.allTargetsto false.I’m a bit confused as to the solution. Where is this
checkOnSaveattribute 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.jsonfor the workspace settings.