diff options
| author | bors <bors@rust-lang.org> | 2017-01-10 06:50:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-01-10 06:50:59 +0000 |
| commit | 78c892d8659ae1cf1717b9a8a4bb407d8667f672 (patch) | |
| tree | 0a27becbef4ae12a18f01390607e28ce63e5faf6 | |
| parent | 2fa9feb37a06abf96f4aeee7d92888135319d546 (diff) | |
| parent | 893f42a83466cf02b6fd6d3c82d5419cdad47474 (diff) | |
| download | rust-78c892d8659ae1cf1717b9a8a4bb407d8667f672.tar.gz rust-78c892d8659ae1cf1717b9a8a4bb407d8667f672.zip | |
Auto merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic
Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.
r? @steveklabnik
| -rw-r--r-- | src/doc/book/lang-items.md | 11 | ||||
| -rw-r--r-- | src/doc/book/no-stdlib.md | 10 |
2 files changed, 10 insertions, 11 deletions
diff --git a/src/doc/book/lang-items.md b/src/doc/book/lang-items.md index 6a08c1b6bb4..0d6a142ca4f 100644 --- a/src/doc/book/lang-items.md +++ b/src/doc/book/lang-items.md @@ -16,15 +16,12 @@ and one for deallocation. A freestanding program that uses the `Box` sugar for dynamic allocations via `malloc` and `free`: ```rust,ignore -#![feature(lang_items, box_syntax, start, libc)] +#![feature(lang_items, box_syntax, start, libc, core_intrinsics)] #![no_std] +use core::intrinsics; extern crate libc; -extern { - fn abort() -> !; -} - #[lang = "owned_box"] pub struct Box<T>(*mut T); @@ -34,7 +31,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 { // Check if `malloc` failed: if p as usize == 0 { - abort(); + intrinsics::abort(); } p @@ -58,7 +55,7 @@ fn main(argc: isize, argv: *const *const u8) -> isize { } #[lang = "eh_personality"] extern fn rust_eh_personality() {} -#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { loop {} } +#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { unsafe { intrinsics::abort() } } # #[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {} # #[no_mangle] pub extern fn rust_eh_register_frames () {} # #[no_mangle] pub extern fn rust_eh_unregister_frames () {} diff --git a/src/doc/book/no-stdlib.md b/src/doc/book/no-stdlib.md index a06de35c0ce..79f0593be17 100644 --- a/src/doc/book/no-stdlib.md +++ b/src/doc/book/no-stdlib.md @@ -37,9 +37,10 @@ The function marked `#[start]` is passed the command line parameters in the same format as C: ```rust,ignore -#![feature(lang_items)] +#![feature(lang_items, core_intrinsics)] #![feature(start)] #![no_std] +use core::intrinsics; // Pull in the system libc library for what crt0.o likely requires. extern crate libc; @@ -69,7 +70,7 @@ pub extern fn rust_eh_unwind_resume() { pub extern fn rust_begin_panic(_msg: core::fmt::Arguments, _file: &'static str, _line: u32) -> ! { - loop {} + unsafe { intrinsics::abort() } } ``` @@ -79,10 +80,11 @@ correct ABI and the correct name, which requires overriding the compiler's name mangling too: ```rust,ignore -#![feature(lang_items)] +#![feature(lang_items, core_intrinsics)] #![feature(start)] #![no_std] #![no_main] +use core::intrinsics; // Pull in the system libc library for what crt0.o likely requires. extern crate libc; @@ -112,7 +114,7 @@ pub extern fn rust_eh_unwind_resume() { pub extern fn rust_begin_panic(_msg: core::fmt::Arguments, _file: &'static str, _line: u32) -> ! { - loop {} + unsafe { intrinsics::abort() } } ``` |
