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
f1(arg: Priv)15     pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface
f2() -> Priv16     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
f1(arg: Priv)21         pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface
f2() -> Priv22         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
f<T: PrivTr>(arg: T)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
f<U: PrivTr>(arg: U)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
f<T>(arg: T) where T: PrivTr46     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
f<U>(arg: U) where U: PrivTr52         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 
f1(arg: [Priv; 1])63     pub fn f1(arg: [Priv; 1]) {} //~ ERROR private type `generics::Priv` in public interface
f2(arg: Pub<Priv>)64     pub fn f2(arg: Pub<Priv>) {} //~ ERROR private type `generics::Priv` in public interface
f3(arg: Priv<Pub>)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 {
f(arg: Priv)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
f3(arg: <Priv as PrivTr>::Assoc)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 {
f(arg: Priv)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 
f1(arg: PrivUseAlias)131     pub fn f1(arg: PrivUseAlias) {} //~ ERROR private type `Priv1` in public interface
f2(arg: PrivAlias)132     pub fn f2(arg: PrivAlias) {} //~ ERROR private type `Priv2` in public interface
f3(arg: <Priv as PrivTr>::Assoc)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 
f2(arg: PrivAliasGeneric)143     pub fn f2(arg: PrivAliasGeneric) {}
144     //~^ ERROR private type `aliases_params::Priv` in public interface
f3(arg: Result<u8>)145     pub fn f3(arg: Result<u8>) {} //~ ERROR private type `aliases_params::Priv` in public interface
146 }
147 
main()148 fn main() {}
149