2016-05-16 23:26:18 +00:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-01-22 18:08:57 +00:00
|
|
|
// revisions:lexical nll
|
|
|
|
#![cfg_attr(nll, feature(nll))]
|
|
|
|
|
2017-10-23 03:01:00 +00:00
|
|
|
// ignore-wasm32-bare compiled with panic=abort by default
|
|
|
|
|
2018-02-24 19:21:33 +00:00
|
|
|
#![feature(generators, generator_trait, untagged_unions)]
|
|
|
|
#![feature(slice_patterns)]
|
2016-05-29 19:01:06 +00:00
|
|
|
|
|
|
|
use std::cell::{Cell, RefCell};
|
2017-10-17 12:20:22 +00:00
|
|
|
use std::ops::Generator;
|
2016-05-29 19:01:06 +00:00
|
|
|
use std::panic;
|
|
|
|
use std::usize;
|
|
|
|
|
|
|
|
struct InjectedFailure;
|
2016-05-16 23:26:18 +00:00
|
|
|
|
|
|
|
struct Allocator {
|
|
|
|
data: RefCell<Vec<bool>>,
|
2016-05-29 19:01:06 +00:00
|
|
|
failing_op: usize,
|
|
|
|
cur_ops: Cell<usize>,
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:01:06 +00:00
|
|
|
impl panic::UnwindSafe for Allocator {}
|
|
|
|
impl panic::RefUnwindSafe for Allocator {}
|
|
|
|
|
2016-05-16 23:26:18 +00:00
|
|
|
impl Drop for Allocator {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let data = self.data.borrow();
|
|
|
|
if data.iter().any(|d| *d) {
|
|
|
|
panic!("missing free: {:?}", data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Allocator {
|
2016-05-29 19:01:06 +00:00
|
|
|
fn new(failing_op: usize) -> Self {
|
|
|
|
Allocator {
|
|
|
|
failing_op: failing_op,
|
|
|
|
cur_ops: Cell::new(0),
|
|
|
|
data: RefCell::new(vec![])
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 23:26:18 +00:00
|
|
|
fn alloc(&self) -> Ptr {
|
2016-05-29 19:01:06 +00:00
|
|
|
self.cur_ops.set(self.cur_ops.get() + 1);
|
|
|
|
|
|
|
|
if self.cur_ops.get() == self.failing_op {
|
|
|
|
panic!(InjectedFailure);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:26:18 +00:00
|
|
|
let mut data = self.data.borrow_mut();
|
|
|
|
let addr = data.len();
|
|
|
|
data.push(true);
|
|
|
|
Ptr(addr, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Ptr<'a>(usize, &'a Allocator);
|
|
|
|
impl<'a> Drop for Ptr<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self.1.data.borrow_mut()[self.0] {
|
|
|
|
false => {
|
|
|
|
panic!("double free at index {:?}", self.0)
|
|
|
|
}
|
|
|
|
ref mut d => *d = false
|
|
|
|
}
|
2016-05-29 19:01:06 +00:00
|
|
|
|
|
|
|
self.1.cur_ops.set(self.1.cur_ops.get()+1);
|
|
|
|
|
|
|
|
if self.1.cur_ops.get() == self.1.failing_op {
|
|
|
|
panic!(InjectedFailure);
|
|
|
|
}
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dynamic_init(a: &Allocator, c: bool) {
|
|
|
|
let _x;
|
|
|
|
if c {
|
|
|
|
_x = Some(a.alloc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:01:06 +00:00
|
|
|
fn dynamic_drop(a: &Allocator, c: bool) {
|
2016-05-16 23:26:18 +00:00
|
|
|
let x = a.alloc();
|
|
|
|
if c {
|
|
|
|
Some(x)
|
|
|
|
} else {
|
|
|
|
None
|
2016-05-29 19:01:06 +00:00
|
|
|
};
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 12:22:59 +00:00
|
|
|
struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
|
|
|
|
fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
|
|
|
|
for i in 0..2 {
|
|
|
|
let x;
|
|
|
|
let y;
|
|
|
|
if (c0 && i == 0) || (c1 && i == 1) {
|
|
|
|
x = (a.alloc(), a.alloc(), a.alloc());
|
|
|
|
y = TwoPtrs(a.alloc(), a.alloc());
|
|
|
|
if c {
|
|
|
|
drop(x.1);
|
|
|
|
drop(y.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 15:34:07 +00:00
|
|
|
fn field_assignment(a: &Allocator, c0: bool) {
|
|
|
|
let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
|
|
|
|
|
|
|
|
x.1 = a.alloc();
|
|
|
|
x.1 = a.alloc();
|
|
|
|
|
|
|
|
let f = (x.0).0;
|
|
|
|
if c0 {
|
|
|
|
(x.0).0 = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:26:18 +00:00
|
|
|
fn assignment2(a: &Allocator, c0: bool, c1: bool) {
|
|
|
|
let mut _v = a.alloc();
|
|
|
|
let mut _w = a.alloc();
|
|
|
|
if c0 {
|
|
|
|
drop(_v);
|
|
|
|
}
|
|
|
|
_v = _w;
|
|
|
|
if c1 {
|
|
|
|
_w = a.alloc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assignment1(a: &Allocator, c0: bool) {
|
|
|
|
let mut _v = a.alloc();
|
|
|
|
let mut _w = a.alloc();
|
|
|
|
if c0 {
|
|
|
|
drop(_v);
|
|
|
|
}
|
|
|
|
_v = _w;
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:12:53 +00:00
|
|
|
#[allow(unions_with_drop_fields)]
|
|
|
|
union Boxy<T> {
|
|
|
|
a: T,
|
|
|
|
b: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn union1(a: &Allocator) {
|
|
|
|
unsafe {
|
|
|
|
let mut u = Boxy { a: a.alloc() };
|
|
|
|
u.b = a.alloc();
|
|
|
|
drop(u.a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 22:02:52 +00:00
|
|
|
fn array_simple(a: &Allocator) {
|
|
|
|
let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_simple(a: &Allocator) {
|
|
|
|
let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:20:22 +00:00
|
|
|
fn generator(a: &Allocator, run_count: usize) {
|
|
|
|
assert!(run_count < 4);
|
|
|
|
|
|
|
|
let mut gen = || {
|
|
|
|
(a.alloc(),
|
|
|
|
yield a.alloc(),
|
|
|
|
a.alloc(),
|
|
|
|
yield a.alloc()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
for _ in 0..run_count {
|
2018-03-19 23:48:41 +00:00
|
|
|
unsafe { gen.resume(); }
|
2017-10-17 12:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 18:37:18 +00:00
|
|
|
fn mixed_drop_and_nondrop(a: &Allocator) {
|
|
|
|
// check that destructor panics handle drop
|
|
|
|
// and non-drop blocks in the same scope correctly.
|
|
|
|
//
|
|
|
|
// Surprisingly enough, this used to not work.
|
|
|
|
let (x, y, z);
|
|
|
|
x = a.alloc();
|
|
|
|
y = 5;
|
|
|
|
z = a.alloc();
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:23:27 +00:00
|
|
|
#[allow(unreachable_code)]
|
|
|
|
fn vec_unreachable(a: &Allocator) {
|
|
|
|
let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
|
|
|
|
}
|
|
|
|
|
2017-11-28 12:48:23 +00:00
|
|
|
fn slice_pattern_first(a: &Allocator) {
|
|
|
|
let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_pattern_middle(a: &Allocator) {
|
|
|
|
let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_pattern_two(a: &Allocator) {
|
|
|
|
let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_pattern_last(a: &Allocator) {
|
|
|
|
let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_pattern_one_of(a: &Allocator, i: usize) {
|
|
|
|
let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
let _x = match i {
|
|
|
|
0 => { let [a, ..] = array; a }
|
|
|
|
1 => { let [_, a, ..] = array; a }
|
|
|
|
2 => { let [_, _, a, _] = array; a }
|
|
|
|
3 => { let [_, _, _, a] = array; a }
|
|
|
|
_ => panic!("unmatched"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-31 20:34:13 +00:00
|
|
|
fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
|
|
|
|
let a = [a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
if arg {
|
|
|
|
let[.., _x, _] = a;
|
|
|
|
} else {
|
|
|
|
let[_, _y..] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
|
|
|
|
let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
if arg2 {
|
|
|
|
drop(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg {
|
|
|
|
let[.., _x, _] = a;
|
|
|
|
} else {
|
|
|
|
let[_, _y..] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_pattern_reassign(a: &Allocator) {
|
|
|
|
let mut ar = [a.alloc(), a.alloc()];
|
|
|
|
let[_, _x] = ar;
|
|
|
|
ar = [a.alloc(), a.alloc()];
|
|
|
|
let[.., _y] = ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subslice_pattern_reassign(a: &Allocator) {
|
|
|
|
let mut ar = [a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
let[_, _, _x] = ar;
|
|
|
|
ar = [a.alloc(), a.alloc(), a.alloc()];
|
|
|
|
let[_, _y..] = ar;
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:01:06 +00:00
|
|
|
fn run_test<F>(mut f: F)
|
|
|
|
where F: FnMut(&Allocator)
|
|
|
|
{
|
|
|
|
let first_alloc = Allocator::new(usize::MAX);
|
|
|
|
f(&first_alloc);
|
|
|
|
|
|
|
|
for failing_op in 1..first_alloc.cur_ops.get()+1 {
|
|
|
|
let alloc = Allocator::new(failing_op);
|
|
|
|
let alloc = &alloc;
|
|
|
|
let f = panic::AssertUnwindSafe(&mut f);
|
|
|
|
let result = panic::catch_unwind(move || {
|
|
|
|
f.0(alloc);
|
|
|
|
});
|
|
|
|
match result {
|
|
|
|
Ok(..) => panic!("test executed {} ops but now {}",
|
|
|
|
first_alloc.cur_ops.get(), alloc.cur_ops.get()),
|
|
|
|
Err(e) => {
|
|
|
|
if e.downcast_ref::<InjectedFailure>().is_none() {
|
|
|
|
panic::resume_unwind(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 23:26:18 +00:00
|
|
|
|
2016-09-08 17:12:53 +00:00
|
|
|
fn run_test_nopanic<F>(mut f: F)
|
|
|
|
where F: FnMut(&Allocator)
|
|
|
|
{
|
|
|
|
let first_alloc = Allocator::new(usize::MAX);
|
|
|
|
f(&first_alloc);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:26:18 +00:00
|
|
|
fn main() {
|
2016-05-29 19:01:06 +00:00
|
|
|
run_test(|a| dynamic_init(a, false));
|
|
|
|
run_test(|a| dynamic_init(a, true));
|
|
|
|
run_test(|a| dynamic_drop(a, false));
|
|
|
|
run_test(|a| dynamic_drop(a, true));
|
|
|
|
|
|
|
|
run_test(|a| assignment2(a, false, false));
|
|
|
|
run_test(|a| assignment2(a, false, true));
|
|
|
|
run_test(|a| assignment2(a, true, false));
|
|
|
|
run_test(|a| assignment2(a, true, true));
|
|
|
|
|
|
|
|
run_test(|a| assignment1(a, false));
|
|
|
|
run_test(|a| assignment1(a, true));
|
2016-09-08 17:12:53 +00:00
|
|
|
|
2017-05-10 22:02:52 +00:00
|
|
|
run_test(|a| array_simple(a));
|
|
|
|
run_test(|a| vec_simple(a));
|
2017-08-09 19:23:27 +00:00
|
|
|
run_test(|a| vec_unreachable(a));
|
2017-05-10 22:02:52 +00:00
|
|
|
|
2017-05-15 12:22:59 +00:00
|
|
|
run_test(|a| struct_dynamic_drop(a, false, false, false));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, false, false, true));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, false, true, false));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, false, true, true));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, true, false, false));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, true, false, true));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, true, true, false));
|
|
|
|
run_test(|a| struct_dynamic_drop(a, true, true, true));
|
|
|
|
|
2017-06-27 15:34:07 +00:00
|
|
|
run_test(|a| field_assignment(a, false));
|
|
|
|
run_test(|a| field_assignment(a, true));
|
|
|
|
|
2017-10-17 18:37:18 +00:00
|
|
|
run_test(|a| generator(a, 0));
|
|
|
|
run_test(|a| generator(a, 1));
|
|
|
|
run_test(|a| generator(a, 2));
|
|
|
|
run_test(|a| generator(a, 3));
|
|
|
|
|
|
|
|
run_test(|a| mixed_drop_and_nondrop(a));
|
2017-10-17 12:20:22 +00:00
|
|
|
|
2017-11-28 12:48:23 +00:00
|
|
|
run_test(|a| slice_pattern_first(a));
|
|
|
|
run_test(|a| slice_pattern_middle(a));
|
|
|
|
run_test(|a| slice_pattern_two(a));
|
|
|
|
run_test(|a| slice_pattern_last(a));
|
|
|
|
run_test(|a| slice_pattern_one_of(a, 0));
|
|
|
|
run_test(|a| slice_pattern_one_of(a, 1));
|
|
|
|
run_test(|a| slice_pattern_one_of(a, 2));
|
|
|
|
run_test(|a| slice_pattern_one_of(a, 3));
|
|
|
|
|
2018-01-31 20:34:13 +00:00
|
|
|
run_test(|a| subslice_pattern_from_end(a, true));
|
|
|
|
run_test(|a| subslice_pattern_from_end(a, false));
|
|
|
|
run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
|
|
|
|
run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
|
|
|
|
run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
|
|
|
|
run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
|
|
|
|
run_test(|a| slice_pattern_reassign(a));
|
|
|
|
run_test(|a| subslice_pattern_reassign(a));
|
|
|
|
|
2016-09-08 17:12:53 +00:00
|
|
|
run_test_nopanic(|a| union1(a));
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|