]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/privacy1.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / test / compile-fail / privacy1.rs
1 // Copyright 2013 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 #![feature(lang_items, start, no_std)]
12 #![no_std] // makes debugging this test *a lot* easier (during resolve)
13
14 #[lang="sized"]
15 pub trait Sized {}
16
17 #[lang="copy"]
18 pub trait Copy {}
19
20 mod bar {
21     // shouldn't bring in too much
22     pub use self::glob::*;
23
24     // can't publicly re-export private items
25     pub use self::baz::{foo, bar};
26     //~^ ERROR: function `bar` is private
27
28     pub use self::private::ppriv;
29     //~^ ERROR: function `ppriv` is private
30
31     pub struct A;
32     impl A {
33         pub fn foo() {}
34         fn bar() {}
35
36         pub fn foo2(&self) {}
37         fn bar2(&self) {}
38     }
39
40     trait B {
41         fn foo() -> Self;
42     }
43
44     impl B for isize { fn foo() -> isize { 3 } }
45
46     pub enum Enum {
47         Pub
48     }
49
50     mod baz {
51         pub struct A;
52         impl A {
53             pub fn foo() {}
54             fn bar() {}
55
56             pub fn foo2(&self) {}
57             fn bar2(&self) {}
58         }
59
60         // both of these are re-exported by `bar`, but only one should be
61         // validly re-exported
62         pub fn foo() {}
63         fn bar() {}
64     }
65
66     extern {
67         fn epriv();
68         pub fn epub();
69     }
70
71     fn test() {
72         self::Enum::Pub;
73         unsafe {
74             epriv();
75             epub();
76         }
77         self::baz::A;
78         self::baz::A::foo();
79         self::baz::A::bar(); //~ ERROR: method `bar` is private
80         self::baz::A.foo2();
81         self::baz::A.bar2(); //~ ERROR: method `bar2` is private
82
83         // this used to cause an ICE in privacy traversal.
84         super::gpub();
85     }
86
87     mod glob {
88         pub fn gpub() {}
89         fn gpriv() {}
90     }
91
92     mod private {
93         fn ppriv() {}
94     }
95 }
96
97 pub fn gpub() {}
98
99 fn lol() {
100     bar::A;
101     bar::A::foo();
102     bar::A::bar(); //~ ERROR: method `bar` is private
103     bar::A.foo2();
104     bar::A.bar2(); //~ ERROR: method `bar2` is private
105 }
106
107 mod foo {
108     fn test() {
109         ::bar::A::foo();
110         ::bar::A::bar();        //~ ERROR: method `bar` is private
111         ::bar::A.foo2();
112         ::bar::A.bar2();        //~ ERROR: method `bar2` is private
113         ::bar::baz::A::foo();   //~ ERROR: method `foo` is inaccessible
114                                 //~^ NOTE: module `baz` is private
115         ::bar::baz::A::bar();   //~ ERROR: method `bar` is private
116         ::bar::baz::A.foo2();   //~ ERROR: struct `A` is inaccessible
117                                 //~^ NOTE: module `baz` is private
118         ::bar::baz::A.bar2();   //~ ERROR: struct `A` is inaccessible
119                                 //~^ ERROR: method `bar2` is private
120                                 //~^^ NOTE: module `baz` is private
121
122         let _: isize =
123         ::bar::B::foo();        //~ ERROR: method `foo` is inaccessible
124                                 //~^ NOTE: trait `B` is private
125         ::lol();
126
127         ::bar::Enum::Pub;
128
129         unsafe {
130             ::bar::epriv(); //~ ERROR: function `epriv` is private
131             ::bar::epub();
132         }
133
134         ::bar::foo();
135         ::bar::bar();
136
137         ::bar::gpub();
138
139         ::bar::baz::foo(); //~ ERROR: function `foo` is inaccessible
140                            //~^ NOTE: module `baz` is private
141         ::bar::baz::bar(); //~ ERROR: function `bar` is private
142     }
143
144     fn test2() {
145         use bar::baz::{foo, bar};
146         //~^ ERROR: function `foo` is inaccessible
147         //~^^ ERROR: function `bar` is private
148         foo();
149         bar();
150     }
151
152     fn test3() {
153         use bar::baz;
154         //~^ ERROR: module `baz` is private
155     }
156
157     fn test4() {
158         use bar::{foo, bar};
159         foo();
160         bar();
161     }
162
163     fn test5() {
164         use bar;
165         bar::foo();
166         bar::bar();
167     }
168
169     impl ::bar::B for f32 { fn foo() -> f32 { 1.0 } }
170     //~^ ERROR: trait `B` is private
171 }
172
173 pub mod mytest {
174     // Even though the inner `A` struct is a publicly exported item (usable from
175     // external crates through `foo::foo`, it should not be accessible through
176     // its definition path (which has the private `i` module).
177     use self::foo::i::A; //~ ERROR: type `A` is inaccessible
178                          //~^ NOTE: module `i` is private
179
180     pub mod foo {
181         pub use self::i::A as foo;
182
183         mod i {
184             pub struct A;
185         }
186     }
187 }
188
189 #[start] fn main(_: isize, _: *const *const u8) -> isize { 3 }