]> git.lizzy.rs Git - rust.git/blob - src/librustc_privacy/diagnostics.rs
Auto merge of #38932 - petrochenkov:privctor, r=jseyfried
[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 ```compile_fail,E0445
20 #![deny(private_in_public)]
21
22 trait Foo {
23     fn dummy(&self) { }
24 }
25
26 pub trait Bar : Foo {} // error: private trait in public interface
27 pub struct Bar2<T: Foo>(pub T); // same error
28 pub fn foo<T: Foo> (t: T) {} // same error
29 ```
30
31 To solve this error, please ensure that the trait is also public. The trait
32 can be made inaccessible if necessary by placing it into a private inner
33 module, but it still has to be marked with `pub`. Example:
34
35 ```ignore
36 pub trait Foo { // we set the Foo trait public
37     fn dummy(&self) { }
38 }
39
40 pub trait Bar : Foo {} // ok!
41 pub struct Bar2<T: Foo>(pub T); // ok!
42 pub fn foo<T: Foo> (t: T) {} // ok!
43 ```
44 "##,
45
46 E0446: r##"
47 A private type was used in a public type signature. Erroneous code example:
48
49 ```compile_fail,E0446
50 #![deny(private_in_public)]
51
52 mod Foo {
53     struct Bar(u32);
54
55     pub fn bar() -> Bar { // error: private type in public interface
56         Bar(0)
57     }
58 }
59 ```
60
61 To solve this error, please ensure that the type is also public. The type
62 can be made inaccessible if necessary by placing it into a private inner
63 module, but it still has to be marked with `pub`.
64 Example:
65
66 ```
67 mod Foo {
68     pub struct Bar(u32); // we set the Bar type public
69
70     pub fn bar() -> Bar { // ok!
71         Bar(0)
72     }
73 }
74 ```
75 "##,
76
77 E0447: r##"
78 The `pub` keyword was used inside a function. Erroneous code example:
79
80 ```ignore
81 fn foo() {
82     pub struct Bar; // error: visibility has no effect inside functions
83 }
84 ```
85
86 Since we cannot access items defined inside a function, the visibility of its
87 items does not impact outer code. So using the `pub` keyword in this context
88 is invalid.
89 "##,
90
91 E0448: r##"
92 The `pub` keyword was used inside a public enum. Erroneous code example:
93
94 ```compile_fail
95 pub enum Foo {
96     pub Bar, // error: unnecessary `pub` visibility
97 }
98 ```
99
100 Since the enum is already public, adding `pub` on one its elements is
101 unnecessary. Example:
102
103 ```compile_fail,
104 enum Foo {
105     pub Bar, // not ok!
106 }
107 ```
108
109 This is the correct syntax:
110
111 ```ignore
112 pub enum Foo {
113     Bar, // ok!
114 }
115 ```
116 "##,
117
118 E0451: r##"
119 A struct constructor with private fields was invoked. Erroneous code example:
120
121 ```compile_fail,E0451
122 mod Bar {
123     pub struct Foo {
124         pub a: isize,
125         b: isize,
126     }
127 }
128
129 let f = Bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `Bar::Foo`
130                                 //        is private
131 ```
132
133 To fix this error, please ensure that all the fields of the struct are public,
134 or implement a function for easy instantiation. Examples:
135
136 ```
137 mod Bar {
138     pub struct Foo {
139         pub a: isize,
140         pub b: isize, // we set `b` field public
141     }
142 }
143
144 let f = Bar::Foo{ a: 0, b: 0 }; // ok!
145 ```
146
147 Or:
148
149 ```
150 mod Bar {
151     pub struct Foo {
152         pub a: isize,
153         b: isize, // still private
154     }
155
156     impl Foo {
157         pub fn new() -> Foo { // we create a method to instantiate `Foo`
158             Foo { a: 0, b: 0 }
159         }
160     }
161 }
162
163 let f = Bar::Foo::new(); // ok!
164 ```
165 "##,
166
167 }
168
169 register_diagnostics! {
170 //  E0450, moved into resolve
171 }