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