]> git.lizzy.rs Git - rust.git/blob - src/test/ui/auxiliary/pub_and_stability.rs
Move some tests from ui-fulldeps to ui
[rust.git] / src / test / ui / auxiliary / pub_and_stability.rs
1 // Copyright 2017 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 // This crate attempts to enumerate the various scenarios for how a
12 // type can define fields and methods with various visibilities and
13 // stabilities.
14 //
15 // The basic stability pattern in this file has four cases:
16 // 1. no stability attribute at all
17 // 2. a stable attribute (feature "unit_test")
18 // 3. an unstable attribute that unit test declares (feature "unstable_declared")
19 // 4. an unstable attribute that unit test fails to declare (feature "unstable_undeclared")
20 //
21 // This file also covers four kinds of visibility: private,
22 // pub(module), pub(crate), and pub.
23 //
24 // However, since stability attributes can only be observed in
25 // cross-crate linkage scenarios, there is little reason to take the
26 // cross-product (4 stability cases * 4 visibility cases), because the
27 // first three visibility cases cannot be accessed outside this crate,
28 // and therefore stability is only relevant when the visibility is pub
29 // to the whole universe.
30 //
31 // (The only reason to do so would be if one were worried about the
32 // compiler having some subtle bug where adding a stability attribute
33 // introduces a privacy violation. As a way to provide evidence that
34 // this is not occurring, I have put stability attributes on some
35 // non-pub fields, marked with SILLY below)
36
37 #![feature(staged_api)]
38
39 #![stable(feature = "unit_test", since = "1.0.0")]
40
41 #[stable(feature = "unit_test", since = "1.0.0")]
42 pub use m::{Record, Trait, Tuple};
43
44 mod m {
45     #[derive(Default)]
46     #[stable(feature = "unit_test", since = "1.0.0")]
47     pub struct Record {
48         #[stable(feature = "unit_test", since = "1.0.0")]
49         pub a_stable_pub: i32,
50         #[unstable(feature = "unstable_declared", issue = "38412")]
51         pub a_unstable_declared_pub: i32,
52         #[unstable(feature = "unstable_undeclared", issue = "38412")]
53         pub a_unstable_undeclared_pub: i32,
54         #[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY
55         pub(crate) b_crate: i32,
56         #[unstable(feature = "unstable_declared", issue = "38412")] // SILLY
57         pub(in m) c_mod: i32,
58         #[stable(feature = "unit_test", since = "1.0.0")] // SILLY
59         d_priv: i32
60     }
61
62     #[derive(Default)]
63     #[stable(feature = "unit_test", since = "1.0.0")]
64     pub struct Tuple(
65         #[stable(feature = "unit_test", since = "1.0.0")]
66         pub i32,
67         #[unstable(feature = "unstable_declared", issue = "38412")]
68         pub i32,
69         #[unstable(feature = "unstable_undeclared", issue = "38412")]
70         pub i32,
71
72         pub(crate) i32,
73         pub(in m) i32,
74         i32);
75
76     impl Record {
77         #[stable(feature = "unit_test", since = "1.0.0")]
78         pub fn new() -> Self { Default::default() }
79     }
80
81     impl Tuple {
82         #[stable(feature = "unit_test", since = "1.0.0")]
83         pub fn new() -> Self { Default::default() }
84     }
85
86
87     #[stable(feature = "unit_test", since = "1.0.0")]
88     pub trait Trait {
89         #[stable(feature = "unit_test", since = "1.0.0")]
90         type Type;
91         #[stable(feature = "unit_test", since = "1.0.0")]
92         fn stable_trait_method(&self) -> Self::Type;
93         #[unstable(feature = "unstable_undeclared", issue = "38412")]
94         fn unstable_undeclared_trait_method(&self) -> Self::Type;
95         #[unstable(feature = "unstable_declared", issue = "38412")]
96         fn unstable_declared_trait_method(&self) -> Self::Type;
97     }
98
99     #[stable(feature = "unit_test", since = "1.0.0")]
100     impl Trait for Record {
101         type Type = i32;
102         fn stable_trait_method(&self) -> i32 { self.d_priv }
103         fn unstable_undeclared_trait_method(&self) -> i32 { self.d_priv }
104         fn unstable_declared_trait_method(&self) -> i32 { self.d_priv }
105     }
106
107     #[stable(feature = "unit_test", since = "1.0.0")]
108     impl Trait for Tuple {
109         type Type = i32;
110         fn stable_trait_method(&self) -> i32 { self.3 }
111         fn unstable_undeclared_trait_method(&self) -> i32 { self.3 }
112         fn unstable_declared_trait_method(&self) -> i32 { self.3 }
113     }
114
115     impl Record {
116         #[unstable(feature = "unstable_undeclared", issue = "38412")]
117         pub fn unstable_undeclared(&self) -> i32 { self.d_priv }
118         #[unstable(feature = "unstable_declared", issue = "38412")]
119         pub fn unstable_declared(&self) -> i32 { self.d_priv }
120         #[stable(feature = "unit_test", since = "1.0.0")]
121         pub fn stable(&self) -> i32 { self.d_priv }
122
123         #[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY
124         pub(crate) fn pub_crate(&self) -> i32 { self.d_priv }
125         #[unstable(feature = "unstable_declared", issue = "38412")] // SILLY
126         pub(in m) fn pub_mod(&self) -> i32 { self.d_priv }
127         #[stable(feature = "unit_test", since = "1.0.0")] // SILLY
128         fn private(&self) -> i32 { self.d_priv }
129     }
130
131     impl Tuple {
132         #[unstable(feature = "unstable_undeclared", issue = "38412")]
133         pub fn unstable_undeclared(&self) -> i32 { self.0 }
134         #[unstable(feature = "unstable_declared", issue = "38412")]
135         pub fn unstable_declared(&self) -> i32 { self.0 }
136         #[stable(feature = "unit_test", since = "1.0.0")]
137         pub fn stable(&self) -> i32 { self.0 }
138
139         pub(crate) fn pub_crate(&self) -> i32 { self.0 }
140         pub(in m) fn pub_mod(&self) -> i32 { self.0 }
141         fn private(&self) -> i32 { self.0 }
142     }
143 }