Skip to content

Commit 0477976

Browse files
Add error code for private struct field issue
1 parent a056d58 commit 0477976

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

src/librustc_privacy/diagnostics.rs

+88-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ trait Foo {
2222
}
2323
2424
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
2527
```
2628
2729
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
3436
}
3537
3638
pub trait Bar : Foo {} // ok!
39+
pub struct Bar<T: Foo>(pub T); // ok!
40+
pub fn foo<T: Foo> (t: T) {} // ok!
3741
```
3842
"##,
3943

@@ -73,8 +77,8 @@ fn foo() {
7377
}
7478
```
7579
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
7882
is invalid.
7983
"##,
8084

@@ -122,7 +126,25 @@ pub impl Foo for Bar { // error: unnecessary visibility qualifier
122126
```
123127
124128
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+
```
126148
"##,
127149

128150
E0450: r##"
@@ -138,15 +160,76 @@ let f = Bar::Foo(0); // error: cannot invoke tuple struct constructor with
138160
// private fields
139161
```
140162
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:
142166
143167
```
144168
mod Bar {
145169
pub struct Foo(pub isize); // we set its field to public
146170
}
147171
148172
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!
149232
```
150233
"##,
151234

152-
}
235+
}

src/librustc_privacy/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
717717
UnnamedField(idx) => format!("field #{} of {} is private",
718718
idx + 1, struct_desc),
719719
};
720-
self.tcx.sess.span_err(span, &msg[..]);
720+
span_err!(self.tcx.sess, span, E0451,
721+
"{}", &msg[..]);
721722
}
722723

723724
// Given the ID of a method, checks to ensure it's in scope.

0 commit comments

Comments
 (0)