Skip to content

Commit 4459a43

Browse files
committed
run-pass tests.
includes regression tests discovered during bootstrapping and tests of cyclic structure that currently pass and are expected to continue passing under the dropck rule. (Note that all the uses of `unsafe_destructor` are just placating the simple analysis used for that feature, which will eventually go away once we have put the dropck through its paces.)
1 parent d6c158d commit 4459a43

8 files changed

+371
-0
lines changed

src/test/run-pass/arr_cycle.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
use std::cell::Cell;
12+
13+
#[derive(Show)]
14+
struct B<'a> {
15+
a: [Cell<Option<&'a B<'a>>>; 2]
16+
}
17+
18+
impl<'a> B<'a> {
19+
fn new() -> B<'a> {
20+
B { a: [Cell::new(None), Cell::new(None)] }
21+
}
22+
}
23+
24+
fn f() {
25+
let (b1, b2, b3);
26+
b1 = B::new();
27+
b2 = B::new();
28+
b3 = B::new();
29+
b1.a[0].set(Some(&b2));
30+
b1.a[1].set(Some(&b3));
31+
b2.a[0].set(Some(&b2));
32+
b2.a[1].set(Some(&b3));
33+
b3.a[0].set(Some(&b1));
34+
b3.a[1].set(Some(&b2));
35+
}
36+
37+
fn main() {
38+
f();
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// Check that a arena (TypedArena) can carry elements whose drop
12+
// methods might access borrowed data, as long as the borrowed data
13+
// has lifetime that strictly outlives the arena itself.
14+
//
15+
// Compare against compile-fail/dropck_tarena_unsound_drop.rs, which
16+
// shows a similar setup, but restricts `f` so that the struct `C<'a>`
17+
// is force-fed a lifetime equal to that of the borrowed arena.
18+
19+
#![allow(unstable)]
20+
#![feature(unsafe_destructor)]
21+
22+
extern crate arena;
23+
24+
use arena::TypedArena;
25+
26+
trait HasId { fn count(&self) -> usize; }
27+
28+
struct CheckId<T:HasId> { v: T }
29+
30+
// In the code below, the impl of HasId for `&'a usize` does not
31+
// actually access the borrowed data, but the point is that the
32+
// interface to CheckId does not (and cannot) know that, and therefore
33+
// when encountering the a value V of type CheckId<S>, we must
34+
// conservatively force the type S to strictly outlive V.
35+
#[unsafe_destructor]
36+
impl<T:HasId> Drop for CheckId<T> {
37+
fn drop(&mut self) {
38+
assert!(self.v.count() > 0);
39+
}
40+
}
41+
42+
struct C<'a> { _v: CheckId<&'a usize>, }
43+
44+
impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
45+
46+
fn f<'a, 'b>(_arena: &'a TypedArena<C<'b>>) {}
47+
48+
fn main() {
49+
let arena: TypedArena<C> = TypedArena::new();
50+
f(&arena);
51+
}

src/test/run-pass/nondrop-cycle.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
use std::cell::Cell;
12+
13+
struct C<'a> {
14+
p: Cell<Option<&'a C<'a>>>,
15+
}
16+
17+
impl<'a> C<'a> {
18+
fn new() -> C<'a> { C { p: Cell::new(None) } }
19+
}
20+
21+
fn f1() {
22+
let (c1, c2) = (C::new(), C::new());
23+
c1.p.set(Some(&c2));
24+
c2.p.set(Some(&c1));
25+
}
26+
27+
fn f2() {
28+
let (c1, c2);
29+
c1 = C::new();
30+
c2 = C::new();
31+
c1.p.set(Some(&c2));
32+
c2.p.set(Some(&c1));
33+
}
34+
35+
fn main() {
36+
f1();
37+
f2();
38+
}

