]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/issue28498-reject-lifetime-param.rs
Rollup merge of #87922 - Manishearth:c-enum-target-spec, r=nagisa,eddyb
[rust.git] / src / test / ui / span / issue28498-reject-lifetime-param.rs
1 // Demonstrate that having a lifetime param causes dropck to reject code
2 // that might indirectly access previously dropped value.
3 //
4 // Compare with run-pass/issue28498-ugeh-with-lifetime-param.rs
5
6 #[derive(Debug)]
7 struct ScribbleOnDrop(String);
8
9 impl Drop for ScribbleOnDrop {
10     fn drop(&mut self) {
11         self.0 = format!("DROPPED");
12     }
13 }
14
15 struct Foo<'a>(u32, &'a ScribbleOnDrop);
16
17 impl<'a> Drop for Foo<'a> {
18     fn drop(&mut self) {
19         // Use of `may_dangle` is unsound, because destructor accesses borrowed data
20         // in `self.1` and we must force that to strictly outlive `self`.
21         println!("Dropping Foo({}, {:?})", self.0, self.1);
22     }
23 }
24
25 fn main() {
26     let (last_dropped, foo0);
27     let (foo1, first_dropped);
28
29     last_dropped = ScribbleOnDrop(format!("last"));
30     first_dropped = ScribbleOnDrop(format!("first"));
31     foo0 = Foo(0, &last_dropped); // OK
32     foo1 = Foo(1, &first_dropped);
33     //~^ ERROR `first_dropped` does not live long enough
34
35     println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
36 }