Skip to content

Commit 20e528d

Browse files
pnkfelixalexcrichton
authored andcommitted
Regression tests for Issue 25199 (dropck and Box<Trait + 'a>).
1 parent f7772f9 commit 20e528d

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Reject mixing cyclic structure and Drop when using trait
12+
// objects to hide the the cross-references.
13+
//
14+
// (Compare against compile-fail/dropck_vec_cycle_checked.rs)
15+
16+
use std::cell::Cell;
17+
use id::Id;
18+
19+
mod s {
20+
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
21+
22+
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
23+
24+
pub fn next_count() -> usize {
25+
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
26+
}
27+
}
28+
29+
mod id {
30+
use s;
31+
#[derive(Debug)]
32+
pub struct Id {
33+
orig_count: usize,
34+
count: usize,
35+
}
36+
37+
impl Id {
38+
pub fn new() -> Id {
39+
let c = s::next_count();
40+
println!("building Id {}", c);
41+
Id { orig_count: c, count: c }
42+
}
43+
pub fn count(&self) -> usize {
44+
println!("Id::count on {} returns {}", self.orig_count, self.count);
45+
self.count
46+
}
47+
}
48+
49+
impl Drop for Id {
50+
fn drop(&mut self) {
51+
println!("dropping Id {}", self.count);
52+
self.count = 0;
53+
}
54+
}
55+
}
56+
57+
trait HasId {
58+
fn count(&self) -> usize;
59+
}
60+
61+
#[derive(Debug)]
62+
struct CheckId<T:HasId> {
63+
v: T
64+
}
65+
66+
#[allow(non_snake_case)]
67+
fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
68+
69+
impl<T:HasId> Drop for CheckId<T> {
70+
fn drop(&mut self) {
71+
assert!(self.v.count() > 0);
72+
}
73+
}
74+
75+
trait Obj<'a> : HasId {
76+
fn set0(&self, b: &'a Box<Obj<'a>>);
77+
fn set1(&self, b: &'a Box<Obj<'a>>);
78+
}
79+
80+
struct O<'a> {
81+
id: Id,
82+
obj0: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
83+
obj1: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
84+
}
85+
86+
impl<'a> HasId for O<'a> {
87+
fn count(&self) -> usize { self.id.count() }
88+
}
89+
90+
impl<'a> O<'a> {
91+
fn new() -> Box<O<'a>> {
92+
Box::new(O {
93+
id: Id::new(),
94+
obj0: CheckId(Cell::new(None)),
95+
obj1: CheckId(Cell::new(None)),
96+
})
97+
}
98+
}
99+
100+
impl<'a> HasId for Cell<Option<&'a Box<Obj<'a>>>> {
101+
fn count(&self) -> usize {
102+
match self.get() {
103+
None => 1,
104+
Some(c) => c.count(),
105+
}
106+
}
107+
}
108+
109+
impl<'a> Obj<'a> for O<'a> {
110+
fn set0(&self, b: &'a Box<Obj<'a>>) {
111+
self.obj0.v.set(Some(b))
112+
}
113+
fn set1(&self, b: &'a Box<Obj<'a>>) {
114+
self.obj1.v.set(Some(b))
115+
}
116+
}
117+
118+
119+
fn f() {
120+
let (o1, o2, o3): (Box<Obj>, Box<Obj>, Box<Obj>) = (O::new(), O::new(), O::new());
121+
o1.set0(&o2); //~ ERROR `o2` does not live long enough
122+
o1.set1(&o3); //~ ERROR `o3` does not live long enough
123+
o2.set0(&o2); //~ ERROR `o2` does not live long enough
124+
o2.set1(&o3); //~ ERROR `o3` does not live long enough
125+
o3.set0(&o1); //~ ERROR `o1` does not live long enough
126+
o3.set1(&o2); //~ ERROR `o2` does not live long enough
127+
}
128+
129+
fn main() {
130+
f();
131+
}

src/test/compile-fail/issue-25199.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for Issue 25199: Check that one cannot hide a
12+
// destructor's access to borrowed data behind a boxed trait object.
13+
//
14+
// Prior to fixing Issue 25199, this example was able to be compiled
15+
// with rustc, and thus when you ran it, you would see the `Drop` impl
16+
// for `Test` accessing state that had already been dropping (which is
17+
// marked explicitly here with checking code within the `Drop` impl
18+
// for `VecHolder`, but in the general case could just do unsound
19+
// things like accessing memory that has been freed).
20+
//
21+
// Note that I would have liked to encode my go-to example of cyclic
22+
// structure that accesses its neighbors in drop (and thus is
23+
// fundamentally unsound) via this trick, but the closest I was able
24+
// to come was dropck_trait_cycle_checked.rs, which is not quite as
25+
// "good" as this regression test because the encoding of that example
26+
// was forced to attach a lifetime to the trait definition itself
27+
// (`trait Obj<'a>`) while *this* example is solely
28+
29+
use std::cell::RefCell;
30+
31+
trait Obj { }
32+
33+
struct VecHolder {
34+
v: Vec<(bool, &'static str)>,
35+
}
36+
37+
impl Drop for VecHolder {
38+
fn drop(&mut self) {
39+
println!("Dropping Vec");
40+
self.v[30].0 = false;
41+
self.v[30].1 = "invalid access: VecHolder dropped already";
42+
}
43+
}
44+
45+
struct Container<'a> {
46+
v: VecHolder,
47+
d: RefCell<Vec<Box<Obj+'a>>>,
48+
}
49+
50+
impl<'a> Container<'a> {
51+
fn new() -> Container<'a> {
52+
Container {
53+
d: RefCell::new(Vec::new()),
54+
v: VecHolder {
55+
v: vec![(true, "valid"); 100]
56+
}
57+
}
58+
}
59+
60+
fn store<T: Obj+'a>(&'a self, val: T) {
61+
self.d.borrow_mut().push(Box::new(val));
62+
}
63+
}
64+
65+
struct Test<'a> {
66+
test: &'a Container<'a>,
67+
}
68+
69+
impl<'a> Obj for Test<'a> { }
70+
impl<'a> Drop for Test<'a> {
71+
fn drop(&mut self) {
72+
for e in &self.test.v.v {
73+
assert!(e.0, e.1);
74+
}
75+
}
76+
}
77+
78+
fn main() {
79+
let container = Container::new();
80+
let test = Test{test: &container}; //~ ERROR `container` does not live long enough
81+
println!("container.v[30]: {:?}", container.v.v[30]);
82+
container.store(test); //~ ERROR `container` does not live long enough
83+
}

0 commit comments

Comments
 (0)