1 // Private types and traits are not allowed in public interfaces.
2 // This test also ensures that the checks are performed even inside private modules.
3 
4 #![feature(associated_type_defaults)]
5 #![deny(private_in_public)]
6 #![allow(improper_ctypes)]
7 
8 mod types {
9     struct Priv;
10     pub struct Pub;
11     pub trait PubTr {
12         type Alias;
13     }
14 
15     pub type Alias = Priv; //~ ERROR private type `types::Priv` in public interface
16     //~^ WARNING hard error
17     pub enum E {
18         V1(Priv), //~ ERROR private type `types::Priv` in public interface
19         //~^ WARNING hard error
20         V2 { field: Priv }, //~ ERROR private type `types::Priv` in public interface
21         //~^ WARNING hard error
22     }
23     pub trait Tr {
24         const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface
25         //~^ WARNING hard error
26         type Alias = Priv; //~ ERROR private type `types::Priv` in public interface
f1(arg: Priv)27         fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface
28         //~^ WARNING hard error
f2() -> Priv29         fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface
30         //~^ WARNING hard error
31     }
32     extern "C" {
33         pub static ES: Priv; //~ ERROR private type `types::Priv` in public interface
34         //~^ WARNING hard error
ef1(arg: Priv)35         pub fn ef1(arg: Priv); //~ ERROR private type `types::Priv` in public interface
36         //~^ WARNING hard error
ef2() -> Priv37         pub fn ef2() -> Priv; //~ ERROR private type `types::Priv` in public interface
38         //~^ WARNING hard error
39     }
40     impl PubTr for Pub {
41         type Alias = Priv; //~ ERROR private type `types::Priv` in public interface
42     }
43 }
44 
45 mod traits {
46     trait PrivTr {}
47     pub struct Pub<T>(T);
48     pub trait PubTr {}
49 
50     pub type Alias<T: PrivTr> = T; //~ ERROR private trait `traits::PrivTr` in public interface
51     //~| WARNING hard error
52     //~| WARNING bounds on generic parameters are not enforced in type aliases
53     pub trait Tr1: PrivTr {} //~ ERROR private trait `traits::PrivTr` in public interface
54     //~^ WARNING hard error
55     pub trait Tr2<T: PrivTr> {} //~ ERROR private trait `traits::PrivTr` in public interface
56         //~^ WARNING hard error
57     pub trait Tr3 {
58         type Alias: PrivTr;
59         //~^ ERROR private trait `traits::PrivTr` in public interface
60         //~| WARNING hard error
f<T: PrivTr>(arg: T)61         fn f<T: PrivTr>(arg: T) {} //~ ERROR private trait `traits::PrivTr` in public interface
62         //~^ WARNING hard error
63     }
64     impl<T: PrivTr> Pub<T> {} //~ ERROR private trait `traits::PrivTr` in public interface
65         //~^ WARNING hard error
66     impl<T: PrivTr> PubTr for Pub<T> {} //~ ERROR private trait `traits::PrivTr` in public interface
67         //~^ WARNING hard error
68 }
69 
70 mod traits_where {
71     trait PrivTr {}
72     pub struct Pub<T>(T);
73     pub trait PubTr {}
74 
75     pub type Alias<T> where T: PrivTr = T;
76         //~^ ERROR private trait `traits_where::PrivTr` in public interface
77         //~| WARNING hard error
78         //~| WARNING where clauses are not enforced in type aliases
79     pub trait Tr2<T> where T: PrivTr {}
80         //~^ ERROR private trait `traits_where::PrivTr` in public interface
81         //~| WARNING hard error
82     pub trait Tr3 {
f<T>(arg: T) where T: PrivTr83         fn f<T>(arg: T) where T: PrivTr {}
84         //~^ ERROR private trait `traits_where::PrivTr` in public interface
85         //~| WARNING hard error
86     }
87     impl<T> Pub<T> where T: PrivTr {}
88         //~^ ERROR private trait `traits_where::PrivTr` in public interface
89         //~| WARNING hard error
90     impl<T> PubTr for Pub<T> where T: PrivTr {}
91         //~^ ERROR private trait `traits_where::PrivTr` in public interface
92         //~| WARNING hard error
93 }
94 
95 mod generics {
96     struct Priv<T = u8>(T);
97     pub struct Pub<T = u8>(T);
98     trait PrivTr<T> {}
99     pub trait PubTr<T> {}
100 
101     pub trait Tr1: PrivTr<Pub> {}
102         //~^ ERROR private trait `generics::PrivTr<generics::Pub>` in public interface
103         //~| WARNING hard error
104     pub trait Tr2: PubTr<Priv> {} //~ ERROR private type `generics::Priv` in public interface
105         //~^ WARNING hard error
106     pub trait Tr3: PubTr<[Priv; 1]> {} //~ ERROR private type `generics::Priv` in public interface
107         //~^ WARNING hard error
108     pub trait Tr4: PubTr<Pub<Priv>> {} //~ ERROR private type `generics::Priv` in public interface
109         //~^ WARNING hard error
110 }
111 
112 mod impls {
113     struct Priv;
114     pub struct Pub;
115     trait PrivTr {
116         type Alias;
117     }
118     pub trait PubTr {
119         type Alias;
120     }
121 
122     impl Priv {
f(arg: Priv)123         pub fn f(arg: Priv) {} // OK
124     }
125     impl PrivTr for Priv {
126         type Alias = Priv; // OK
127     }
128     impl PubTr for Priv {
129         type Alias = Priv; // OK
130     }
131     impl PrivTr for Pub {
132         type Alias = Priv; // OK
133     }
134     impl PubTr for Pub {
135         type Alias = Priv; //~ ERROR private type `impls::Priv` in public interface
136     }
137 }
138 
139 mod impls_generics {
140     struct Priv<T = u8>(T);
141     pub struct Pub<T = u8>(T);
142     trait PrivTr<T = u8> {
143         type Alias;
144     }
145     pub trait PubTr<T = u8> {
146         type Alias;
147     }
148 
149     impl Priv<Pub> {
f(arg: Priv)150         pub fn f(arg: Priv) {} // OK
151     }
152     impl Pub<Priv> {
f(arg: Priv)153         pub fn f(arg: Priv) {} // OK
154     }
155     impl PrivTr<Pub> for Priv {
156         type Alias = Priv; // OK
157     }
158     impl PubTr<Priv> for Priv {
159         type Alias = Priv; // OK
160     }
161     impl PubTr for Priv<Pub> {
162         type Alias = Priv; // OK
163     }
164     impl PubTr for [Priv; 1] {
165         type Alias = Priv; // OK
166     }
167     impl PubTr for Pub<Priv> {
168         type Alias = Priv; // OK
169     }
170     impl PrivTr<Pub> for Pub {
171         type Alias = Priv; // OK
172     }
173     impl PubTr<Priv> for Pub {
174         type Alias = Priv; // OK
175     }
176 }
177 
178 mod aliases_pub {
179     struct Priv;
180     mod m {
181         pub struct Pub1;
182         pub struct Pub2;
183         pub struct Pub3;
184         pub trait PubTr<T = u8> {
185             type Check = u8;
186         }
187     }
188 
189     use self::m::Pub1 as PrivUseAlias;
190     use self::m::PubTr as PrivUseAliasTr;
191     type PrivAlias = m::Pub2;
192     trait PrivTr {
193         type AssocAlias;
194     }
195     impl PrivTr for Priv {
196         type AssocAlias = m::Pub3;
197     }
198 
f1(arg: PrivUseAlias)199     pub fn f1(arg: PrivUseAlias) {} // OK
f2(arg: PrivAlias)200     pub fn f2(arg: PrivAlias) {} // OK
201 
202     pub trait Tr1: PrivUseAliasTr {} // OK
203     pub trait Tr2: PrivUseAliasTr<PrivAlias> {} // OK
204 
205     impl PrivAlias {
f(arg: Priv)206         pub fn f(arg: Priv) {} //~ ERROR private type `aliases_pub::Priv` in public interface
207         //~^ WARNING hard error
208     }
209     impl PrivUseAliasTr for PrivUseAlias {
210         type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface
211     }
212     impl PrivUseAliasTr for PrivAlias {
213         type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface
214     }
215     impl PrivUseAliasTr for <Priv as PrivTr>::AssocAlias {
216         type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface
217     }
218     impl PrivUseAliasTr for Option<<Priv as PrivTr>::AssocAlias> {
219         type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface
220     }
221     impl PrivUseAliasTr for (<Priv as PrivTr>::AssocAlias, Priv) {
222         type Check = Priv; // OK
223     }
224     impl PrivUseAliasTr for Option<(<Priv as PrivTr>::AssocAlias, Priv)> {
225         type Check = Priv; // OK
226     }
227 }
228 
229 mod aliases_priv {
230     struct Priv;
231 
232     struct Priv1;
233     struct Priv2;
234     struct Priv3;
235     trait PrivTr1<T = u8> {
236         type Check = u8;
237     }
238 
239     use self::Priv1 as PrivUseAlias;
240     use self::PrivTr1 as PrivUseAliasTr;
241     type PrivAlias = Priv2;
242     trait PrivTr {
243         type AssocAlias;
244     }
245     impl PrivTr for Priv {
246         type AssocAlias = Priv3;
247     }
248 
249     pub trait Tr1: PrivUseAliasTr {}
250         //~^ ERROR private trait `PrivTr1` in public interface
251         //~| WARNING hard error
252     pub trait Tr2: PrivUseAliasTr<PrivAlias> {}
253         //~^ ERROR private trait `PrivTr1<Priv2>` in public interface
254         //~| WARNING hard error
255         //~| ERROR private type `Priv2` in public interface
256         //~| WARNING hard error
257 
258     impl PrivUseAlias {
f(arg: Priv)259         pub fn f(arg: Priv) {} // OK
260     }
261     impl PrivAlias {
f(arg: Priv)262         pub fn f(arg: Priv) {} // OK
263     }
264     impl PrivUseAliasTr for PrivUseAlias {
265         type Check = Priv; // OK
266     }
267     impl PrivUseAliasTr for PrivAlias {
268         type Check = Priv; // OK
269     }
270     impl PrivUseAliasTr for <Priv as PrivTr>::AssocAlias {
271         type Check = Priv; // OK
272     }
273 }
274 
275 mod aliases_params {
276     struct Priv;
277     type PrivAliasGeneric<T = Priv> = T;
278     type Result<T> = ::std::result::Result<T, Priv>;
279 
f1(arg: PrivAliasGeneric<u8>)280     pub fn f1(arg: PrivAliasGeneric<u8>) {} // OK, not an error
281 }
282 
main()283 fn main() {}
284