vscode-rust icon indicating copy to clipboard operation
vscode-rust copied to clipboard

Issue when defining a panic function with #![no_std]

Open GRVYDEV opened this issue 4 years ago • 6 comments

Hello,

I am currently trying to write an operating system in Rust. Obviously I can't link to the std lib and I need to write my own panic function. As of right now the plugin is giving me an error saying that it found a duplicate panic_impl item however it compiles fine. Is there a way you could check for #![no_std] and prevent this error from showing up? Or is there any way I can configure the plugin to ignore this error?

Error: found duplicate lang item `panic_impl` the lang item is first defined in crate `std` (which `test` depends on) first definition in `std` loaded from /home/grvy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-93cbfed54dd1bac8.rlib second definition in the local crate (`grvy_os`)rustc(E0152)

Code:

#![no_std] // don't link to the Rust std library
#![no_main] // disable all Rust-level entry points

use core::panic::PanicInfo;

#[no_mangle] // don't mangle the name of this function
pub extern "C" fn _start() -> ! {
    // this function is the entry point, since the linker looks for a function 
    // named '_start' by default
    loop {}
}

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

GRVYDEV avatar Apr 07 '21 15:04 GRVYDEV

Looks like the compiler might end up using the equivalent of --all-targets. Amusingly, rust-analyzer has the same "problem" by default.

lnicola avatar Apr 07 '21 16:04 lnicola

Yes, I am having the same issue. #![no_std] gives me this false error: image There is #729 which was closed with this fix:

{
    "rust.target": "thumbv7em-none-eabihf",
    "rust.all_targets": false
}

That issue should not have been closed because this doesn't work for me (probably because I use rust-analyzer). It says Unknown Configuration Setting. Another fix that was mentioned in that issue that does work for me is this:

{
    "rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
    "rust-analyzer.checkOnSave.allTargets": false
}

However, this does not only disable the error for #![non_std], but all errors and warnings. Also, it looks like a dirty hack to me. Most of the time the thumbv7em-none-eabihf target isn't what you are actually working with.

I hope vscode-rust/rust-analyzer will properly fix this issue and support #![non_std].

wooster0 avatar Apr 08 '21 09:04 wooster0

@r00ster91 matklad.rust-analyzer is independent of this extension (rust-lang.rust), so don't mix them -- and feel free to file any rust-analyzer specific issues over there.

In your case I think "rust-analyzer.checkOnSave.allTargets": false should suffice.

lnicola avatar Apr 08 '21 09:04 lnicola

Unfortunately neither of these fixes works for me so I guess Ill just have to deal with it for now

GRVYDEV avatar Apr 09 '21 17:04 GRVYDEV

I am also having this same issue with no luck fixing it

ryanfuji avatar Jul 15 '21 18:07 ryanfuji

Guys please fix it! This really annoying (((

udik-chudik avatar Feb 03 '22 21:02 udik-chudik