mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Update unit testing post for new panic handler
This commit is contained in:
@@ -24,23 +24,20 @@ Unfortunately it's a bit more complicated for `no_std` applications such as our
|
|||||||
|
|
||||||
```
|
```
|
||||||
> cargo test
|
> cargo test
|
||||||
Compiling blog_os v0.2.0 (file:///home/philipp/Documents/blog_os)
|
Compiling blog_os v0.2.0 (file:///…/blog_os)
|
||||||
error[E0152]: duplicate lang item found: `panic_fmt`.
|
error[E0152]: duplicate lang item found: `panic_impl`.
|
||||||
--> src/main.rs:26:1
|
--> src/main.rs:35:1
|
||||||
|
|
|
|
||||||
26 | / pub extern "C" fn rust_begin_panic(
|
35 | / pub fn panic(info: &PanicInfo) -> ! {
|
||||||
27 | | _msg: core::fmt::Arguments,
|
36 | | println!("{}", info);
|
||||||
28 | | _file: &'static str,
|
37 | | loop {}
|
||||||
29 | | _line: u32,
|
38 | | }
|
||||||
... |
|
|
||||||
32 | | loop {}
|
|
||||||
33 | | }
|
|
||||||
| |_^
|
| |_^
|
||||||
|
|
|
|
||||||
= note: first defined in crate `std`.
|
= note: first defined in crate `std`.
|
||||||
```
|
```
|
||||||
|
|
||||||
The problem is that unit tests are built for the host machine, with the `std` library included. This makes sense because they should be able to run as a normal application on the host operating system. Since the standard library has it's own implementation of the `panic_fmt` language item, we get the above error. To fix it, we use [conditional compilation] to include our implementation of the language item only in non-test environments:
|
The problem is that unit tests are built for the host machine, with the `std` library included. This makes sense because they should be able to run as a normal application on the host operating system. Since the standard library has it's own `panic_implementation` function, we get the above error. To fix it, we use [conditional compilation] to include our implementation of the panic handler only in non-test environments:
|
||||||
|
|
||||||
[conditional compilation]: https://doc.rust-lang.org/reference/attributes.html#conditional-compilation
|
[conditional compilation]: https://doc.rust-lang.org/reference/attributes.html#conditional-compilation
|
||||||
|
|
||||||
@@ -49,14 +46,10 @@ The problem is that unit tests are built for the host machine, with the `std` li
|
|||||||
// in src/main.rs
|
// in src/main.rs
|
||||||
|
|
||||||
#[cfg(not(test))] // only compile when the test flag is not set
|
#[cfg(not(test))] // only compile when the test flag is not set
|
||||||
#[lang = "panic_fmt"]
|
#[panic_implementation]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn rust_begin_panic(
|
pub fn panic(info: &PanicInfo) -> ! {
|
||||||
_msg: core::fmt::Arguments,
|
println!("{}", info);
|
||||||
_file: &'static str,
|
|
||||||
_line: u32,
|
|
||||||
_column: u32,
|
|
||||||
) -> ! {
|
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub extern "C" fn _start() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This function is called on panic.
|
/// This function is called on panic.
|
||||||
|
#[cfg(not(test))]
|
||||||
#[panic_implementation]
|
#[panic_implementation]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn panic(info: &PanicInfo) -> ! {
|
pub fn panic(info: &PanicInfo) -> ! {
|
||||||
|
|||||||
Reference in New Issue
Block a user