Skip to content

Commit c337e68

Browse files
committed
Suggest proper vis type in the help message
1 parent 601d3b0 commit c337e68

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

compiler/rustc_privacy/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ privacy_from_private_dep_in_public_interface =
88
privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interface
99
.label = can't leak {$vis_descr} {$kind}
1010
.visibility_label = `{$descr}` declared as {$vis_descr}
11-
.suggestion = consider adding `pub` in front of it
11+
.suggestion = consider adding `{$vis_sugg}` in front of it
1212
1313
privacy_item_is_private = {$kind} `{$descr}` is private
1414
.label = private {$kind}

compiler/rustc_privacy/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct InPublicInterface<'a> {
5656
pub span: Span,
5757
pub vis_descr: &'static str,
5858
pub kind: &'a str,
59+
pub vis_sugg: &'static str,
5960
pub descr: DiagArgFromDisplay<'a>,
6061
#[label(privacy_visibility_label)]
6162
pub vis_span: Span,

compiler/rustc_privacy/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,12 +1337,31 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
13371337
}
13381338
};
13391339

1340+
// FIXME: this code was adapted from the above `vis_descr` computation,
1341+
// but it's not clear if it's correct.
1342+
let vis_sugg = match self.required_visibility {
1343+
ty::Visibility::Public => "pub",
1344+
ty::Visibility::Restricted(vis_def_id) => {
1345+
if vis_def_id
1346+
== self.tcx.parent_module_from_def_id(local_def_id).to_local_def_id()
1347+
{
1348+
"???FIXME???"
1349+
} else if vis_def_id.is_top_level_module() {
1350+
"pub(crate)"
1351+
} else {
1352+
"???FIXME???"
1353+
}
1354+
}
1355+
};
1356+
13401357
self.tcx.dcx().emit_err(InPublicInterface {
13411358
span,
13421359
vis_descr,
13431360
kind,
1361+
vis_sugg,
13441362
descr: descr.into(),
13451363
vis_span,
1364+
suggestion: vis_span,
13461365
});
13471366
return false;
13481367
}

tests/ui/error-codes/E0446.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0446]: private type `Bar` in public interface
22
--> $DIR/E0446.rs:10:5
33
|
44
LL | struct Bar;
5-
| ---------- `Bar` declared as private
5+
| ----------
6+
| |
7+
| `Bar` declared as private
8+
| help: consider adding `pub` in front of it
69
...
710
LL | type Alias1 = Bar;
811
| ^^^^^^^^^^^ can't leak private type
@@ -11,7 +14,10 @@ error[E0446]: private trait `PrivTr` in public interface
1114
--> $DIR/E0446.rs:11:5
1215
|
1316
LL | trait PrivTr {}
14-
| ------------ `PrivTr` declared as private
17+
| ------------
18+
| |
19+
| `PrivTr` declared as private
20+
| help: consider adding `pub` in front of it
1521
...
1622
LL | type Alias2 = Box<dyn PrivTr>;
1723
| ^^^^^^^^^^^ can't leak private trait

tests/ui/privacy/issue-30079.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | struct Priv;
1818
| -----------
1919
| |
2020
| `m2::Priv` declared as private
21-
| help: consider adding `pub` in front of it
21+
| help: consider adding `pub(crate)` in front of it
2222
LL | impl ::std::ops::Deref for ::SemiPriv {
2323
LL | type Target = Priv;
2424
| ^^^^^^^^^^^ can't leak private type
@@ -30,7 +30,7 @@ LL | struct Priv;
3030
| -----------
3131
| |
3232
| `m3::Priv` declared as private
33-
| help: consider adding `pub` in front of it
33+
| help: consider adding `pub(crate)` in front of it
3434
LL | impl ::SemiPrivTrait for () {
3535
LL | type Assoc = Priv;
3636
| ^^^^^^^^^^ can't leak private type

tests/ui/privacy/private-in-public-assoc-ty.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ error[E0446]: private trait `PrivTr` in public interface
7575
--> $DIR/private-in-public-assoc-ty.rs:41:9
7676
|
7777
LL | trait PrivTr {}
78-
| ------------ `PrivTr` declared as private
78+
| ------------
79+
| |
80+
| `PrivTr` declared as private
81+
| help: consider adding `pub` in front of it
7982
...
8083
LL | type Exist = impl PrivTr;
8184
| ^^^^^^^^^^ can't leak private trait

tests/ui/privacy/where-priv-type.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ LL | type AssocTy = Const<{ my_const_fn(U) }>;
7575
| ^^^^^^^^^^^^ can't leak private type
7676
...
7777
LL | const fn my_const_fn(val: u8) -> u8 {
78-
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
78+
| -----------------------------------
79+
| |
80+
| `fn(u8) -> u8 {my_const_fn}` declared as private
81+
| help: consider adding `pub` in front of it
7982

8083
error: aborting due to 1 previous error; 5 warnings emitted
8184

0 commit comments

Comments
 (0)