]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/dropck-eyepatch.rs
Rollup merge of #92959 - asquared31415:test-non-fn-help, r=estebank
[rust.git] / src / test / ui / drop / dropck-eyepatch.rs
1 // run-pass
2 #![feature(dropck_eyepatch)]
3
4 // The point of this test is to illustrate that the `#[may_dangle]`
5 // attribute specifically allows, in the context of a type
6 // implementing `Drop`, a generic parameter to be instantiated with a
7 // lifetime that does not strictly outlive the owning type itself.
8 //
9 // Here we test that a model use of `#[may_dangle]` will compile and run.
10 //
11 // The illustration is made concrete by comparison with two variations
12 // on the type with `#[may_dangle]`:
13 //
14 //   1. an analogous type that does not implement `Drop` (and thus
15 //      should exhibit maximal flexibility with respect to dropck), and
16 //
17 //   2. an analogous type that does not use `#[may_dangle]` (and thus
18 //      should exhibit the standard limitations imposed by dropck.
19 //
20 // The types in this file follow a pattern, {D,P,S}{t,r}, where:
21 //
22 // - D means "I implement Drop"
23 //
24 // - P means "I implement Drop but guarantee my (first) parameter is
25 //     pure, i.e., not accessed from the destructor"; no other parameters
26 //     are pure.
27 //
28 // - S means "I do not implement Drop"
29 //
30 // - t suffix is used when the first generic is a type
31 //
32 // - r suffix is used when the first generic is a lifetime.
33
34 trait Foo { fn foo(&self, _: &str); }
35
36 struct Dt<A: Foo>(&'static str, A);
37 struct Dr<'a, B:'a+Foo>(&'static str, &'a B);
38 struct Pt<A,B: Foo>(&'static str, A, B);
39 struct Pr<'a, 'b, B:'a+'b+Foo>(&'static str, &'a B, &'b B);
40 struct St<A: Foo>(&'static str, A);
41 struct Sr<'a, B:'a+Foo>(&'static str, &'a B);
42
43 impl<A: Foo> Drop for Dt<A> {
44     fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
45 }
46 impl<'a, B: Foo> Drop for Dr<'a, B> {
47     fn drop(&mut self) { println!("drop {}", self.0); self.1.foo(self.0); }
48 }
49 unsafe impl<#[may_dangle] A, B: Foo> Drop for Pt<A, B> {
50     // (unsafe to access self.1  due to #[may_dangle] on A)
51     fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
52 }
53 unsafe impl<#[may_dangle] 'a, 'b, B: Foo> Drop for Pr<'a, 'b, B> {
54     // (unsafe to access self.1 due to #[may_dangle] on 'a)
55     fn drop(&mut self) { println!("drop {}", self.0); self.2.foo(self.0); }
56 }
57
58 fn main() {
59     use std::cell::RefCell;
60
61     impl Foo for RefCell<String> {
62         fn foo(&self, s: &str) {
63             let s2 = format!("{}|{}", *self.borrow(), s);
64             *self.borrow_mut() = s2;
65         }
66     }
67
68     impl<'a, T:Foo> Foo for &'a T {
69         fn foo(&self, s: &str) {
70             (*self).foo(s);
71         }
72     }
73
74     struct CheckOnDrop(RefCell<String>, &'static str);
75     impl Drop for CheckOnDrop {
76         fn drop(&mut self) { assert_eq!(*self.0.borrow(), self.1); }
77     }
78
79     let c_long;
80     let (c, dt, dr, pt, pr, st, sr)
81         : (CheckOnDrop, Dt<_>, Dr<_>, Pt<_, _>, Pr<_>, St<_>, Sr<_>);
82     c_long = CheckOnDrop(RefCell::new("c_long".to_string()),
83                          "c_long|pr|pt|dr|dt");
84     c = CheckOnDrop(RefCell::new("c".to_string()),
85                     "c");
86
87     // No error: sufficiently long-lived state can be referenced in dtors
88     dt = Dt("dt", &c_long.0);
89     dr = Dr("dr", &c_long.0);
90
91     // No error: Drop impl asserts .1 (A and &'a _) are not accessed
92     pt = Pt("pt", &c.0, &c_long.0);
93     pr = Pr("pr", &c.0, &c_long.0);
94
95     // No error: St and Sr have no destructor.
96     st = St("st", &c.0);
97     sr = Sr("sr", &c.0);
98
99     println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
100     assert_eq!(*c_long.0.borrow(), "c_long");
101     assert_eq!(*c.0.borrow(), "c");
102 }