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