Skip to content

Commit 8b27223

Browse files
committed
test: Add tests for issue #12223, "drop allowed while active borrows
still in scope". This issue was fixed by PR #12828 and #5781. All that was left was to add tests. Closes #12223.
1 parent 14c0b3a commit 8b27223

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Diff for: src/test/compile-fail/drop-with-active-borrows-1.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 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+
fn main() {
12+
let a = "".to_string();
13+
let b: Vec<&str> = a.as_slice().lines().collect();
14+
drop(a); //~ ERROR cannot move out of `a` because it is borrowed
15+
for s in b.iter() {
16+
println!("{}", *s);
17+
}
18+
}
19+

Diff for: src/test/compile-fail/drop-with-active-borrows-2.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 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+
fn read_lines_borrowed() -> Vec<&str> {
12+
let raw_lines: Vec<String> = vec!("foo ".to_string(), " bar".to_string());
13+
raw_lines.iter().map(|l| l.as_slice().trim()).collect()
14+
//~^ ERROR `raw_lines` does not live long enough
15+
}
16+
17+
fn main() {
18+
println!("{}", read_lines_borrowed());
19+
}

Diff for: src/test/run-pass/drop-with-type-ascription-1.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 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+
fn main() {
12+
let foo = "hello".to_string();
13+
let foo: Vec<&str> = foo.as_slice().words().collect();
14+
let invalid_string = foo.get(0);
15+
assert_eq!(*invalid_string, "hello");
16+
}
17+

Diff for: src/test/run-pass/drop-with-type-ascription-2.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 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+
fn main() {
12+
let args = vec!("foobie", "asdf::asdf");
13+
let arr: Vec<&str> = args.get(1).as_slice().split_str("::").collect();
14+
assert_eq!(*arr.get(0), "asdf");
15+
assert_eq!(*arr.get(0), "asdf");
16+
}
17+

0 commit comments

Comments
 (0)