-1

For a no_std application there are a few language items defined in lang_items.rs, one of them being the panic_fmt language item (to specify the behavior of panic! in this no_std context) defined like:

#[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }

When compiling, I receive this error:

error[E0522]: definition of an unknown language item: `panic_fmt`
 --> src/lang_items.rs:3:1
  |
3 | #[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }
  | ^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `panic_fmt`

error: `#[panic_implementation]` function required, but not found

After reading RFC 2070 I learned there was a recent breaking change for no_std/embedded programs. While it's recommended that I use the #[panic_implementation] attributes, a recently added feature, I still receive an error, doing so like:

#[panic_implementation] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }

Gives the error:

error[E0658]: #[panic_implementation] is an unstable feature (see issue #44489)
 --> src/lang_items.rs:4:1
  |
4 | #[panic_implementation] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: add #![feature(panic_implementation)] to the crate attributes to enable

Following their suggestion of adding #![feature(panic_implementation)] to the top of the lang_items.rs file doesn't seem to do the trick, as I'm getting the same error. How do I enable this unstable feature properly so that I can compile this no_std application with behavior for panic! defined?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dynsne
  • 341
  • 1
  • 11
  • What is "lang_items.rs"? That's nothing standard. – Shepmaster Aug 20 '18 at 18:32
  • 1
    Please review how to create a [MCVE] and then [edit] your question to include it. We cannot tell what crates, types, traits, fields, etc. are present in the code. Try to produce something that reproduces your error on the [Rust Playground](https://play.rust-lang.org) or you can reproduce it in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) as well. – Shepmaster Aug 20 '18 at 18:33
  • Do you know that `feature`s require a nightly compiler? – mcarton Aug 20 '18 at 18:34
  • Yes, I'm using a nightly compiler :-). – dynsne Aug 20 '18 at 18:35
  • 1
    *Following their suggestion of adding `#![feature(panic_implementation)]` to the top of the lang_items.rs file* — that is **not** the suggestion. Possible duplicate of [What is a crate attribute and where do I add it?](https://stackoverflow.com/q/27454761/155423). – Shepmaster Aug 20 '18 at 18:44
  • Thanks shepmaster, I read your answer earlier and then realized what I was doing wrong. – dynsne Aug 20 '18 at 18:46

1 Answers1

1

Okay, it was a simple mistake I was making. Here is a link to the playground containing my code. First of all, I needed to add the crate attribute to the top of my crate root (which, for me, was lib.rs). lang_items.rs was just a file that was used in lib.rs like so:

#![feature(compiler_builtins_lib, lang_items, asm, panic_implementation, core_intrinsics)]
#![no_builtins]
#![no_std]

pub mod lang_items;

const GPIO_BASE: usize = 0x3F000000 + 0x200000;

const GPIO_FSEL1: *mut u32 = (GPIO_BASE + 0x04) as *mut u32;
const GPIO_SET0: *mut u32 = (GPIO_BASE + 0x1C) as *mut u32;
const GPIO_CLR0: *mut u32 = (GPIO_BASE + 0x28) as *mut u32;

#[inline(never)]
fn spin_sleep_ms(ms: usize) {
    for _ in 0..(ms * 600) {
        unsafe { asm!("nop" :::: "volatile"); }
    }
}

#[no_mangle]
pub unsafe extern "C" fn kmain() {
    // STEP 1: Set GPIO Pin 16 as output.
    GPIO_FSEL1.write_volatile(0x1 << 0x12);
    // STEP 2: Continuously set and clear GPIO 16.
    loop {
        GPIO_SET0.write_volatile(0x1 << 0x10);
        spin_sleep_ms(256);
        GPIO_CLR0.write_volatile(0x1 << 0x10);
        spin_sleep_ms(256);
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dynsne
  • 341
  • 1
  • 11
  • 1
    There's no need to duplicate answers when an existing Q&A has already solved the problem. That just increases the amount of maintenance needed to keep Q&A useful. – Shepmaster Aug 20 '18 at 18:48