Auto merge of #31430 - nagisa:mir-dyndrop, r=nikomatsakis
Zeroing on-drop seems to work fine. Still thinking about the best way to approach zeroing on-move. (based on top of the other drop PR; only the last 2 commits are relevant)
This commit is contained in:
commit
339a409bfd
22 changed files with 405 additions and 221 deletions
42
src/test/run-fail/mir_dynamic_drops_1.rs
Normal file
42
src/test/run-fail/mir_dynamic_drops_1.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// 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.
|
||||
#![feature(rustc_attrs)]
|
||||
// error-pattern:drop 1
|
||||
// error-pattern:drop 2
|
||||
use std::io::{self, Write};
|
||||
|
||||
|
||||
/// Structure which will not allow to be dropped twice.
|
||||
struct Droppable<'a>(&'a mut bool, u32);
|
||||
impl<'a> Drop for Droppable<'a> {
|
||||
fn drop(&mut self) {
|
||||
if *self.0 {
|
||||
writeln!(io::stderr(), "{} dropped twice", self.1);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
writeln!(io::stderr(), "drop {}", self.1);
|
||||
*self.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
#[rustc_mir]
|
||||
fn mir(){
|
||||
let (mut xv, mut yv) = (false, false);
|
||||
let x = Droppable(&mut xv, 1);
|
||||
let y = Droppable(&mut yv, 2);
|
||||
let mut z = x;
|
||||
let k = y;
|
||||
z = k;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mir();
|
||||
panic!();
|
||||
}
|
||||
40
src/test/run-fail/mir_dynamic_drops_2.rs
Normal file
40
src/test/run-fail/mir_dynamic_drops_2.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// 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.
|
||||
#![feature(rustc_attrs)]
|
||||
// error-pattern:drop 1
|
||||
use std::io::{self, Write};
|
||||
|
||||
|
||||
/// Structure which will not allow to be dropped twice.
|
||||
struct Droppable<'a>(&'a mut bool, u32);
|
||||
impl<'a> Drop for Droppable<'a> {
|
||||
fn drop(&mut self) {
|
||||
if *self.0 {
|
||||
writeln!(io::stderr(), "{} dropped twice", self.1);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
writeln!(io::stderr(), "drop {}", self.1);
|
||||
*self.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
#[rustc_mir]
|
||||
fn mir<'a>(d: Droppable<'a>){
|
||||
loop {
|
||||
let x = d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut xv = false;
|
||||
mir(Droppable(&mut xv, 1));
|
||||
panic!();
|
||||
}
|
||||
45
src/test/run-fail/mir_dynamic_drops_3.rs
Normal file
45
src/test/run-fail/mir_dynamic_drops_3.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// 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.
|
||||
#![feature(rustc_attrs)]
|
||||
// error-pattern:unwind happens
|
||||
// error-pattern:drop 3
|
||||
// error-pattern:drop 2
|
||||
// error-pattern:drop 1
|
||||
use std::io::{self, Write};
|
||||
|
||||
|
||||
/// Structure which will not allow to be dropped twice.
|
||||
struct Droppable<'a>(&'a mut bool, u32);
|
||||
impl<'a> Drop for Droppable<'a> {
|
||||
fn drop(&mut self) {
|
||||
if *self.0 {
|
||||
writeln!(io::stderr(), "{} dropped twice", self.1);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
writeln!(io::stderr(), "drop {}", self.1);
|
||||
*self.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn may_panic<'a>() -> Droppable<'a> {
|
||||
panic!("unwind happens");
|
||||
}
|
||||
|
||||
#[rustc_mir]
|
||||
fn mir<'a>(d: Droppable<'a>){
|
||||
let (mut a, mut b) = (false, false);
|
||||
let y = Droppable(&mut a, 2);
|
||||
let x = [Droppable(&mut b, 1), y, d, may_panic()];
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut c = false;
|
||||
mir(Droppable(&mut c, 3));
|
||||
}
|
||||
39
src/test/run-pass/mir_fat_ptr_drop.rs
Normal file
39
src/test/run-pass/mir_fat_ptr_drop.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
// test that ordinary fat pointer operations work.
|
||||
|
||||
#![feature(braced_empty_structs)]
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
use std::sync::atomic;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
|
||||
static COUNTER: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
|
||||
|
||||
struct DropMe {
|
||||
}
|
||||
|
||||
impl Drop for DropMe {
|
||||
fn drop(&mut self) {
|
||||
COUNTER.fetch_add(1, SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
#[rustc_mir]
|
||||
fn fat_ptr_move_then_drop(a: Box<[DropMe]>) {
|
||||
let b = a;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a: Box<[DropMe]> = Box::new([DropMe { }]);
|
||||
fat_ptr_move_then_drop(a);
|
||||
assert_eq!(COUNTER.load(SeqCst), 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue