]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dropck/dropck-eyepatch-reorder.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / dropck / dropck-eyepatch-reorder.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(dropck_eyepatch, rustc_attrs)]
12
13 // The point of this test is to test uses of `#[may_dangle]` attribute
14 // where the formal declaration order (in the impl generics) does not
15 // match the actual usage order (in the type instantiation).
16 //
17 // See also dropck-eyepatch.rs for more information about the general
18 // structure of the test.
19
20 use std::fmt;
21
22 struct Dt<A: fmt::Debug>(&'static str, A);
23 struct Dr<'a, B:'a+fmt::Debug>(&'static str, &'a B);
24 struct Pt<A: fmt::Debug, B: fmt::Debug>(&'static str, A, B);
25 struct Pr<'a, 'b, B:'a+'b+fmt::Debug>(&'static str, &'a B, &'b B);
26 struct St<A: fmt::Debug>(&'static str, A);
27 struct Sr<'a, B:'a+fmt::Debug>(&'static str, &'a B);
28
29 impl<A: fmt::Debug> Drop for Dt<A> {
30     fn drop(&mut self) { println!("drop {} {:?}", self.0, self.1); }
31 }
32 impl<'a, B: fmt::Debug> Drop for Dr<'a, B> {
33     fn drop(&mut self) { println!("drop {} {:?}", self.0, self.1); }
34 }
35 unsafe impl<B: fmt::Debug, #[may_dangle] A: fmt::Debug> Drop for Pt<A, B> {
36     // (unsafe to access self.1  due to #[may_dangle] on A)
37     fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); }
38 }
39 unsafe impl<'b, #[may_dangle] 'a, B: fmt::Debug> Drop for Pr<'a, 'b, B> {
40     // (unsafe to access self.1 due to #[may_dangle] on 'a)
41     fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); }
42 }
43
44 fn main() { #![rustc_error] // rust-lang/rust#49855
45     use std::cell::Cell;
46     let c_long;
47     let (c, mut dt, mut dr, mut pt, mut pr, st, sr, c_shortest)
48         : (Cell<_>, Dt<_>, Dr<_>, Pt<_, _>, Pr<_>, St<_>, Sr<_>, Cell<_>);
49     c_long = Cell::new(1);
50     c = Cell::new(1);
51     c_shortest = Cell::new(1);
52
53     // No error: sufficiently long-lived state can be referenced in dtors
54     dt = Dt("dt", &c_long);
55     dr = Dr("dr", &c_long);
56
57     // Error: destructor order imprecisely modelled
58     dt = Dt("dt", &c);
59     //~^ ERROR `c` does not live long enough
60     dr = Dr("dr", &c);
61     //~^ ERROR `c` does not live long enough
62
63     // Error: `c_shortest` dies too soon for the references in dtors to be valid.
64     dt = Dt("dt", &c_shortest);
65     //~^ ERROR `c_shortest` does not live long enough
66     dr = Dr("dr", &c_shortest);
67     //~^ ERROR `c_shortest` does not live long enough
68
69     // No error: Drop impl asserts .1 (A and &'a _) are not accessed
70     pt = Pt("pt", &c_shortest, &c_long);
71     pr = Pr("pr", &c_shortest, &c_long);
72
73     // Error: Drop impl's assertion does not apply to `B` nor `&'b _`
74     pt = Pt("pt", &c_long, &c_shortest);
75     //~^ ERROR `c_shortest` does not live long enough
76     pr = Pr("pr", &c_long, &c_shortest);
77     //~^ ERROR `c_shortest` does not live long enough
78
79     // No error: St and Sr have no destructor.
80     st = St("st", &c_shortest);
81     sr = Sr("sr", &c_shortest);
82
83     println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
84 }