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