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