]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/private-in-public.rs
b819ef116efe91d5c8eb0949404edd441c4ff79f
[rust.git] / src / test / compile-fail / private-in-public.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
17 mod types {
18     struct Priv;
19     pub struct Pub;
20     pub trait PubTr {
21         type Alias;
22     }
23
24     pub const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface
25     pub static S: Priv = Priv; //~ ERROR private type `types::Priv` in public interface
26     pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface
27     pub fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface
28     pub struct S1(pub Priv); //~ ERROR private type `types::Priv` in public interface
29     pub struct S2 { pub field: Priv } //~ ERROR private type `types::Priv` in public interface
30     impl Pub {
31         pub const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface
32         pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface
33         pub fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface
34     }
35 }
36
37 mod traits {
38     trait PrivTr {}
39     pub struct Pub<T>(T);
40     pub trait PubTr {}
41
42     pub enum E<T: PrivTr> { V(T) } //~ ERROR private trait `traits::PrivTr` in public interface
43     pub fn f<T: PrivTr>(arg: T) {} //~ ERROR private trait `traits::PrivTr` in public interface
44     pub struct S1<T: PrivTr>(T); //~ ERROR private trait `traits::PrivTr` in public interface
45     impl<T: PrivTr> Pub<T> { //~ ERROR private trait `traits::PrivTr` in public interface
46         pub fn f<U: PrivTr>(arg: U) {} //~ ERROR private trait `traits::PrivTr` in public interface
47     }
48 }
49
50 mod traits_where {
51     trait PrivTr {}
52     pub struct Pub<T>(T);
53     pub trait PubTr {}
54
55     pub enum E<T> where T: PrivTr { V(T) }
56     //~^ ERROR private trait `traits_where::PrivTr` in public interface
57     pub fn f<T>(arg: T) where T: PrivTr {}
58     //~^ ERROR private trait `traits_where::PrivTr` in public interface
59     pub struct S1<T>(T) where T: PrivTr;
60     //~^ ERROR private trait `traits_where::PrivTr` in public interface
61     impl<T> Pub<T> where T: PrivTr {
62     //~^ ERROR private trait `traits_where::PrivTr` in public interface
63         pub fn f<U>(arg: U) where U: PrivTr {}
64         //~^ ERROR private trait `traits_where::PrivTr` in public interface
65     }
66 }
67
68 mod generics {
69     struct Priv<T = u8>(T);
70     pub struct Pub<T = u8>(T);
71     trait PrivTr<T> {}
72     pub trait PubTr<T> {}
73
74     pub fn f1(arg: [Priv; 1]) {} //~ ERROR private type `generics::Priv` in public interface
75     pub fn f2(arg: Pub<Priv>) {} //~ ERROR private type `generics::Priv` in public interface
76     pub fn f3(arg: Priv<Pub>) {}
77     //~^ ERROR private type `generics::Priv<generics::Pub>` in public interface
78 }
79
80 mod impls {
81     struct Priv;
82     pub struct Pub;
83     trait PrivTr {
84         type Alias;
85     }
86     pub trait PubTr {
87         type Alias;
88     }
89
90     impl Pub {
91         pub fn f(arg: Priv) {} //~ ERROR private type `impls::Priv` in public interface
92     }
93 }
94
95 mod aliases_pub {
96     struct Priv;
97     mod m {
98         pub struct Pub1;
99         pub struct Pub2;
100         pub struct Pub3;
101         pub trait PubTr<T = u8> {
102             type Check = u8;
103         }
104     }
105
106     use self::m::Pub1 as PrivUseAlias;
107     use self::m::PubTr as PrivUseAliasTr;
108     type PrivAlias = m::Pub2;
109     trait PrivTr {
110         type Assoc = m::Pub3;
111     }
112     impl PrivTr for Priv {}
113
114     // This should be OK, but associated type aliases are not substituted yet
115     pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
116     //~^ ERROR private type `<aliases_pub::Priv as aliases_pub::PrivTr>::Assoc` in public interface
117     //~| ERROR private type `aliases_pub::Priv` in public interface
118
119     impl PrivUseAlias {
120         pub fn f(arg: Priv) {} //~ ERROR private type `aliases_pub::Priv` in public interface
121     }
122 }
123
124 mod aliases_priv {
125     struct Priv;
126
127     struct Priv1;
128     struct Priv2;
129     struct Priv3;
130     trait PrivTr1<T = u8> {
131         type Check = u8;
132     }
133
134     use self::Priv1 as PrivUseAlias;
135     use self::PrivTr1 as PrivUseAliasTr;
136     type PrivAlias = Priv2;
137     trait PrivTr {
138         type Assoc = Priv3;
139     }
140     impl PrivTr for Priv {}
141
142     pub fn f1(arg: PrivUseAlias) {} //~ ERROR private type `aliases_priv::Priv1` in public interface
143     pub fn f2(arg: PrivAlias) {} //~ ERROR private type `aliases_priv::Priv2` in public interface
144     pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
145     //~^ ERROR private type `<aliases_priv::Priv as aliases_priv::PrivTr>::Assoc` in public
146     //~| ERROR private type `aliases_priv::Priv` in public interface
147 }
148
149 mod aliases_params {
150     struct Priv;
151     type PrivAliasGeneric<T = Priv> = T;
152     type Result<T> = ::std::result::Result<T, Priv>;
153
154     pub fn f2(arg: PrivAliasGeneric) {}
155     //~^ ERROR private type `aliases_params::Priv` in public interface
156     pub fn f3(arg: Result<u8>) {} //~ ERROR private type `aliases_params::Priv` in public interface
157 }
158
159 fn main() {}