]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/privacy1.rs
Auto merge of #23934 - lfairy:write-no-deref, r=alexcrichton
[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="phantom_fn"]
15 pub trait PhantomFn<A:?Sized,R:?Sized=()> { }
16 impl<A:?Sized, R:?Sized, U:?Sized> PhantomFn<A,R> for U { }
17
18 #[lang="sized"]
19 pub trait Sized : PhantomFn<Self> {}
20
21 #[lang="copy"]
22 pub trait Copy : PhantomFn<Self> {}
23
24 mod bar {
25     // shouldn't bring in too much
26     pub use self::glob::*;
27
28     // can't publicly re-export private items
29     pub use self::baz::{foo, bar};
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         pub fn foo() {}
61         pub fn bar() {}
62     }
63
64     extern {
65         fn epriv();
66         pub fn epub();
67     }
68
69     fn test() {
70         self::Enum::Pub;
71         unsafe {
72             epriv();
73             epub();
74         }
75         self::baz::A;
76         self::baz::A::foo();
77         self::baz::A::bar(); //~ ERROR: method `bar` is private
78         self::baz::A.foo2();
79         self::baz::A.bar2(); //~ ERROR: method `bar2` is private
80
81         // this used to cause an ICE in privacy traversal.
82         super::gpub();
83     }
84
85     mod glob {
86         pub fn gpub() {}
87         fn gpriv() {}
88     }
89 }
90
91 pub fn gpub() {}
92
93 fn lol() {
94     bar::A;
95     bar::A::foo();
96     bar::A::bar(); //~ ERROR: method `bar` is private
97     bar::A.foo2();
98     bar::A.bar2(); //~ ERROR: method `bar2` is private
99 }
100
101 mod foo {
102     fn test() {
103         ::bar::A::foo();
104         ::bar::A::bar();        //~ ERROR: method `bar` is private
105         ::bar::A.foo2();
106         ::bar::A.bar2();        //~ ERROR: method `bar2` is private
107         ::bar::baz::A::foo();   //~ ERROR: method `foo` is inaccessible
108                                 //~^ NOTE: module `baz` is private
109         ::bar::baz::A::bar();   //~ ERROR: method `bar` is private
110         ::bar::baz::A.foo2();   //~ ERROR: struct `A` is inaccessible
111                                 //~^ NOTE: module `baz` is private
112         ::bar::baz::A.bar2();   //~ ERROR: struct `A` is inaccessible
113                                 //~^ ERROR: method `bar2` is private
114                                 //~^^ NOTE: module `baz` is private
115
116         let _: isize =
117         ::bar::B::foo();        //~ ERROR: method `foo` is inaccessible
118                                 //~^ NOTE: trait `B` is private
119         ::lol();
120
121         ::bar::Enum::Pub;
122
123         unsafe {
124             ::bar::epriv(); //~ ERROR: function `epriv` is private
125             ::bar::epub();
126         }
127
128         ::bar::foo();
129         ::bar::bar();
130
131         ::bar::gpub();
132
133         ::bar::baz::foo(); //~ ERROR: function `foo` is inaccessible
134                            //~^ NOTE: module `baz` is private
135         ::bar::baz::bar(); //~ ERROR: function `bar` is inaccessible
136     }
137
138     fn test2() {
139         use bar::baz::{foo, bar};
140         //~^ ERROR: function `foo` is inaccessible
141         //~^^ ERROR: function `bar` is inaccessible
142         foo();
143         bar();
144     }
145
146     fn test3() {
147         use bar::baz;
148         //~^ ERROR: module `baz` is private
149     }
150
151     fn test4() {
152         use bar::{foo, bar};
153         foo();
154         bar();
155     }
156
157     fn test5() {
158         use bar;
159         bar::foo();
160         bar::bar();
161     }
162
163     impl ::bar::B for f32 { fn foo() -> f32 { 1.0 } }
164     //~^ ERROR: trait `B` is private
165 }
166
167 pub mod mytest {
168     // Even though the inner `A` struct is a publicly exported item (usable from
169     // external crates through `foo::foo`, it should not be accessible through
170     // its definition path (which has the private `i` module).
171     use self::foo::i::A; //~ ERROR: type `A` is inaccessible
172                          //~^ NOTE: module `i` is private
173
174     pub mod foo {
175         pub use self::i::A as foo;
176
177         mod i {
178             pub struct A;
179         }
180     }
181 }
182
183 #[start] fn main(_: isize, _: *const *const u8) -> isize { 3 }