]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0446.md
Rollup merge of #107190 - fmease:fix-81698, r=compiler-errors
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0446.md
1 A private type was used in a public type signature.
2
3 Erroneous code example:
4
5 ```compile_fail,E0446
6 #![deny(private_in_public)]
7 struct Bar(u32);
8
9 mod foo {
10     use crate::Bar;
11     pub fn bar() -> Bar { // error: private type in public interface
12         Bar(0)
13     }
14 }
15
16 fn main() {}
17 ```
18
19 There are two ways to solve this error. The first is to make the public type
20 signature only public to a module that also has access to the private type.
21 This is done by using pub(crate) or pub(in crate::my_mod::etc)
22 Example:
23
24 ```
25 struct Bar(u32);
26
27 mod foo {
28     use crate::Bar;
29     pub(crate) fn bar() -> Bar { // only public to crate root
30         Bar(0)
31     }
32 }
33
34 fn main() {}
35 ```
36
37 The other way to solve this error is to make the private type public.
38 Example:
39
40 ```
41 pub struct Bar(u32); // we set the Bar type public
42 mod foo {
43     use crate::Bar;
44     pub fn bar() -> Bar { // ok!
45         Bar(0)
46     }
47 }
48
49 fn main() {}
50 ```