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