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