]> git.lizzy.rs Git - rust.git/blob - tests/ui/span/issue28498-reject-passed-to-fn.rs
Rollup merge of #106441 - mllken:abstract-socket-noref, r=joshtriplett
[rust.git] / tests / ui / span / issue28498-reject-passed-to-fn.rs
1 // Demonstrate that a type param in negative position causes dropck to reject code
2 // that might indirectly access previously dropped value.
3 //
4 // Compare with run-pass/issue28498-ugeh-with-passed-to-fn.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<T>(u32, T, Box<for <'r> fn(&'r T) -> String>);
16
17 impl<T> Drop for Foo<T> {
18     fn drop(&mut self) {
19         // Use of `may_dangle` is unsound, because we pass `T` to the callback in `self.2`
20         // below, and thus potentially read from borrowed data.
21         println!("Dropping Foo({}, {})", self.0, (self.2)(&self.1));
22     }
23 }
24
25 fn callback(s: & &ScribbleOnDrop) -> String { format!("{:?}", s) }
26
27 fn main() {
28     let (last_dropped, foo0);
29     let (foo1, first_dropped);
30
31     last_dropped = ScribbleOnDrop(format!("last"));
32     first_dropped = ScribbleOnDrop(format!("first"));
33     foo0 = Foo(0, &last_dropped, Box::new(callback)); // OK
34     foo1 = Foo(1, &first_dropped, Box::new(callback));
35     //~^ ERROR `first_dropped` does not live long enough
36
37     println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
38 }