]> git.proxmox.com Git - rustc.git/blob - tests/ui/dropck/dropck-eyepatch.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / dropck / dropck-eyepatch.rs
1 #![feature(dropck_eyepatch)]
2
3 // The point of this test is to illustrate that the `#[may_dangle]`
4 // attribute specifically allows, in the context of a type
5 // implementing `Drop`, a generic parameter to be instantiated with a
6 // lifetime that does not strictly outlive the owning type itself.
7 //
8 // Here we test that only the expected errors are issued.
9 //
10 // The illustration is made concrete by comparison with two variations
11 // on the type with `#[may_dangle]`:
12 //
13 // 1. an analogous type that does not implement `Drop` (and thus
14 // should exhibit maximal flexibility with respect to dropck), and
15 //
16 // 2. an analogous type that does not use `#[may_dangle]` (and thus
17 // should exhibit the standard limitations imposed by dropck.
18 //
19 // The types in this file follow a pattern, {D,P,S}{t,r}, where:
20 //
21 // - D means "I implement Drop"
22 //
23 // - P means "I implement Drop but guarantee my (first) parameter is
24 // pure, i.e., not accessed from the destructor"; no other parameters
25 // are pure.
26 //
27 // - S means "I do not implement Drop"
28 //
29 // - t suffix is used when the first generic is a type
30 //
31 // - r suffix is used when the first generic is a lifetime.
32
33 use std::fmt;
34
35 struct Dt<A: fmt::Debug>(&'static str, A);
36 struct Dr<'a, B:'a+fmt::Debug>(&'static str, &'a B);
37 struct Pt<A,B: fmt::Debug>(&'static str, A, B);
38 struct Pr<'a, 'b, B:'a+'b+fmt::Debug>(&'static str, &'a B, &'b B);
39 struct St<A: fmt::Debug>(&'static str, A);
40 struct Sr<'a, B:'a+fmt::Debug>(&'static str, &'a B);
41
42 impl<A: fmt::Debug> Drop for Dt<A> {
43 fn drop(&mut self) { println!("drop {} {:?}", self.0, self.1); }
44 }
45 impl<'a, B: fmt::Debug> Drop for Dr<'a, B> {
46 fn drop(&mut self) { println!("drop {} {:?}", self.0, self.1); }
47 }
48 unsafe impl<#[may_dangle] A, B: fmt::Debug> Drop for Pt<A, B> {
49 // (unsafe to access self.1 due to #[may_dangle] on A)
50 fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); }
51 }
52 unsafe impl<#[may_dangle] 'a, 'b, B: fmt::Debug> Drop for Pr<'a, 'b, B> {
53 // (unsafe to access self.1 due to #[may_dangle] on 'a)
54 fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); }
55 }
56
57
58 fn main() {
59 use std::cell::Cell;
60
61 // We use separate blocks with separate variable to prevent the error
62 // messages from being deduplicated.
63
64 {
65 let c_long;
66 let (mut dt, mut dr): (Dt<_>, Dr<_>);
67 c_long = Cell::new(1);
68
69 // No error: sufficiently long-lived state can be referenced in dtors
70 dt = Dt("dt", &c_long);
71 dr = Dr("dr", &c_long);
72 }
73
74 {
75 let (c, mut dt, mut dr): (Cell<_>, Dt<_>, Dr<_>);
76 c = Cell::new(1);
77
78 // No Error: destructor order precisely modelled
79 dt = Dt("dt", &c);
80 dr = Dr("dr", &c);
81 }
82
83 {
84 let (mut dt, mut dr, c_shortest): (Dt<_>, Dr<_>, Cell<_>);
85 c_shortest = Cell::new(1);
86
87 // Error: `c_shortest` dies too soon for the references in dtors to be valid.
88 dt = Dt("dt", &c_shortest);
89 //~^ ERROR `c_shortest` does not live long enough
90 dr = Dr("dr", &c_shortest);
91 }
92
93 {
94 let c_long;
95 let (mut pt, mut pr, c_shortest): (Pt<_, _>, Pr<_>, Cell<_>);
96 c_long = Cell::new(1);
97 c_shortest = Cell::new(1);
98
99 // No error: Drop impl asserts .1 (A and &'a _) are not accessed
100 pt = Pt("pt", &c_shortest, &c_long);
101 pr = Pr("pr", &c_shortest, &c_long);
102 }
103
104 {
105 let c_long;
106 let (mut pt, mut pr, c_shortest): (Pt<_, _>, Pr<_>, Cell<_>);
107 c_long = Cell::new(1);
108 c_shortest = Cell::new(1);
109 // Error: Drop impl's assertion does not apply to `B` nor `&'b _`
110 pt = Pt("pt", &c_long, &c_shortest);
111 //~^ ERROR `c_shortest` does not live long enough
112 pr = Pr("pr", &c_long, &c_shortest);
113 }
114
115 {
116 let (st, sr, c_shortest): (St<_>, Sr<_>, Cell<_>);
117 c_shortest = Cell::new(1);
118 // No error: St and Sr have no destructor.
119 st = St("st", &c_shortest);
120 sr = Sr("sr", &c_shortest);
121 }
122 }
123 fn use_imm<T>(_: &T) { }