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