@@ -22,6 +22,8 @@ trait Foo {
22
22
}
23
23
24
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
25
27
```
26
28
27
29
To solve this error, please ensure that the trait is also public and accessible
@@ -34,6 +36,8 @@ pub trait Foo { // we set the Foo trait public
34
36
}
35
37
36
38
pub trait Bar : Foo {} // ok!
39
+ pub struct Bar<T: Foo>(pub T); // ok!
40
+ pub fn foo<T: Foo> (t: T) {} // ok!
37
41
```
38
42
"## ,
39
43
@@ -73,8 +77,8 @@ fn foo() {
73
77
}
74
78
```
75
79
76
- Since we cannot access inside function's elements , the visibility of its
77
- elements does not impact outer code. So using the `pub` keyword in this context
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
78
82
is invalid.
79
83
"## ,
80
84
@@ -122,7 +126,25 @@ pub impl Foo for Bar { // error: unnecessary visibility qualifier
122
126
```
123
127
124
128
To fix this error, please remove the visibility qualifier when it is not
125
- required.
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
+ ```
126
148
"## ,
127
149
128
150
E0450 : r##"
@@ -138,15 +160,76 @@ let f = Bar::Foo(0); // error: cannot invoke tuple struct constructor with
138
160
// private fields
139
161
```
140
162
141
- To solve this issue, please ensure that all tuple's fields are public. Example:
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:
142
166
143
167
```
144
168
mod Bar {
145
169
pub struct Foo(pub isize); // we set its field to public
146
170
}
147
171
148
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!
149
232
```
150
233
"## ,
151
234
152
- }
235
+ }
0 commit comments