|
| 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 visiblities 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 visiblity 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 | +#![feature(pub_restricted)] |
| 39 | + |
| 40 | +#![stable(feature = "unit_test", since = "0.0.0")] |
| 41 | + |
| 42 | +#[stable(feature = "unit_test", since = "0.0.0")] |
| 43 | +pub use m::{Record, Trait, Tuple}; |
| 44 | + |
| 45 | +mod m { |
| 46 | + #[derive(Default)] |
| 47 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 48 | + pub struct Record { |
| 49 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 50 | + pub a_stable_pub: i32, |
| 51 | + #[unstable(feature = "unstable_declared", issue = "38412")] |
| 52 | + pub a_unstable_declared_pub: i32, |
| 53 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] |
| 54 | + pub a_unstable_undeclared_pub: i32, |
| 55 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY |
| 56 | + pub(crate) b_crate: i32, |
| 57 | + #[unstable(feature = "unstable_declared", issue = "38412")] // SILLY |
| 58 | + pub(m) c_mod: i32, |
| 59 | + #[stable(feature = "unit_test", since = "0.0.0")] // SILLY |
| 60 | + d_priv: i32 |
| 61 | + } |
| 62 | + |
| 63 | + #[derive(Default)] |
| 64 | + #[stable(feature = "unit_test", since = "1.0.0")] |
| 65 | + pub struct Tuple( |
| 66 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 67 | + pub i32, |
| 68 | + #[unstable(feature = "unstable_declared", issue = "38412")] |
| 69 | + pub i32, |
| 70 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] |
| 71 | + pub i32, |
| 72 | + |
| 73 | + pub(crate) i32, |
| 74 | + pub(m) i32, |
| 75 | + i32); |
| 76 | + |
| 77 | + impl Record { |
| 78 | + #[stable(feature = "unit_test", since = "1.0.0")] |
| 79 | + pub fn new() -> Self { Default::default() } |
| 80 | + } |
| 81 | + |
| 82 | + impl Tuple { |
| 83 | + #[stable(feature = "unit_test", since = "1.0.0")] |
| 84 | + pub fn new() -> Self { Default::default() } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 89 | + pub trait Trait { |
| 90 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 91 | + type Type; |
| 92 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 93 | + fn stable_trait_method(&self) -> Self::Type; |
| 94 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] |
| 95 | + fn unstable_undeclared_trait_method(&self) -> Self::Type; |
| 96 | + #[unstable(feature = "unstable_declared", issue = "38412")] |
| 97 | + fn unstable_declared_trait_method(&self) -> Self::Type; |
| 98 | + } |
| 99 | + |
| 100 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 101 | + impl Trait for Record { |
| 102 | + type Type = i32; |
| 103 | + fn stable_trait_method(&self) -> i32 { self.d_priv } |
| 104 | + fn unstable_undeclared_trait_method(&self) -> i32 { self.d_priv } |
| 105 | + fn unstable_declared_trait_method(&self) -> i32 { self.d_priv } |
| 106 | + } |
| 107 | + |
| 108 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 109 | + impl Trait for Tuple { |
| 110 | + type Type = i32; |
| 111 | + fn stable_trait_method(&self) -> i32 { self.3 } |
| 112 | + fn unstable_undeclared_trait_method(&self) -> i32 { self.3 } |
| 113 | + fn unstable_declared_trait_method(&self) -> i32 { self.3 } |
| 114 | + } |
| 115 | + |
| 116 | + impl Record { |
| 117 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] |
| 118 | + pub fn unstable_undeclared(&self) -> i32 { self.d_priv } |
| 119 | + #[unstable(feature = "unstable_declared", issue = "38412")] |
| 120 | + pub fn unstable_declared(&self) -> i32 { self.d_priv } |
| 121 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 122 | + pub fn stable(&self) -> i32 { self.d_priv } |
| 123 | + |
| 124 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] // SILLY |
| 125 | + pub(crate) fn pub_crate(&self) -> i32 { self.d_priv } |
| 126 | + #[unstable(feature = "unstable_declared", issue = "38412")] // SILLY |
| 127 | + pub(m) fn pub_mod(&self) -> i32 { self.d_priv } |
| 128 | + #[stable(feature = "unit_test", since = "0.0.0")] // SILLY |
| 129 | + fn private(&self) -> i32 { self.d_priv } |
| 130 | + } |
| 131 | + |
| 132 | + impl Tuple { |
| 133 | + #[unstable(feature = "unstable_undeclared", issue = "38412")] |
| 134 | + pub fn unstable_undeclared(&self) -> i32 { self.0 } |
| 135 | + #[unstable(feature = "unstable_declared", issue = "38412")] |
| 136 | + pub fn unstable_declared(&self) -> i32 { self.0 } |
| 137 | + #[stable(feature = "unit_test", since = "0.0.0")] |
| 138 | + pub fn stable(&self) -> i32 { self.0 } |
| 139 | + |
| 140 | + pub(crate) fn pub_crate(&self) -> i32 { self.0 } |
| 141 | + pub(m) fn pub_mod(&self) -> i32 { self.0 } |
| 142 | + fn private(&self) -> i32 { self.0 } |
| 143 | + } |
| 144 | +} |
0 commit comments