Skip to content

Commit 42e1622

Browse files
Add new error code for visibility inside a function
1 parent c4a3936 commit 42e1622

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/librustc_privacy/diagnostics.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
register_long_diagnostics! {
1414

1515
E0445: r##"
16-
A private trait was used on a "public" type. Erroneous code example:
16+
A private trait was used on a public type parameter bound. Erroneous code
17+
examples:
1718
1819
```
1920
trait Foo {
@@ -23,8 +24,9 @@ trait Foo {
2324
pub trait Bar : Foo {} // error: private trait in exported type parameter bound
2425
```
2526
26-
To solve this error, please ensure the trait is accessible at the same level of
27-
the type(s) on which it's implemented. Example:
27+
To solve this error, please ensure that the trait is also public and accessible
28+
at the same level of the public functions or types which are bound on it.
29+
Example:
2830
2931
```
3032
pub trait Foo { // we set the Foo trait public
@@ -48,8 +50,8 @@ mod Foo {
4850
}
4951
```
5052
51-
To solve this error, please ensure the type is accessible at the same level of
52-
the exported type signature. Example:
53+
To solve this error, please ensure that the type is also public and accessible
54+
at the same level of the public functions or types which use it. Example:
5355
5456
```
5557
mod Foo {
@@ -62,4 +64,18 @@ mod Foo {
6264
```
6365
"##,
6466

67+
E0447: r##"
68+
The `pub` keyword was used inside a function. Erroneous code example:
69+
70+
```
71+
fn foo() {
72+
pub struct Bar; // error: visibility has no effect inside functions
73+
}
74+
```
75+
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
78+
is invalid.
79+
"##,
80+
6581
}

src/librustc_privacy/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,8 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
10981098
let tcx = self.tcx;
10991099
fn check_inherited(tcx: &ty::ctxt, sp: Span, vis: hir::Visibility) {
11001100
if vis != hir::Inherited {
1101-
tcx.sess.span_err(sp, "visibility has no effect inside functions");
1101+
span_err!(tcx.sess, sp, E0447,
1102+
"visibility has no effect inside functions");
11021103
}
11031104
}
11041105
let check_struct = |def: &hir::StructDef| {

0 commit comments

Comments
 (0)