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