Auto merge of #2255 - dtolnay-contrib:rustfmt7, r=oli-obk

Format tests with rustfmt (288-299 of 299)

Extracted from #2097.

I'll make a separate PR to enable checking the `tests` directory's formatting in CI. I'll need to rebase that after both this and #2254 have landed, and if any new non-rustfmt-formatted files appear in the meantime, we can include formatting those in the same PR that enables the CI.
This commit is contained in:
bors 2022-06-22 10:04:08 +00:00
commit 3d1d345f8f
12 changed files with 169 additions and 138 deletions

View File

@ -6,7 +6,8 @@
struct MuchAlign;
fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
// Try many times as this might work by chance.
for _ in 0..10 {
let buf = [0u32; 256];
// `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not
// for the actual alignment required by `MuchAlign`.
@ -14,7 +15,9 @@ fn main() {
// as the reference is not aligned to its dynamic alignment requirements.
let mut ptr = &MuchAlign as &dyn std::fmt::Debug;
// Overwrite the data part of `ptr` so it points to `buf`.
unsafe { (&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8); }
unsafe {
(&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8);
}
// Re-borrow that. This should be UB.
let _ptr = &*ptr; //~ERROR alignment 256 is required
}

View File

@ -10,11 +10,9 @@ struct Foo {
}
fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
let foo = Foo {
x: 42,
y: 99,
};
// Try many times as this might work by chance.
for _ in 0..10 {
let foo = Foo { x: 42, y: 99 };
let p = &foo.x;
let i = *p; //~ERROR alignment 4 is required
}

View File

@ -2,7 +2,8 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
// Try many times as this might work by chance.
for _ in 0..10 {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.

View File

@ -2,7 +2,8 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
// Try many times as this might work by chance.
for _ in 0..10 {
let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
// This must fail because alignment is violated. Test specifically for loading pointers,

View File

@ -4,7 +4,9 @@
fn main() {
// Make sure we notice when a u16 is loaded at offset 1 into a u8 allocation.
// (This would be missed if u8 allocations are *always* at odd addresses.)
for _ in 0..10 { // Try many times as this might work by chance.
//
// Try many times as this might work by chance.
for _ in 0..10 {
let x = [0u8; 4];
let ptr = x.as_ptr().wrapping_offset(1).cast::<u16>();
let _val = unsafe { *ptr }; //~ERROR but alignment

View File

@ -3,7 +3,8 @@
use std::ptr;
fn main() {
for _ in 0..10 { // Try many times as this might work by chance.
// Try many times as this might work by chance.
for _ in 0..10 {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.

View File

@ -3,7 +3,8 @@
// compile-flags: -Zmir-opt-level=0 -Zmiri-disable-validation
fn main() {
for i in 0..10 { // Try many times as this might work by chance.
// Try many times as this might work by chance.
for i in 0..10 {
let x = i as u8;
let x = &x as *const _ as *const [u32; 0];
// This must fail because alignment is violated. Test specifically for loading ZST.

View File

@ -13,110 +13,110 @@ use std::intrinsics::*;
pub fn main() {
unsafe {
assert_eq!(ctpop(0u8), 0); assert_eq!(ctpop(0i8), 0);
assert_eq!(ctpop(0u16), 0); assert_eq!(ctpop(0i16), 0);
assert_eq!(ctpop(0u32), 0); assert_eq!(ctpop(0i32), 0);
assert_eq!(ctpop(0u64), 0); assert_eq!(ctpop(0i64), 0);
[assert_eq!(ctpop(0u8), 0), assert_eq!(ctpop(0i8), 0)];
[assert_eq!(ctpop(0u16), 0), assert_eq!(ctpop(0i16), 0)];
[assert_eq!(ctpop(0u32), 0), assert_eq!(ctpop(0i32), 0)];
[assert_eq!(ctpop(0u64), 0), assert_eq!(ctpop(0i64), 0)];
assert_eq!(ctpop(1u8), 1); assert_eq!(ctpop(1i8), 1);
assert_eq!(ctpop(1u16), 1); assert_eq!(ctpop(1i16), 1);
assert_eq!(ctpop(1u32), 1); assert_eq!(ctpop(1i32), 1);
assert_eq!(ctpop(1u64), 1); assert_eq!(ctpop(1i64), 1);
[assert_eq!(ctpop(1u8), 1), assert_eq!(ctpop(1i8), 1)];
[assert_eq!(ctpop(1u16), 1), assert_eq!(ctpop(1i16), 1)];
[assert_eq!(ctpop(1u32), 1), assert_eq!(ctpop(1i32), 1)];
[assert_eq!(ctpop(1u64), 1), assert_eq!(ctpop(1i64), 1)];
assert_eq!(ctpop(10u8), 2); assert_eq!(ctpop(10i8), 2);
assert_eq!(ctpop(10u16), 2); assert_eq!(ctpop(10i16), 2);
assert_eq!(ctpop(10u32), 2); assert_eq!(ctpop(10i32), 2);
assert_eq!(ctpop(10u64), 2); assert_eq!(ctpop(10i64), 2);
[assert_eq!(ctpop(10u8), 2), assert_eq!(ctpop(10i8), 2)];
[assert_eq!(ctpop(10u16), 2), assert_eq!(ctpop(10i16), 2)];
[assert_eq!(ctpop(10u32), 2), assert_eq!(ctpop(10i32), 2)];
[assert_eq!(ctpop(10u64), 2), assert_eq!(ctpop(10i64), 2)];
assert_eq!(ctpop(100u8), 3); assert_eq!(ctpop(100i8), 3);
assert_eq!(ctpop(100u16), 3); assert_eq!(ctpop(100i16), 3);
assert_eq!(ctpop(100u32), 3); assert_eq!(ctpop(100i32), 3);
assert_eq!(ctpop(100u64), 3); assert_eq!(ctpop(100i64), 3);
[assert_eq!(ctpop(100u8), 3), assert_eq!(ctpop(100i8), 3)];
[assert_eq!(ctpop(100u16), 3), assert_eq!(ctpop(100i16), 3)];
[assert_eq!(ctpop(100u32), 3), assert_eq!(ctpop(100i32), 3)];
[assert_eq!(ctpop(100u64), 3), assert_eq!(ctpop(100i64), 3)];
assert_eq!(ctpop(-1i8 as u8), 8); assert_eq!(ctpop(-1i8), 8);
assert_eq!(ctpop(-1i16 as u16), 16); assert_eq!(ctpop(-1i16), 16);
assert_eq!(ctpop(-1i32 as u32), 32); assert_eq!(ctpop(-1i32), 32);
assert_eq!(ctpop(-1i64 as u64), 64); assert_eq!(ctpop(-1i64), 64);
[assert_eq!(ctpop(-1i8 as u8), 8), assert_eq!(ctpop(-1i8), 8)];
[assert_eq!(ctpop(-1i16 as u16), 16), assert_eq!(ctpop(-1i16), 16)];
[assert_eq!(ctpop(-1i32 as u32), 32), assert_eq!(ctpop(-1i32), 32)];
[assert_eq!(ctpop(-1i64 as u64), 64), assert_eq!(ctpop(-1i64), 64)];
assert_eq!(ctlz(0u8), 8); assert_eq!(ctlz(0i8), 8);
assert_eq!(ctlz(0u16), 16); assert_eq!(ctlz(0i16), 16);
assert_eq!(ctlz(0u32), 32); assert_eq!(ctlz(0i32), 32);
assert_eq!(ctlz(0u64), 64); assert_eq!(ctlz(0i64), 64);
[assert_eq!(ctlz(0u8), 8), assert_eq!(ctlz(0i8), 8)];
[assert_eq!(ctlz(0u16), 16), assert_eq!(ctlz(0i16), 16)];
[assert_eq!(ctlz(0u32), 32), assert_eq!(ctlz(0i32), 32)];
[assert_eq!(ctlz(0u64), 64), assert_eq!(ctlz(0i64), 64)];
assert_eq!(ctlz(1u8), 7); assert_eq!(ctlz(1i8), 7);
assert_eq!(ctlz(1u16), 15); assert_eq!(ctlz(1i16), 15);
assert_eq!(ctlz(1u32), 31); assert_eq!(ctlz(1i32), 31);
assert_eq!(ctlz(1u64), 63); assert_eq!(ctlz(1i64), 63);
[assert_eq!(ctlz(1u8), 7), assert_eq!(ctlz(1i8), 7)];
[assert_eq!(ctlz(1u16), 15), assert_eq!(ctlz(1i16), 15)];
[assert_eq!(ctlz(1u32), 31), assert_eq!(ctlz(1i32), 31)];
[assert_eq!(ctlz(1u64), 63), assert_eq!(ctlz(1i64), 63)];
assert_eq!(ctlz(10u8), 4); assert_eq!(ctlz(10i8), 4);
assert_eq!(ctlz(10u16), 12); assert_eq!(ctlz(10i16), 12);
assert_eq!(ctlz(10u32), 28); assert_eq!(ctlz(10i32), 28);
assert_eq!(ctlz(10u64), 60); assert_eq!(ctlz(10i64), 60);
[assert_eq!(ctlz(10u8), 4), assert_eq!(ctlz(10i8), 4)];
[assert_eq!(ctlz(10u16), 12), assert_eq!(ctlz(10i16), 12)];
[assert_eq!(ctlz(10u32), 28), assert_eq!(ctlz(10i32), 28)];
[assert_eq!(ctlz(10u64), 60), assert_eq!(ctlz(10i64), 60)];
assert_eq!(ctlz(100u8), 1); assert_eq!(ctlz(100i8), 1);
assert_eq!(ctlz(100u16), 9); assert_eq!(ctlz(100i16), 9);
assert_eq!(ctlz(100u32), 25); assert_eq!(ctlz(100i32), 25);
assert_eq!(ctlz(100u64), 57); assert_eq!(ctlz(100i64), 57);
[assert_eq!(ctlz(100u8), 1), assert_eq!(ctlz(100i8), 1)];
[assert_eq!(ctlz(100u16), 9), assert_eq!(ctlz(100i16), 9)];
[assert_eq!(ctlz(100u32), 25), assert_eq!(ctlz(100i32), 25)];
[assert_eq!(ctlz(100u64), 57), assert_eq!(ctlz(100i64), 57)];
assert_eq!(ctlz_nonzero(1u8), 7); assert_eq!(ctlz_nonzero(1i8), 7);
assert_eq!(ctlz_nonzero(1u16), 15); assert_eq!(ctlz_nonzero(1i16), 15);
assert_eq!(ctlz_nonzero(1u32), 31); assert_eq!(ctlz_nonzero(1i32), 31);
assert_eq!(ctlz_nonzero(1u64), 63); assert_eq!(ctlz_nonzero(1i64), 63);
[assert_eq!(ctlz_nonzero(1u8), 7), assert_eq!(ctlz_nonzero(1i8), 7)];
[assert_eq!(ctlz_nonzero(1u16), 15), assert_eq!(ctlz_nonzero(1i16), 15)];
[assert_eq!(ctlz_nonzero(1u32), 31), assert_eq!(ctlz_nonzero(1i32), 31)];
[assert_eq!(ctlz_nonzero(1u64), 63), assert_eq!(ctlz_nonzero(1i64), 63)];
assert_eq!(ctlz_nonzero(10u8), 4); assert_eq!(ctlz_nonzero(10i8), 4);
assert_eq!(ctlz_nonzero(10u16), 12); assert_eq!(ctlz_nonzero(10i16), 12);
assert_eq!(ctlz_nonzero(10u32), 28); assert_eq!(ctlz_nonzero(10i32), 28);
assert_eq!(ctlz_nonzero(10u64), 60); assert_eq!(ctlz_nonzero(10i64), 60);
[assert_eq!(ctlz_nonzero(10u8), 4), assert_eq!(ctlz_nonzero(10i8), 4)];
[assert_eq!(ctlz_nonzero(10u16), 12), assert_eq!(ctlz_nonzero(10i16), 12)];
[assert_eq!(ctlz_nonzero(10u32), 28), assert_eq!(ctlz_nonzero(10i32), 28)];
[assert_eq!(ctlz_nonzero(10u64), 60), assert_eq!(ctlz_nonzero(10i64), 60)];
assert_eq!(ctlz_nonzero(100u8), 1); assert_eq!(ctlz_nonzero(100i8), 1);
assert_eq!(ctlz_nonzero(100u16), 9); assert_eq!(ctlz_nonzero(100i16), 9);
assert_eq!(ctlz_nonzero(100u32), 25); assert_eq!(ctlz_nonzero(100i32), 25);
assert_eq!(ctlz_nonzero(100u64), 57); assert_eq!(ctlz_nonzero(100i64), 57);
[assert_eq!(ctlz_nonzero(100u8), 1), assert_eq!(ctlz_nonzero(100i8), 1)];
[assert_eq!(ctlz_nonzero(100u16), 9), assert_eq!(ctlz_nonzero(100i16), 9)];
[assert_eq!(ctlz_nonzero(100u32), 25), assert_eq!(ctlz_nonzero(100i32), 25)];
[assert_eq!(ctlz_nonzero(100u64), 57), assert_eq!(ctlz_nonzero(100i64), 57)];
assert_eq!(cttz(-1i8 as u8), 0); assert_eq!(cttz(-1i8), 0);
assert_eq!(cttz(-1i16 as u16), 0); assert_eq!(cttz(-1i16), 0);
assert_eq!(cttz(-1i32 as u32), 0); assert_eq!(cttz(-1i32), 0);
assert_eq!(cttz(-1i64 as u64), 0); assert_eq!(cttz(-1i64), 0);
[assert_eq!(cttz(-1i8 as u8), 0), assert_eq!(cttz(-1i8), 0)];
[assert_eq!(cttz(-1i16 as u16), 0), assert_eq!(cttz(-1i16), 0)];
[assert_eq!(cttz(-1i32 as u32), 0), assert_eq!(cttz(-1i32), 0)];
[assert_eq!(cttz(-1i64 as u64), 0), assert_eq!(cttz(-1i64), 0)];
assert_eq!(cttz(0u8), 8); assert_eq!(cttz(0i8), 8);
assert_eq!(cttz(0u16), 16); assert_eq!(cttz(0i16), 16);
assert_eq!(cttz(0u32), 32); assert_eq!(cttz(0i32), 32);
assert_eq!(cttz(0u64), 64); assert_eq!(cttz(0i64), 64);
[assert_eq!(cttz(0u8), 8), assert_eq!(cttz(0i8), 8)];
[assert_eq!(cttz(0u16), 16), assert_eq!(cttz(0i16), 16)];
[assert_eq!(cttz(0u32), 32), assert_eq!(cttz(0i32), 32)];
[assert_eq!(cttz(0u64), 64), assert_eq!(cttz(0i64), 64)];
assert_eq!(cttz(1u8), 0); assert_eq!(cttz(1i8), 0);
assert_eq!(cttz(1u16), 0); assert_eq!(cttz(1i16), 0);
assert_eq!(cttz(1u32), 0); assert_eq!(cttz(1i32), 0);
assert_eq!(cttz(1u64), 0); assert_eq!(cttz(1i64), 0);
[assert_eq!(cttz(1u8), 0), assert_eq!(cttz(1i8), 0)];
[assert_eq!(cttz(1u16), 0), assert_eq!(cttz(1i16), 0)];
[assert_eq!(cttz(1u32), 0), assert_eq!(cttz(1i32), 0)];
[assert_eq!(cttz(1u64), 0), assert_eq!(cttz(1i64), 0)];
assert_eq!(cttz(10u8), 1); assert_eq!(cttz(10i8), 1);
assert_eq!(cttz(10u16), 1); assert_eq!(cttz(10i16), 1);
assert_eq!(cttz(10u32), 1); assert_eq!(cttz(10i32), 1);
assert_eq!(cttz(10u64), 1); assert_eq!(cttz(10i64), 1);
[assert_eq!(cttz(10u8), 1), assert_eq!(cttz(10i8), 1)];
[assert_eq!(cttz(10u16), 1), assert_eq!(cttz(10i16), 1)];
[assert_eq!(cttz(10u32), 1), assert_eq!(cttz(10i32), 1)];
[assert_eq!(cttz(10u64), 1), assert_eq!(cttz(10i64), 1)];
assert_eq!(cttz(100u8), 2); assert_eq!(cttz(100i8), 2);
assert_eq!(cttz(100u16), 2); assert_eq!(cttz(100i16), 2);
assert_eq!(cttz(100u32), 2); assert_eq!(cttz(100i32), 2);
assert_eq!(cttz(100u64), 2); assert_eq!(cttz(100i64), 2);
[assert_eq!(cttz(100u8), 2), assert_eq!(cttz(100i8), 2)];
[assert_eq!(cttz(100u16), 2), assert_eq!(cttz(100i16), 2)];
[assert_eq!(cttz(100u32), 2), assert_eq!(cttz(100i32), 2)];
[assert_eq!(cttz(100u64), 2), assert_eq!(cttz(100i64), 2)];
assert_eq!(cttz_nonzero(-1i8 as u8), 0); assert_eq!(cttz_nonzero(-1i8), 0);
assert_eq!(cttz_nonzero(-1i16 as u16), 0); assert_eq!(cttz_nonzero(-1i16), 0);
assert_eq!(cttz_nonzero(-1i32 as u32), 0); assert_eq!(cttz_nonzero(-1i32), 0);
assert_eq!(cttz_nonzero(-1i64 as u64), 0); assert_eq!(cttz_nonzero(-1i64), 0);
[assert_eq!(cttz_nonzero(-1i8 as u8), 0), assert_eq!(cttz_nonzero(-1i8), 0)];
[assert_eq!(cttz_nonzero(-1i16 as u16), 0), assert_eq!(cttz_nonzero(-1i16), 0)];
[assert_eq!(cttz_nonzero(-1i32 as u32), 0), assert_eq!(cttz_nonzero(-1i32), 0)];
[assert_eq!(cttz_nonzero(-1i64 as u64), 0), assert_eq!(cttz_nonzero(-1i64), 0)];
assert_eq!(cttz_nonzero(1u8), 0); assert_eq!(cttz_nonzero(1i8), 0);
assert_eq!(cttz_nonzero(1u16), 0); assert_eq!(cttz_nonzero(1i16), 0);
assert_eq!(cttz_nonzero(1u32), 0); assert_eq!(cttz_nonzero(1i32), 0);
assert_eq!(cttz_nonzero(1u64), 0); assert_eq!(cttz_nonzero(1i64), 0);
[assert_eq!(cttz_nonzero(1u8), 0), assert_eq!(cttz_nonzero(1i8), 0)];
[assert_eq!(cttz_nonzero(1u16), 0), assert_eq!(cttz_nonzero(1i16), 0)];
[assert_eq!(cttz_nonzero(1u32), 0), assert_eq!(cttz_nonzero(1i32), 0)];
[assert_eq!(cttz_nonzero(1u64), 0), assert_eq!(cttz_nonzero(1i64), 0)];
assert_eq!(cttz_nonzero(10u8), 1); assert_eq!(cttz_nonzero(10i8), 1);
assert_eq!(cttz_nonzero(10u16), 1); assert_eq!(cttz_nonzero(10i16), 1);
assert_eq!(cttz_nonzero(10u32), 1); assert_eq!(cttz_nonzero(10i32), 1);
assert_eq!(cttz_nonzero(10u64), 1); assert_eq!(cttz_nonzero(10i64), 1);
[assert_eq!(cttz_nonzero(10u8), 1), assert_eq!(cttz_nonzero(10i8), 1)];
[assert_eq!(cttz_nonzero(10u16), 1), assert_eq!(cttz_nonzero(10i16), 1)];
[assert_eq!(cttz_nonzero(10u32), 1), assert_eq!(cttz_nonzero(10i32), 1)];
[assert_eq!(cttz_nonzero(10u64), 1), assert_eq!(cttz_nonzero(10i64), 1)];
assert_eq!(cttz_nonzero(100u8), 2); assert_eq!(cttz_nonzero(100i8), 2);
assert_eq!(cttz_nonzero(100u16), 2); assert_eq!(cttz_nonzero(100i16), 2);
assert_eq!(cttz_nonzero(100u32), 2); assert_eq!(cttz_nonzero(100i32), 2);
assert_eq!(cttz_nonzero(100u64), 2); assert_eq!(cttz_nonzero(100i64), 2);
[assert_eq!(cttz_nonzero(100u8), 2), assert_eq!(cttz_nonzero(100i8), 2)];
[assert_eq!(cttz_nonzero(100u16), 2), assert_eq!(cttz_nonzero(100i16), 2)];
[assert_eq!(cttz_nonzero(100u32), 2), assert_eq!(cttz_nonzero(100i32), 2)];
[assert_eq!(cttz_nonzero(100u64), 2), assert_eq!(cttz_nonzero(100i64), 2)];
assert_eq!(bswap(0x0Au8), 0x0A); // no-op
assert_eq!(bswap(0x0Ai8), 0x0A); // no-op
@ -127,20 +127,20 @@ pub fn main() {
assert_eq!(bswap(0x0122334455667708u64), 0x0877665544332201);
assert_eq!(bswap(0x0122334455667708i64), 0x0877665544332201);
assert_eq!(exact_div(9*9u32, 3), 27);
assert_eq!(exact_div(-9*9i32, 3), -27);
assert_eq!(exact_div(9*9i8, -3), -27);
assert_eq!(exact_div(-9*9i64, -3), 27);
assert_eq!(exact_div(9 * 9u32, 3), 27);
assert_eq!(exact_div(-9 * 9i32, 3), -27);
assert_eq!(exact_div(9 * 9i8, -3), -27);
assert_eq!(exact_div(-9 * 9i64, -3), 27);
assert_eq!(unchecked_div(9*9u32, 2), 40);
assert_eq!(unchecked_div(-9*9i32, 2), -40);
assert_eq!(unchecked_div(9*9i8, -2), -40);
assert_eq!(unchecked_div(-9*9i64, -2), 40);
assert_eq!(unchecked_div(9 * 9u32, 2), 40);
assert_eq!(unchecked_div(-9 * 9i32, 2), -40);
assert_eq!(unchecked_div(9 * 9i8, -2), -40);
assert_eq!(unchecked_div(-9 * 9i64, -2), 40);
assert_eq!(unchecked_rem(9*9u32, 2), 1);
assert_eq!(unchecked_rem(-9*9i32, 2), -1);
assert_eq!(unchecked_rem(9*9i8, -2), 1);
assert_eq!(unchecked_rem(-9*9i64, -2), -1);
assert_eq!(unchecked_rem(9 * 9u32, 2), 1);
assert_eq!(unchecked_rem(-9 * 9i32, 2), -1);
assert_eq!(unchecked_rem(9 * 9i8, -2), 1);
assert_eq!(unchecked_rem(-9 * 9i64, -2), -1);
assert_eq!(unchecked_add(23u8, 19), 42);
assert_eq!(unchecked_add(5, -10), -5);

View File

@ -3,8 +3,8 @@
#![feature(never_type)]
#![allow(unconditional_panic, non_fmt_panics)]
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::cell::Cell;
use std::panic::{catch_unwind, AssertUnwindSafe};
thread_local! {
static MY_COUNTER: Cell<usize> = Cell::new(0);
@ -59,23 +59,29 @@ fn main() {
test(None, |old_val| core::panic!("Hello from panic: {:?}", old_val));
// Built-in panics; also make sure the message is right.
test(
Some("index out of bounds: the len is 3 but the index is 4"),
|_old_val| { let _val = [0, 1, 2][4]; loop {} },
);
test(
Some("attempt to divide by zero"),
|_old_val| { let _val = 1/0; loop {} },
);
test(Some("index out of bounds: the len is 3 but the index is 4"), |_old_val| {
let _val = [0, 1, 2][4];
loop {}
});
test(Some("attempt to divide by zero"), |_old_val| {
let _val = 1 / 0;
loop {}
});
test(
Some("align_offset: align is not a power-of-two"),
|_old_val| { (0usize as *const u8).align_offset(3); loop {} },
);
test(Some("align_offset: align is not a power-of-two"), |_old_val| {
(0usize as *const u8).align_offset(3);
loop {}
});
// Assertion and debug assertion
test(None, |_old_val| { assert!(false); loop {} });
test(None, |_old_val| { debug_assert!(false); loop {} });
test(None, |_old_val| {
assert!(false);
loop {}
});
test(None, |_old_val| {
debug_assert!(false);
loop {}
});
eprintln!("Success!"); // Make sure we get this in stderr
}
@ -89,7 +95,8 @@ fn test(expect_msg: Option<&str>, do_panic: impl FnOnce(usize) -> !) {
let res = catch_unwind(AssertUnwindSafe(|| {
let _string = "LEAKED FROM CLOSURE".to_string();
do_panic_counter(do_panic)
})).expect_err("do_panic() did not panic!");
}))
.expect_err("do_panic() did not panic!");
// See if we can extract the panic message.
let msg = if let Some(s) = res.downcast_ref::<String>() {

View File

@ -5,12 +5,14 @@ fn eq_ref<T>(x: &T, y: &T) -> bool {
x as *const _ == y as *const _
}
fn f() -> i32 { 42 }
fn f() -> i32 {
42
}
fn ptr_int_casts() {
// int-ptr-int
assert_eq!(1 as *const i32 as usize, 1);
assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4*4);
assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4 * 4);
// negative overflowing wrapping_offset (going through memory because
// this used to trigger an ICE on 32bit)
@ -18,7 +20,8 @@ fn ptr_int_casts() {
*val = (1 as *const u8).wrapping_offset(-4);
assert_eq!(*val as usize, usize::MAX - 2);
{ // ptr-int-ptr
// ptr-int-ptr
{
let x = 13;
let mut y = &x as &_ as *const _ as usize;
y += 13;
@ -27,13 +30,14 @@ fn ptr_int_casts() {
assert!(eq_ref(&x, unsafe { &*y }));
}
{ // fnptr-int-fnptr
let x : fn() -> i32 = f;
let y : *mut u8 = unsafe { mem::transmute(x as fn() -> i32) };
// fnptr-int-fnptr
{
let x: fn() -> i32 = f;
let y: *mut u8 = unsafe { mem::transmute(x as fn() -> i32) };
let mut y = y as usize;
y += 13;
y -= 13;
let x : fn() -> i32 = unsafe { mem::transmute(y as *mut u8) };
let x: fn() -> i32 = unsafe { mem::transmute(y as *mut u8) };
assert_eq!(x(), 42);
}
@ -51,13 +55,13 @@ fn ptr_int_ops() {
// bit-operations, covered by alignment
assert_eq!(x & 1, 0);
assert_eq!(x & 0, 0);
assert_eq!(1 & (x+1), 1);
assert_eq!(1 & (x + 1), 1);
let _y = !1 & x;
let _y = !0 & x;
let _y = x & !1;
// remainder, covered by alignment
assert_eq!(x % 2, 0);
assert_eq!((x+1) % 2, 1);
assert_eq!((x + 1) % 2, 1);
// remainder with 1 is always 0
assert_eq!(x % 1, 0);
}

View File

@ -16,15 +16,23 @@
pub fn main() {
fn explicit() {
fn test<F>(_x: Option<Box<F>>) where F: FnMut(Box<dyn for<'a> FnMut(&'a isize)>) {}
fn test<F>(_x: Option<Box<F>>)
where
F: FnMut(Box<dyn for<'a> FnMut(&'a isize)>),
{
}
test(Some(box |_f: Box<dyn for<'a> FnMut(&'a isize)>| {}));
}
// The code below is shorthand for the code above (and more likely
// to represent what one encounters in practice).
fn implicit() {
fn test<F>(_x: Option<Box<F>>) where F: FnMut(Box<dyn FnMut(& isize)>) {}
test(Some(box |_f: Box<dyn FnMut(& isize)>| {}));
fn test<F>(_x: Option<Box<F>>)
where
F: FnMut(Box<dyn FnMut(&isize)>),
{
}
test(Some(box |_f: Box<dyn FnMut(&isize)>| {}));
}
explicit();

View File

@ -1,12 +1,17 @@
#[derive(Debug, PartialEq)]
enum Unit { Unit(()) } // Force non-C-enum representation.
enum Unit {
Unit(()), // Force non-C-enum representation.
}
fn return_unit() -> Unit {
Unit::Unit(())
}
#[derive(Debug, PartialEq)]
enum MyBool { False(()), True(()) } // Force non-C-enum representation.
enum MyBool {
False(()), // Force non-C-enum representation.
True(()),
}
fn return_true() -> MyBool {
MyBool::True(())