From 48acdb719731ba98a1cc5635d26d08cbe8809e4b Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Sun, 4 Apr 2021 22:52:34 +0800 Subject: [PATCH] Add tests for unwind no_std. --- .../unwind-msvc-no-std-link-to-libcmt.rs | 47 +++++++++++++++++++ .../unwind-msvc-no-std-link-to-msvcrt.rs | 47 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs create mode 100644 src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs diff --git a/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs new file mode 100644 index 0000000000000..b27b95ee050f9 --- /dev/null +++ b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-libcmt.rs @@ -0,0 +1,47 @@ +// build-pass +// compile-flags: -C panic=unwind -C target-feature=+crt-static +// only-msvc +// Test that `no_std` with `panic=unwind` under MSVC toolchain +// doesn't cause error when linking to libcmt. + +#![no_std] +#![no_main] +#![feature(alloc_error_handler)] +#![feature(panic_unwind)] + +use core::alloc::{GlobalAlloc, Layout}; + +struct DummyAllocator; + +unsafe impl GlobalAlloc for DummyAllocator { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +#[global_allocator] +static ALLOC: DummyAllocator = DummyAllocator; + +#[alloc_error_handler] +fn rust_oom(_layout: Layout) -> ! { + panic!() +} + +extern crate panic_unwind; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "libcmt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + panic!(); +} diff --git a/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs new file mode 100644 index 0000000000000..95036e5774cee --- /dev/null +++ b/src/test/ui/panic-runtime/unwind-msvc-no-std-link-to-msvcrt.rs @@ -0,0 +1,47 @@ +// build-pass +// compile-flags: -C panic=unwind +// only-msvc +// Test that `no_std` with `panic=unwind` under MSVC toolchain +// doesn't cause error when linking to msvcrt. + +#![no_std] +#![no_main] +#![feature(alloc_error_handler)] +#![feature(panic_unwind)] + +use core::alloc::{GlobalAlloc, Layout}; + +struct DummyAllocator; + +unsafe impl GlobalAlloc for DummyAllocator { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +#[global_allocator] +static ALLOC: DummyAllocator = DummyAllocator; + +#[alloc_error_handler] +fn rust_oom(_layout: Layout) -> ! { + panic!() +} + +extern crate panic_unwind; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(_: &PanicInfo) -> ! { + loop {} +} + +#[link(name = "msvcrt")] +extern "C" {} + +#[no_mangle] +pub extern "C" fn main() -> i32 { + panic!(); +}