src/test/run-pass/regions-refcell.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2012-2014 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+
// This is a regression test for something that only came up while
12+
// attempting to bootstrap librustc with new destructor lifetime
13+
// semantics.
14+
15+
use std::collections::HashMap;
16+
use std::cell::RefCell;
17+
18+
// This version does not yet work (associated type issues)...
19+
#[cfg(cannot_use_this_yet)]
20+
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
21+
let one = [1u];
22+
assert_eq!(map.borrow().get("one"), Some(&one[]));
23+
}
24+
25+
#[cfg(cannot_use_this_yet_either)]
26+
// ... and this version does not work (the lifetime of `one` is
27+
// supposed to match the lifetime `'a`) ...
28+
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
29+
let one = [1u];
30+
assert_eq!(map.borrow().get("one"), Some(&one.as_slice()));
31+
}
32+
33+
#[cfg(all(not(cannot_use_this_yet),not(cannot_use_this_yet_either)))]
34+
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
35+
// ...so instead we walk through the trivial slice and make sure
36+
// it contains the element we expect.
37+
38+
for (i, &x) in map.borrow().get("one").unwrap().iter().enumerate() {
39+
assert_eq!((i, x), (0, 1));
40+
}
41+
}
42+
43+
fn main() {
44+
let zer = [0u8];
45+
let one = [1u8];
46+
let two = [2u8];
47+
let mut map = HashMap::new();
48+
map.insert("zero", &zer[]);
49+
map.insert("one", &one[]);
50+
map.insert("two", &two[]);
51+
let map = RefCell::new(map);
52+
foo(map);
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2012-2014 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+
// This is a regression test for something that only came up while
12+
// attempting to bootstrap libsyntax; it is adapted from
13+
// `syntax::ext::tt::generic_extension`.
14+
15+
pub struct E<'a> {
16+
pub f: &'a u8,
17+
}
18+
impl<'b> E<'b> {
19+
pub fn m(&self) -> &'b u8 { self.f }
20+
}
21+
22+
pub struct P<'c> {
23+
pub g: &'c u8,
24+
}
25+
pub trait M {
26+
fn n(&self) -> u8;
27+
}
28+
impl<'d> M for P<'d> {
29+
fn n(&self) -> u8 { *self.g }
30+
}
31+
32+
fn extension<'e>(x: &'e E<'e>) -> Box<M+'e> {
33+
loop {
34+
let p = P { g: x.m() };
35+
return Box::new(p) as Box<M+'e>;
36+
}
37+
}
38+
39+
fn main() {
40+
let w = E { f: &10u8 };
41+
let o = extension(&w);
42+
assert_eq!(o.n(), 10u8);
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 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+
// Uncovered during work on new scoping rules for safe destructors
12+
// as an important use case to support properly.
13+
14+
pub struct E<'a> {
15+
pub f: &'a u8,
16+
}
17+
impl<'b> E<'b> {
18+
pub fn m(&self) -> &'b u8 { self.f }
19+
}
20+
21+
pub struct P<'c> {
22+
pub g: &'c u8,
23+
}
24+
pub trait M {
25+
fn n(&self) -> u8;
26+
}
27+
impl<'d> M for P<'d> {
28+
fn n(&self) -> u8 { *self.g }
29+
}
30+
31+
fn extension<'e>(x: &'e E<'e>) -> Box<M+'e> {
32+
loop {
33+
let p = P { g: x.m() };
34+
return Box::new(p) as Box<M+'e>;
35+
}
36+
}
37+
38+
fn main() {
39+
let w = E { f: &10u8 };
40+
let o = extension(&w);
41+
assert_eq!(o.n(), 10u8);
42+
}

src/test/run-pass/vec_cycle.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
use std::cell::Cell;
12+
13+
#[derive(Show)]
14+
struct C<'a> {
15+
v: Vec<Cell<Option<&'a C<'a>>>>,
16+
}
17+
18+
impl<'a> C<'a> {
19+
fn new() -> C<'a> {
20+
C { v: Vec::new() }
21+
}
22+
}
23+
24+
fn f() {
25+
let (mut c1, mut c2, mut c3);
26+
c1 = C::new();
27+
c2 = C::new();
28+
c3 = C::new();
29+
30+
c1.v.push(Cell::new(None));
31+
c1.v.push(Cell::new(None));
32+
c2.v.push(Cell::new(None));
33+
c2.v.push(Cell::new(None));
34+
c3.v.push(Cell::new(None));
35+
c3.v.push(Cell::new(None));
36+
37+
c1.v[0].set(Some(&c2));
38+
c1.v[1].set(Some(&c3));
39+
c2.v[0].set(Some(&c2));
40+
c2.v[1].set(Some(&c3));
41+
c3.v[0].set(Some(&c1));
42+
c3.v[1].set(Some(&c2));
43+
}
44+
45+
fn main() {
46+
f();
47+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
use std::cell::Cell;
12+
13+
#[derive(Show)]
14+
struct Refs<'a> {
15+
v: Vec<Cell<Option<&'a C<'a>>>>,
16+
}
17+
18+
#[derive(Show)]
19+
struct C<'a> {
20+
refs: Refs<'a>,
21+
}
22+
23+
impl<'a> Refs<'a> {
24+
fn new() -> Refs<'a> {
25+
Refs { v: Vec::new() }
26+
}
27+
}
28+
29+
impl<'a> C<'a> {
30+
fn new() -> C<'a> {
31+
C { refs: Refs::new() }
32+
}
33+
}
34+
35+
fn f() {
36+
let (mut c1, mut c2, mut c3);
37+
c1 = C::new();
38+
c2 = C::new();
39+
c3 = C::new();
40+
41+
c1.refs.v.push(Cell::new(None));
42+
c1.refs.v.push(Cell::new(None));
43+
c2.refs.v.push(Cell::new(None));
44+
c2.refs.v.push(Cell::new(None));
45+
c3.refs.v.push(Cell::new(None));
46+
c3.refs.v.push(Cell::new(None));
47+
48+
c1.refs.v[0].set(Some(&c2));
49+
c1.refs.v[1].set(Some(&c3));
50+
c2.refs.v[0].set(Some(&c2));
51+
c2.refs.v[1].set(Some(&c3));
52+
c3.refs.v[0].set(Some(&c1));
53+
c3.refs.v[1].set(Some(&c2));
54+
}
55+
56+
fn main() {
57+
f();
58+
}

0 commit comments

Comments
 (0)