]> git.lizzy.rs Git - rust.git/blob - tests/ui/dropck/issue-28498-ugeh-with-passed-to-fn.rs
Modify existing bounds if they exist
[rust.git] / tests / ui / dropck / issue-28498-ugeh-with-passed-to-fn.rs
1 // run-pass
2
3 // Demonstrate the use of the unguarded escape hatch with a type param in negative position
4 // to assert that destructor will not access any dead data.
5 //
6 // Compare with ui/span/issue28498-reject-lifetime-param.rs
7
8 // Demonstrate that a type param in negative position causes dropck to reject code
9 // that might indirectly access previously dropped value.
10 //
11 // Compare with run-pass/issue28498-ugeh-with-passed-to-fn.rs
12
13 #![feature(dropck_eyepatch)]
14
15 #[derive(Debug)]
16 struct ScribbleOnDrop(String);
17
18 impl Drop for ScribbleOnDrop {
19     fn drop(&mut self) {
20         self.0 = format!("DROPPED");
21     }
22 }
23
24 struct Foo<T>(u32, T, #[allow(unused_tuple_struct_fields)] Box<for <'r> fn(&'r T) -> String>);
25
26 unsafe impl<#[may_dangle] T> Drop for Foo<T> {
27     fn drop(&mut self) {
28         // Use of `may_dangle` is sound, because destructor never passes a `self.1`
29         // to the callback (in `self.2`) despite having it available.
30         println!("Dropping Foo({}, _)", self.0);
31     }
32 }
33
34 fn callback(s: & &ScribbleOnDrop) -> String { format!("{:?}", s) }
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, Box::new(callback));
43     foo1 = Foo(1, &first_dropped, Box::new(callback));
44
45     println!("foo0.1: {:?} foo1.1: {:?}", foo0.1, foo1.1);
46 }