|
| 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 | +// Demonstrate that having a lifetime param causes dropck to reject code |
| 12 | +// that might indirectly access previously dropped value. |
| 13 | +// |
| 14 | +// Compare with run-pass/issue28498-ugeh-with-lifetime-param.rs |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +struct ScribbleOnDrop(String); |
| 18 | + |
| 19 | +impl Drop for ScribbleOnDrop { |
| 20 | + fn drop(&mut self) { |
| 21 | + self.0 = format!("DROPPED"); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +struct Foo<'a>(u32, &'a ScribbleOnDrop); |
| 26 | + |
| 27 | +impl<'a> Drop for Foo<'a> { |
| 28 | + fn drop(&mut self) { |
| 29 | + // Use of `unsafe_destructor_blind_to_params` is unsound, |
| 30 | + // because destructor accesses borrowed data in `self.1` |
| 31 | + // and we must force that to strictly outlive `self`. |
| 32 | + println!("Dropping Foo({}, {:?})", self.0, self.1); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn main() { |
| 37 | + let (last_dropped, foo0); |
| 38 | + let (foo1, first_dropped); |
| 39 | + |
| 40 | + last_dropped = ScribbleOnDrop(format!("last")); |
| 41 | + first_dropped = ScribbleOnDrop(format!("first")); |
| 42 | + foo0 = Foo(0, &last_dropped); |
| 43 | + //~^ ERROR `last_dropped` does not live long enough |
| 44 | + foo1 = Foo(1, &first_dropped); |
| 45 | + //~^ ERROR `first_dropped` does not live long enough |
| 46 | + |
| 47 | + println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1); |
| 48 | +} |
0 commit comments