Skip to content

Commit e66220f

Browse files
committed
Reduce the number of unstable features in tests
1 parent f5e0b76 commit e66220f

File tree

2 files changed

+34
-70
lines changed

2 files changed

+34
-70
lines changed

src/test/ui/allocator/no_std-alloc-error-handler-custom.rs

+19-38
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
// compile-flags:-C panic=abort
88
// aux-build:helper.rs
99

10-
#![feature(start, rustc_private, new_uninit, panic_info_message, lang_items)]
10+
#![feature(rustc_private, lang_items)]
1111
#![feature(alloc_error_handler)]
1212
#![no_std]
13+
#![no_main]
1314

1415
extern crate alloc;
1516
extern crate libc;
@@ -21,35 +22,30 @@ pub fn __aeabi_unwind_cpp_pr0() {}
2122
#[no_mangle]
2223
pub fn __aeabi_unwind_cpp_pr1() {}
2324

24-
use core::ptr::null_mut;
25-
use core::alloc::{GlobalAlloc, Layout};
2625
use alloc::boxed::Box;
26+
use alloc::string::ToString;
27+
use core::alloc::{GlobalAlloc, Layout};
28+
use core::ptr::null_mut;
2729

2830
extern crate helper;
2931

3032
struct MyAllocator;
3133

3234
#[alloc_error_handler]
33-
fn my_oom(layout: Layout) -> !
34-
{
35+
fn my_oom(layout: Layout) -> ! {
3536
use alloc::fmt::write;
3637
unsafe {
3738
let size = layout.size();
3839
let mut s = alloc::string::String::new();
3940
write(&mut s, format_args!("My OOM: failed to allocate {} bytes!\n", size)).unwrap();
40-
let s = s.as_str();
41-
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
41+
libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len());
4242
libc::exit(0)
4343
}
4444
}
4545

