]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/dropck-eyepatch-reorder.rs
Auto merge of #102684 - JhonnyBillM:delete-target-data-layout-errors-wrapper, r=davidtwco
[rust.git] / src / test / ui / drop / dropck-eyepatch-reorder.rs
1 // run-pass
2 #![feature(dropck_eyepatch)]
3
4 // The point of this test is to test uses of `#[may_dangle]` attribute
5 // where the formal declaration order (in the impl generics) does not
6 // match the actual usage order (in the type instantiation).
7 //
8 // See also dropck-eyepatch.rs for more information about the general
9 // structure of the test.
10
11 trait Foo { fn foo(&self, _: &str); }
12
13 struct Dt<A: Foo>(&'static str, A);
14 struct Dr<'a, B:'a+Foo>(&'static str, &'a B);
15 struct Pt<A: Foo, B: Foo>(&'static str, #[allow(unused_tuple_struct_fields)] A, B);
16 struct Pr<'a, 'b, B:'a+'b+Foo>(&'static str, #[allow(unused_tuple_struct_fields)] &'a B, &'b B);
17 struct St<A: Foo>(&'static str, #[allow(unused_tuple_struct_fields)] A);
18 struct Sr<'a, B:'a+Foo>(&'static str, #[allow(unused_tuple_struct_fields)] &'a B);
19
20 impl<A: Foo> Drop for Dt<A> {
21     fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
22 }
23 impl<'a, B: Foo> Drop for Dr<'a, B> {
24     fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
25 }
26 unsafe impl<B: Foo, #[may_dangle] A: Foo> Drop for Pt<A, B> {
27     // (unsafe to access self.1  due to #[may_dangle] on A)
28     fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
29 }
30 unsafe impl<'b, #[may_dangle] 'a, B: Foo> Drop for Pr<'a, 'b, B> {
31     // (unsafe to access self.1 due to #[may_dangle] on 'a)
32     fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
33 }
34
35 fn main() {
36     use std::cell::RefCell;
37
38     impl Foo for RefCell<String> {
39         fn foo(&self, s: &str) {
40             let s2 = format!("{}|{}", *self.borrow(), s);
41             *self.borrow_mut() = s2;
42         }
43     }
44
45     impl<'a, T:Foo> Foo for &'a T {
46         fn foo(&self, s: &str) {
47             (*self).foo(s);
48         }
49     }
50
51     struct CheckOnDrop(RefCell<String>, &'static str);
52     impl Drop for CheckOnDrop {
53         fn drop(&mut self) { assert_eq!(*self.0.borrow(), self.1); }
54     }
55
56     let c_long;
57     let (c, dt, dr, pt, pr, st, sr)
58         : (CheckOnDrop, Dt<_>, Dr<_>, Pt<_, _>, Pr<_>, St<_>, Sr<_>);
59     c_long = CheckOnDrop(RefCell::new("c_long".to_string()),
60                          "c_long|pr|pt|dr|dt");
61     c = CheckOnDrop(RefCell::new("c".to_string()),
62                     "c");
63
64     // No error: sufficiently long-lived state can be referenced in dtors
65     dt = Dt("dt", &c_long.0);
66     dr = Dr("dr", &c_long.0);
67
68     // No error: Drop impl asserts .1 (A and &'a _) are not accessed
69     pt = Pt("pt", &c.0, &c_long.0);
70     pr = Pr("pr", &c.0, &c_long.0);
71
72     // No error: St and Sr have no destructor.
73     st = St("st", &c.0);
74     sr = Sr("sr", &c.0);
75
76     println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
77     assert_eq!(*c_long.0.borrow(), "c_long");
78     assert_eq!(*c.0.borrow(), "c");
79 }