Skip to content

Commit 8361ee5

Browse files
authored
Update E0446.md
The existing error documentation did not show how to use a child module's functions if the types used in those functions are private. These are some other places this problem has popped up that did not present a solution (these are from before the solution existed, 2016-2017. The solution was released in the Rust 2018 edition. However these were the places I was pointed to when I encountered the problem myself): rust-lang#30905 https://stackoverflow.com/questions/39334430/how-to-reference-private-types-from-public-functions-in-private-modules/62374958#62374958
1 parent 4fb54ed commit 8361ee5

File tree

1 file changed

+24
-8
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+24
-8
lines changed

src/librustc_error_codes/error_codes/E0446.md

+24-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Erroneous code example:
44

55
```compile_fail,E0446
66
#![deny(private_in_public)]
7+
struct Bar(u32);
78
8-
mod Foo {
9-
struct Bar(u32);
10-
9+
mod foo {
10+
use crate::Bar;
1111
pub fn bar() -> Bar { // error: private type in public interface
1212
Bar(0)
1313
}
@@ -16,15 +16,31 @@ mod Foo {
1616
fn main() {}
1717
```
1818

19-
To solve this error, please ensure that the type is also public. The type
20-
can be made inaccessible if necessary by placing it into a private inner
21-
module, but it still has to be marked with `pub`.
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)
2222
Example:
2323

2424
```
25-
mod Foo {
26-
pub struct Bar(u32); // we set the Bar type public
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+
}
2733
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;
2844
pub fn bar() -> Bar { // ok!
2945
Bar(0)
3046
}

0 commit comments

Comments
 (0)