]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dropck/dropck-eyepatch-extern-crate.rs
Rollup merge of #55182 - jD91mZM2:rebased, r=alexcrichton
[rust.git] / src / test / ui / dropck / dropck-eyepatch-extern-crate.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 // aux-build:dropck_eyepatch_extern_crate.rs
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 // and that this attribute's effects are preserved when importing
18 // the type from another crate.
19 //
20 // See also dropck-eyepatch.rs for more information about the general
21 // structure of the test.
22 #![feature(rustc_attrs)]
23 extern crate dropck_eyepatch_extern_crate as other;
24
25 use other::{Dt,Dr,Pt,Pr,St,Sr};
26
27 fn main() { #![rustc_error] // rust-lang/rust#49855
28     use std::cell::Cell;
29     let c_long;
30     let (c, mut dt, mut dr, mut pt, mut pr, st, sr, c_shortest)
31         : (Cell<_>, Dt<_>, Dr<_>, Pt<_, _>, Pr<_>, St<_>, Sr<_>, Cell<_>);
32     c_long = Cell::new(1);
33     c = Cell::new(1);
34     c_shortest = Cell::new(1);
35
36     // No error: sufficiently long-lived state can be referenced in dtors
37     dt = Dt("dt", &c_long);
38     dr = Dr("dr", &c_long);
39
40     // Error: destructor order imprecisely modelled
41     dt = Dt("dt", &c);
42     //[ast]~^ ERROR `c` does not live long enough
43     dr = Dr("dr", &c);
44     //[ast]~^ ERROR `c` does not live long enough
45
46     // Error: `c_shortest` dies too soon for the references in dtors to be valid.
47     dt = Dt("dt", &c_shortest);
48     //[ast]~^ ERROR `c_shortest` does not live long enough
49     //[nll]~^^ ERROR `c_shortest` does not live long enough
50     dr = Dr("dr", &c_shortest);
51     //[ast]~^ ERROR `c_shortest` does not live long enough
52     // No error: Drop impl asserts .1 (A and &'a _) are not accessed
53     pt = Pt("pt", &c_shortest, &c_long);
54     pr = Pr("pr", &c_shortest, &c_long);
55
56     // Error: Drop impl's assertion does not apply to `B` nor `&'b _`
57     pt = Pt("pt", &c_long, &c_shortest);
58     //[ast]~^ ERROR `c_shortest` does not live long enough
59     pr = Pr("pr", &c_long, &c_shortest);
60     //[ast]~^ ERROR `c_shortest` does not live long enough
61
62     // No error: St and Sr have no destructor.
63     st = St("st", &c_shortest);
64     sr = Sr("sr", &c_shortest);
65
66     println!("{:?}", (dt.0, dr.0, pt.0, pr.0, st.0, sr.0));
67     use_imm(sr.1); use_imm(st.1); use_imm(pr.1); use_imm(pt.1); use_imm(dr.1); use_imm(dt.1);
68 }
69
70 fn use_imm<T>(_: &T) { }