Skip to content

Commit 8e52875

Browse files
committed
Auto merge of #28312 - GuillaumeGomez:privacy, r=Manishearth
r? @Manishearth
2 parents f6d6421 + 0477976 commit 8e52875

File tree

2 files changed

+252
-11
lines changed

2 files changed

+252
-11
lines changed

src/librustc_privacy/diagnostics.rs

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
}

src/librustc_privacy/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ use rustc::front::map as ast_map;
5050
use syntax::ast;
5151
use syntax::codemap::Span;
5252

53+
pub mod diagnostics;
54+
5355
type Context<'a, 'tcx> = (&'a ty::MethodMap<'tcx>, &'a def::ExportMap);
5456

5557
/// Result of a checking operation - None => no errors were found. Some => an
@@ -715,7 +717,8 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
715717
UnnamedField(idx) => format!("field #{} of {} is private",
716718
idx + 1, struct_desc),
717719
};
718-
self.tcx.sess.span_err(span, &msg[..]);
720+
span_err!(self.tcx.sess, span, E0451,
721+
"{}", &msg[..]);
719722
}
720723

721724
// Given the ID of a method, checks to ensure it's in scope.
@@ -929,9 +932,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
929932
});
930933

931934
if any_priv {
932-
self.tcx.sess.span_err(expr.span,
933-
"cannot invoke tuple struct constructor \
934-
with private fields");
935+
span_err!(self.tcx.sess, expr.span, E0450,
936+
"cannot invoke tuple struct constructor with private \
937+
fields");
935938
}
936939
}
937940
}
@@ -1043,7 +1046,8 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
10431046
let tcx = self.tcx;
10441047
let check_inherited = |sp: Span, vis: hir::Visibility, note: &str| {
10451048
if vis != hir::Inherited {
1046-
tcx.sess.span_err(sp, "unnecessary visibility qualifier");
1049+
span_err!(tcx.sess, sp, E0449,
1050+
"unnecessary visibility qualifier");
10471051
if !note.is_empty() {
10481052
tcx.sess.span_note(sp, note);
10491053
}
@@ -1076,8 +1080,8 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
10761080
match v.node.vis {
10771081
hir::Public => {
10781082
if item.vis == hir::Public {
1079-
tcx.sess.span_err(v.span, "unnecessary `pub` \
1080-
visibility");
1083+
span_err!(tcx.sess, v.span, E0448,
1084+
"unnecessary `pub` visibility");
10811085
}
10821086
}
10831087
hir::Inherited => {}
@@ -1098,7 +1102,8 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
10981102
let tcx = self.tcx;
10991103
fn check_inherited(tcx: &ty::ctxt, sp: Span, vis: hir::Visibility) {
11001104
if vis != hir::Inherited {
1101-
tcx.sess.span_err(sp, "visibility has no effect inside functions");
1105+
span_err!(tcx.sess, sp, E0447,
1106+
"visibility has no effect inside functions");
11021107
}
11031108
}
11041109
let check_struct = |def: &hir::StructDef| {
@@ -1193,8 +1198,8 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
11931198
if !self.tcx.sess.features.borrow().visible_private_types &&
11941199
self.path_is_private_type(trait_ref.trait_ref.ref_id) {
11951200
let span = trait_ref.trait_ref.path.span;
1196-
self.tcx.sess.span_err(span, "private trait in exported type \
1197-
parameter bound");
1201+
span_err!(self.tcx.sess, span, E0445,
1202+
"private trait in exported type parameter bound");
11981203
}
11991204
}
12001205
}
@@ -1435,7 +1440,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14351440
if let hir::TyPath(_, ref p) = t.node {
14361441
if !self.tcx.sess.features.borrow().visible_private_types &&
14371442
self.path_is_private_type(t.id) {
1438-
self.tcx.sess.span_err(p.span, "private type in exported type signature");
1443+
span_err!(self.tcx.sess, p.span, E0446,
1444+
"private type in exported type signature");
14391445
}
14401446
}
14411447
visit::walk_ty(self, t)

0 commit comments

Comments
 (0)