]> git.proxmox.com Git - rustc.git/blob - src/test/ui/span/issue28498-reject-passed-to-fn.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / test / ui / span / issue28498-reject-passed-to-fn.rs
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 a type param in negative position causes dropck to reject code
12 // that might indirectly access previously dropped value.
13 //
14 // Compare with run-pass/issue28498-ugeh-with-passed-to-fn.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<T>(u32, T, Box<for <'r> fn(&'r T) -> String>);
26
27 impl<T> Drop for Foo<T> {
28 fn drop(&mut self) {
29 // Use of `unsafe_destructor_blind_to_params` is unsound,
30 // because we pass `T` to the callback in `self.2`
31 // below, and thus potentially read from borrowed data.
32 println!("Dropping Foo({}, {})", self.0, (self.2)(&self.1));
33 }
34 }
35
36 fn callback(s: & &ScribbleOnDrop) -> String { format!("{:?}", s) }
37
38 fn main() {
39 let (last_dropped, foo0);
40 let (foo1, first_dropped);
41
42 last_dropped = ScribbleOnDrop(format!("last"));
43 first_dropped = ScribbleOnDrop(format!("first"));
44 foo0 = Foo(0, &last_dropped, Box::new(callback));
45 //~^ ERROR `last_dropped` does not live long enough
46 foo1 = Foo(1, &first_dropped, Box::new(callback));
47 //~^ ERROR `first_dropped` does not live long enough
48
49 println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
50 }