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