4646
unsafe impl GlobalAlloc for MyAllocator {
4747
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
48-
if layout.size() < 4096 {
49-
libc::malloc(layout.size()) as _
50-
} else {
51-
null_mut()
52-
}
48+
if layout.size() < 4096 { libc::malloc(layout.size()) as _ } else { null_mut() }
5349
}
5450
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
5551
}
@@ -60,26 +56,12 @@ static A: MyAllocator = MyAllocator;
6056
#[panic_handler]
6157
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
6258
unsafe {
63-
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
64-
const PSTR: &str = "panic occurred: ";
65-
const CR: &str = "\n";
66-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
67-
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
68-
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
69-
}
70-
if let Some(args) = panic_info.message() {
71-
let mut s = alloc::string::String::new();
72-
alloc::fmt::write(&mut s, *args).unwrap();
73-
let s = s.as_str();
74-
const PSTR: &str = "panic occurred: ";
75-
const CR: &str = "\n";
76-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
77-
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
78-
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
79-
} else {
80-
const PSTR: &str = "panic occurred\n";
81-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
82-
}
59+
let s = panic_info.to_string();
60+
const PSTR: &str = "panic occurred: ";
61+
const CR: &str = "\n";
62+
libc::write(libc::STDERR_FILENO, PSTR.as_ptr() as *const _, PSTR.len());
63+
libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len());
64+
libc::write(libc::STDERR_FILENO, CR.as_ptr() as *const _, CR.len());
8365
libc::exit(1)
8466
}
8567
}
@@ -89,15 +71,14 @@ fn panic(panic_info: &core::panic::PanicInfo) -> ! {
8971
// in these libraries will refer to `rust_eh_personality` if LLVM can not *prove* the contents won't
9072
// unwind. So, for this test case we will define the symbol.
9173
#[lang = "eh_personality"]
92-
extern fn rust_eh_personality() {}
74+
extern "C" fn rust_eh_personality() {}
9375

94-
#[derive(Debug)]
76+
#[derive(Default, Debug)]
9577
struct Page(#[allow(unused_tuple_struct_fields)] [[u64; 32]; 16]);
9678

97-
#[start]
98-
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
99-
let zero = Box::<Page>::new_zeroed();
100-
let zero = unsafe { zero.assume_init() };
79+
#[no_mangle]
80+
fn main(_argc: i32, _argv: *const *const u8) -> isize {
81+
let zero = Box::<Page>::new(Default::default());
10182
helper::work_with(&zero);
10283
1
10384
}

src/test/ui/allocator/no_std-alloc-error-handler-default.rs

+15-32
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
// compile-flags:-C panic=abort
88
// aux-build:helper.rs
99

10-
#![feature(start, rustc_private, new_uninit, panic_info_message, lang_items)]
10+
#![feature(rustc_private, lang_items)]
1111
#![no_std]
12+
#![no_main]
1213

1314
extern crate alloc;
1415
extern crate libc;
@@ -21,6 +22,7 @@ pub fn __aeabi_unwind_cpp_pr0() {}
2122
pub fn __aeabi_unwind_cpp_pr1() {}
2223

2324
use alloc::boxed::Box;
25+
use alloc::string::ToString;
2426
use core::alloc::{GlobalAlloc, Layout};
2527
use core::ptr::null_mut;
2628

@@ -30,11 +32,7 @@ struct MyAllocator;
3032

3133
unsafe impl GlobalAlloc for MyAllocator {
3234
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
33-
if layout.size() < 4096 {
34-
libc::malloc(layout.size()) as _
35-
} else {
36-
null_mut()
37-
}
35+
if layout.size() < 4096 { libc::malloc(layout.size()) as _ } else { null_mut() }
3836
}
3937
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
4038
}
@@ -45,26 +43,12 @@ static A: MyAllocator = MyAllocator;
4543
#[panic_handler]
4644
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
4745
unsafe {
48-
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
49-
const PSTR: &str = "panic occurred: ";
50-
const CR: &str = "\n";
51-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
52-
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
53-
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
54-
}
55-
if let Some(args) = panic_info.message() {
56-
let mut s = alloc::string::String::new();
57-
alloc::fmt::write(&mut s, *args).unwrap();
58-
let s = s.as_str();
59-
const PSTR: &str = "panic occurred: ";
60-
const CR: &str = "\n";
61-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
62-
libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len());
63-
libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len());
64-
} else {
65-
const PSTR: &str = "panic occurred\n";
66-
libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len());
67-
}
46+
let s = panic_info.to_string();
47+
const PSTR: &str = "panic occurred: ";
48+
const CR: &str = "\n";
49+
libc::write(libc::STDERR_FILENO, PSTR.as_ptr() as *const _, PSTR.len());
50+
libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len());
51+
libc::write(libc::STDERR_FILENO, CR.as_ptr() as *const _, CR.len());
6852
libc::exit(0)
6953
}
7054
}
@@ -74,15 +58,14 @@ fn panic(panic_info: &core::panic::PanicInfo) -> ! {
7458
// in these libraries will refer to `rust_eh_personality` if LLVM can not *prove* the contents won't
7559
// unwind. So, for this test case we will define the symbol.
7660
#[lang = "eh_personality"]
77-
extern fn rust_eh_personality() {}
61+
extern "C" fn rust_eh_personality() {}
7862

79-
#[derive(Debug)]
63+
#[derive(Default, Debug)]
8064
struct Page(#[allow(unused_tuple_struct_fields)] [[u64; 32]; 16]);
8165

82-
#[start]
83-
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
84-
let zero = Box::<Page>::new_zeroed();
85-
let zero = unsafe { zero.assume_init() };
66+
#[no_mangle]
67+
fn main(_argc: i32, _argv: *const *const u8) -> isize {
68+
let zero = Box::<Page>::new(Default::default());
8669
helper::work_with(&zero);
8770
1
8871
}

0 commit comments

Comments
 (0)