Skip to content

Commit e966e8c

Browse files
committed
Add initial attempt at ui/nll test for maybe-initialized drop
1 parent 0bb2b66 commit e966e8c

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2017 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+
// revisions: mir
11+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
12+
13+
struct Wrap<'p> { p: &'p mut i32 }
14+
15+
impl<'p> Drop for Wrap<'p> {
16+
fn drop(&mut self) {
17+
*self.p += 1;
18+
}
19+
}
20+
21+
fn foo() {
22+
let mut x = 0;
23+
let wrap = Wrap { p: &mut x };
24+
x += 1; //~ ERROR because of dtor
25+
}
26+
27+
fn bar() {
28+
let mut x = 0;
29+
let wrap = Wrap { p: &mut x };
30+
std::mem::drop(wrap);
31+
x += 1; // OK, drop is inert
32+
}
33+
34+
struct Foo<'p> { a: String, b: Wrap<'p> }
35+
36+
fn move_string(_a: String) { }
37+
38+
fn move_wrap<'p>(_b: Wrap<'p>) { }
39+
40+
fn baz_a() {
41+
let mut x = 0;
42+
let wrap = Wrap { p: &mut x };
43+
let s = String::from("str");
44+
let foo = Foo { a: s, b: wrap };
45+
move_string(foo.a);
46+
}
47+
48+
fn baz_a_b() {
49+
let mut x = 0;
50+
let wrap = Wrap { p: &mut x };
51+
let s = String::from("str");
52+
let foo = Foo { a: s, b: wrap };
53+
move_string(foo.a);
54+
move_wrap(foo.b);
55+
}
56+
57+
fn baz_b() {
58+
let mut x = 0;
59+
let wrap = Wrap { p: &mut x };
60+
let s = String::from("str");
61+
let foo = Foo { a: s, b: wrap };
62+
move_wrap(foo.b);
63+
}
64+
65+
fn main() { }
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
error[E0506]: cannot assign to `x` because it is borrowed (Ast)
2+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:24:5
3+
|
4+
23 | let wrap = Wrap { p: &mut x };
5+
| - borrow of `x` occurs here
6+
24 | x += 1; //~ ERROR because of dtor
7+
| ^^^^^^ assignment to borrowed `x` occurs here
8+
9+
error[E0506]: cannot assign to `x` because it is borrowed (Ast)
10+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:31:5
11+
|
12+
29 | let wrap = Wrap { p: &mut x };
13+
| - borrow of `x` occurs here
14+
30 | std::mem::drop(wrap);
15+
31 | x += 1; // OK, drop is inert
16+
| ^^^^^^ assignment to borrowed `x` occurs here
17+
18+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
19+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:24:5
20+
|
21+
23 | let wrap = Wrap { p: &mut x };
22+
| ------ borrow of `x` occurs here
23+
24 | x += 1; //~ ERROR because of dtor
24+
| ^^^^^^ assignment to borrowed `x` occurs here
25+
26+
error[E0503]: cannot use `x` because it was mutably borrowed (Mir)
27+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:24:5
28+
|
29+
23 | let wrap = Wrap { p: &mut x };
30+
| ------ borrow of `x` occurs here
31+
24 | x += 1; //~ ERROR because of dtor
32+
| ^^^^^^ use of borrowed `x`
33+
34+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
35+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:25:2
36+
|
37+
23 | let wrap = Wrap { p: &mut x };
38+
| ------ borrow of `x` occurs here
39+
24 | x += 1; //~ ERROR because of dtor
40+
25 | }
41+
| ^ assignment to borrowed `x` occurs here
42+
43+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
44+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:31:5
45+
|
46+
29 | let wrap = Wrap { p: &mut x };
47+
| ------ borrow of `x` occurs here
48+
30 | std::mem::drop(wrap);
49+
31 | x += 1; // OK, drop is inert
50+
| ^^^^^^ assignment to borrowed `x` occurs here
51+
52+
error[E0503]: cannot use `x` because it was mutably borrowed (Mir)
53+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:31:5
54+
|
55+
29 | let wrap = Wrap { p: &mut x };
56+
| ------ borrow of `x` occurs here
57+
30 | std::mem::drop(wrap);
58+
31 | x += 1; // OK, drop is inert
59+
| ^^^^^^ use of borrowed `x`
60+
61+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
62+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:32:2
63+
|
64+
29 | let wrap = Wrap { p: &mut x };
65+
| ------ borrow of `x` occurs here
66+
...
67+
32 | }
68+
| ^ assignment to borrowed `x` occurs here
69+
70+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
71+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:46:2
72+
|
73+
42 | let wrap = Wrap { p: &mut x };
74+
| ------ borrow of `x` occurs here
75+
...
76+
46 | }
77+
| ^ assignment to borrowed `x` occurs here
78+
79+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
80+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:55:2
81+
|
82+
50 | let wrap = Wrap { p: &mut x };
83+
| ------ borrow of `x` occurs here
84+
...
85+
55 | }
86+
| ^ assignment to borrowed `x` occurs here
87+
88+
error[E0506]: cannot assign to `x` because it is borrowed (Mir)
89+
--> /home/paulf/rust/src/test/ui/nll/maybe-initialized-drop.rs:63:2
90+
|
91+
59 | let wrap = Wrap { p: &mut x };
92+
| ------ borrow of `x` occurs here
93+
...
94+
63 | }
95+
| ^ assignment to borrowed `x` occurs here
96+
97+
error: aborting due to 11 previous errors

0 commit comments

Comments
 (0)