]> git.lizzy.rs Git - rust.git/blob - src/librustc_privacy/diagnostics.rs
Rollup merge of #31703 - WiSaGaN:bugfix/fix-link-in-release-notes, r=alexcrichton
[rust.git] / src / librustc_privacy / diagnostics.rs
1 // Copyright 2014 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 #![allow(non_snake_case)]
12
13 register_long_diagnostics! {
14
15 E0445: r##"
16 A private trait was used on a public type parameter bound. Erroneous code
17 examples:
18
19 ```compile_fail
20 trait Foo {
21     fn dummy(&self) { }
22 }
23
24 pub trait Bar : Foo {} // error: private trait in public interface
25 pub struct Bar2<T: Foo>(pub T); // same error
26 pub fn foo<T: Foo> (t: T) {} // same error
27 ```
28
29 To solve this error, please ensure that the trait is also public. The trait
30 can be made inaccessible if necessary by placing it into a private inner
31 module, but it still has to be marked with `pub`. Example:
32
33 ```ignore
34 pub trait Foo { // we set the Foo trait public
35     fn dummy(&self) { }
36 }
37
38 pub trait Bar : Foo {} // ok!
39 pub struct Bar2<T: Foo>(pub T); // ok!
40 pub fn foo<T: Foo> (t: T) {} // ok!
41 ```
42 "##,
43
44 E0446: r##"
45 A private type was used in a public type signature. Erroneous code example:
46
47 ```compile_fail
48 mod Foo {
49     struct Bar(u32);
50
51     pub fn bar() -> Bar { // error: private type in public interface
52         Bar(0)
53     }
54 }
55 ```
56
57 To solve this error, please ensure that the type is also public. The type
58 can be made inaccessible if necessary by placing it into a private inner
59 module, but it still has to be marked with `pub`.
60 Example:
61
62 ```
63 mod Foo {
64     pub struct Bar(u32); // we set the Bar type public
65
66     pub fn bar() -> Bar { // ok!
67         Bar(0)
68     }
69 }
70 ```
71 "##,
72
73 E0447: r##"
74 The `pub` keyword was used inside a function. Erroneous code example:
75
76 ```compile_fail
77 fn foo() {
78     pub struct Bar; // error: visibility has no effect inside functions
79 }
80 ```
81
82 Since we cannot access items defined inside a function, the visibility of its
83 items does not impact outer code. So using the `pub` keyword in this context
84 is invalid.
85 "##,
86
87 E0448: r##"
88 The `pub` keyword was used inside a public enum. Erroneous code example:
89
90 ```compile_fail
91 pub enum Foo {
92     pub Bar, // error: unnecessary `pub` visibility
93 }
94 ```
95
96 Since the enum is already public, adding `pub` on one its elements is
97 unnecessary. Example:
98
99 ```compile_fail
100 enum Foo {
101     pub Bar, // not ok!
102 }
103 ```
104
105 This is the correct syntax:
106
107 ```ignore
108 pub enum Foo {
109     Bar, // ok!
110 }
111 ```
112 "##,
113
114 E0449: r##"
115 A visibility qualifier was used when it was unnecessary. Erroneous code
116 examples:
117
118 ```compile_fail
119 struct Bar;
120
121 trait Foo {
122     fn foo();
123 }
124
125 pub impl Bar {} // error: unnecessary visibility qualifier
126
127 pub impl Foo for Bar { // error: unnecessary visibility qualifier
128     pub fn foo() {} // error: unnecessary visibility qualifier
129 }
130 ```
131
132 To fix this error, please remove the visibility qualifier when it is not
133 required. Example:
134
135 ```ignore
136 struct Bar;
137
138 trait Foo {
139     fn foo();
140 }
141
142 // Directly implemented methods share the visibility of the type itself,
143 // so `pub` is unnecessary here
144 impl Bar {}
145
146 // Trait methods share the visibility of the trait, so `pub` is
147 // unnecessary in either case
148 pub impl Foo for Bar {
149     pub fn foo() {}
150 }
151 ```
152 "##,
153
154 E0450: r##"
155 A tuple constructor was invoked while some of its fields are private. Erroneous
156 code example:
157
158 ```compile_fail
159 mod Bar {
160     pub struct Foo(isize);
161 }
162
163 let f = Bar::Foo(0); // error: cannot invoke tuple struct constructor with
164                      //        private fields
165 ```
166
167 To solve this issue, please ensure that all of the fields of the tuple struct
168 are public. Alternatively, provide a `new()` method to the tuple struct to
169 construct it from a given inner value. Example:
170
171 ```
172 mod Bar {
173     pub struct Foo(pub isize); // we set its field to public
174 }
175
176 let f = Bar::Foo(0); // ok!
177
178 // or:
179 mod bar {
180     pub struct Foo(isize);
181
182     impl Foo {
183         pub fn new(x: isize) -> Foo {
184             Foo(x)
185         }
186     }
187 }
188
189 let f = bar::Foo::new(1);
190 ```
191 "##,
192
193 E0451: r##"
194 A struct constructor with private fields was invoked. Erroneous code example:
195
196 ```compile_fail
197 mod Bar {
198     pub struct Foo {
199         pub a: isize,
200         b: isize,
201     }
202 }
203
204 let f = Bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `Bar::Foo`
205                                 //        is private
206 ```
207
208 To fix this error, please ensure that all the fields of the struct, or
209 implement a function for easy instantiation. Examples:
210
211 ```
212 mod Bar {
213     pub struct Foo {
214         pub a: isize,
215         pub b: isize, // we set `b` field public
216     }
217 }
218
219 let f = Bar::Foo{ a: 0, b: 0 }; // ok!
220 ```
221
222 Or:
223
224 ```
225 mod Bar {
226     pub struct Foo {
227         pub a: isize,
228         b: isize, // still private
229     }
230
231     impl Foo {
232         pub fn new() -> Foo { // we create a method to instantiate `Foo`
233             Foo { a: 0, b: 0 }
234         }
235     }
236 }
237
238 let f = Bar::Foo::new(); // ok!
239 ```
240 "##,
241
242 }