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