]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dropck/dropck-eyepatch-extern-crate.rs
Merge 'rust-clippy/master' into clippyup
[rust.git] / src / test / ui / dropck / dropck-eyepatch-extern-crate.rs
1 // aux-build:dropck_eyepatch_extern_crate.rs
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 // and that this attribute's effects are preserved when importing
8 // the type from another crate.
9 //
10 // See also dropck-eyepatch.rs for more information about the general
11 // structure of the test.
12 extern crate dropck_eyepatch_extern_crate as other;
13
14 use other::{Dt,Dr,Pt,Pr,St,Sr};
15
16 fn main() {
17     use std::cell::Cell;
18
19     // We use separate blocks with separate variable to prevent the error
20     // messages from being deduplicated.
21
22     {
23         let c_long;
24         let (mut dt, mut dr): (Dt<_>, Dr<_>);
25         c_long = Cell::new(1);
26
27         // No error: sufficiently long-lived state can be referenced in dtors
28         dt = Dt("dt", &c_long);
29         dr = Dr("dr", &c_long);
30     }
31
32     {
33         let (c, mut dt, mut dr): (Cell<_>, Dt<_>, Dr<_>);
34         c = Cell::new(1);
35
36         // No Error: destructor order precisely modelled
37         dt = Dt("dt", &c);
38         dr = Dr("dr", &c);
39     }
40
41     {
42         let (mut dt, mut dr, c_shortest): (Dt<_>, Dr<_>, Cell<_>);
43         c_shortest = Cell::new(1);
44
45         // Error: `c_shortest` dies too soon for the references in dtors to be valid.
46         dt = Dt("dt", &c_shortest);
47         //~^ ERROR `c_shortest` does not live long enough
48         dr = Dr("dr", &c_shortest);
49     }
50
51     {
52         let c_long;
53         let (mut pt, mut pr, c_shortest): (Pt<_, _>, Pr<_>, Cell<_>);
54         c_long = Cell::new(1);
55         c_shortest = Cell::new(1);
56
57         // No error: Drop impl asserts .1 (A and &'a _) are not accessed
58         pt = Pt("pt", &c_shortest, &c_long);
59         pr = Pr("pr", &c_shortest, &c_long);
60     }
61
62     {
63         let c_long;
64         let (mut pt, mut pr, c_shortest): (Pt<_, _>, Pr<_>, Cell<_>);
65         c_long = Cell::new(1);
66         c_shortest = Cell::new(1);
67         // Error: Drop impl's assertion does not apply to `B` nor `&'b _`
68         pt = Pt("pt", &c_long, &c_shortest);
69         //~^ ERROR `c_shortest` does not live long enough
70         pr = Pr("pr", &c_long, &c_shortest);
71     }
72
73     {
74         let (st, sr, c_shortest): (St<_>, Sr<_>, Cell<_>);
75         c_shortest = Cell::new(1);
76         // No error: St and Sr have no destructor.
77         st = St("st", &c_shortest);
78         sr = Sr("sr", &c_shortest);
79     }
80 }
81
82 fn use_imm<T>(_: &T) { }