[resolved] Test with no std

I’m making a toy OS with rust and in order to compile i need to put this functions in my lib.rs

#![feature(lang_items)] #![no_std] ...
#[allow(non_snake_case)] #[no_mangle] pub extern "C" fn _Unwind_Resume() -> ! { loop {} }
#[lang = "eh_personality"] extern "C" fn eh_personality() {}
#[lang = "panic_fmt"] #[no_mangle] pub extern "C" fn panic_fmt() -> ! { loop {} }

and every thing is working but now i want to write some cargo tests so as a start i created this function

#[test] fn it_works() { const i: u8 = 99; assert_eq!(88, i); }

now when i run cargo test i get error [E0152] because of the functions i put earlier

duplicate lang item found: eh_personality first defined in crate panic_unwind
duplicate lang item found: panic_fmt first defined in crate std

as you can see panic_fmt is defined in crate std but i don’t use std, if i remove the functions from the lib.rs i can run testes but i’m no longer able to compile and vise versa.
so how can i fix this ?

Try doing a regular build with a main function instead of using the testing framework. It may be linking in its own panic implementation independent of yours, since tests rely on panics to signal failure.

im sorry but i think you get me wrong i don’t have a main function its a lib i call my main function “kmain” from assembly

You could add #[cfg(not(test))] to each of your lang item fns, to disable them when building tests:

#[cfg(not(test))] #[allow(non_snake_case)] #[no_mangle] pub extern "C" fn _Unwind_Resume() -> ! { loop {} } 
#[cfg(not(test))] #[lang = "eh_personality"] extern "C" fn eh_personality() {} 
#[cfg(not(test))] #[lang = "panic_fmt"] #[no_mangle] pub extern "C" fn panic_fmt() -> ! { loop {} } 
2 Likes

I put all of mine in a module to make this easier.

thank you mbrubeck, i’m new here so how can i close this ?

You can edit the title to add a marker like “[resolved]”