]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issues/issue-24805-dropck-itemless.rs
1d1bd21075bf468f27c744c7cac019d2ac6d8098
[rust.git] / src / test / run-pass / issues / issue-24805-dropck-itemless.rs
1 // Copyright 2015 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 // run-pass
12 #![allow(deprecated)]
13
14 // Check that item-less traits do not cause dropck to inject extra
15 // region constraints.
16
17 #![allow(non_camel_case_types)]
18
19 #![feature(dropck_parametricity)]
20
21 trait UserDefined { }
22
23 impl UserDefined for i32 { }
24 impl<'a, T> UserDefined for &'a T { }
25
26 // e.g. `impl_drop!(Send, D_Send)` expands to:
27 //   ```rust
28 //   struct D_Send<T:Send>(T);
29 //   impl<T:Send> Drop for D_Send<T> { fn drop(&mut self) { } }
30 //   ```
31 macro_rules! impl_drop {
32     ($Bound:ident, $Id:ident) => {
33         struct $Id<T:$Bound>(T);
34         impl <T:$Bound> Drop for $Id<T> {
35             #[unsafe_destructor_blind_to_params]
36             fn drop(&mut self) { }
37         }
38     }
39 }
40
41 impl_drop!{Send,         D_Send}
42 impl_drop!{Sized,        D_Sized}
43
44 // See note below regarding Issue 24895
45 // impl_drop!{Copy,         D_Copy}
46
47 impl_drop!{Sync,         D_Sync}
48 impl_drop!{UserDefined,  D_UserDefined}
49
50 macro_rules! body {
51     ($id:ident) => { {
52         // `_d` and `d1` are assigned the *same* lifetime by region inference ...
53         let (_d, d1);
54
55         d1 = $id(1);
56         // ... we store a reference to `d1` within `_d` ...
57         _d = $id(&d1);
58
59         // ... a *conservative* dropck will thus complain, because it
60         // thinks Drop of _d could access the already dropped `d1`.
61     } }
62 }
63
64 fn f_send() { body!(D_Send) }
65 fn f_sized() { body!(D_Sized) }
66 fn f_sync() { body!(D_Sync) }
67
68 // Issue 24895: Copy: Clone implies `impl<T:Copy> Drop for ...` can
69 // access a user-defined clone() method, which causes this test case
70 // to fail.
71 //
72 // If 24895 is resolved by removing the `Copy: Clone` relationship,
73 // then this definition and the call below should be uncommented. If
74 // it is resolved by deciding to keep the `Copy: Clone` relationship,
75 // then this comment and the associated bits of code can all be
76 // removed.
77
78 // fn f_copy() { body!(D_Copy) }
79
80 fn f_userdefined() { body!(D_UserDefined) }
81
82 fn main() {
83     f_send();
84     f_sized();
85     // See note above regarding Issue 24895.
86     // f_copy();
87     f_sync();
88     f_userdefined();
89 }