Skip to content

Commit

Permalink
Suggest proper vis type in the help message
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 2, 2023
1 parent 82b6cb9 commit bb3e619
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_privacy/messages.ftl
Expand Up @@ -8,7 +8,7 @@ privacy_from_private_dep_in_public_interface =
privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interface
.label = can't leak {$vis_descr} {$kind}
.visibility_label = `{$descr}` declared as {$vis_descr}
.suggestion = consider adding `pub` in front of it
.suggestion = consider adding `{$vis_sugg}` in front of it
privacy_item_is_private = {$kind} `{$descr}` is private
.label = private {$kind}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_privacy/src/errors.rs
Expand Up @@ -55,6 +55,7 @@ pub struct InPublicInterface<'a> {
pub span: Span,
pub vis_descr: &'static str,
pub kind: &'a str,
pub vis_sugg: &'static str,
pub descr: DiagnosticArgFromDisplay<'a>,
#[label(privacy_visibility_label)]
pub vis_span: Span,
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_privacy/src/lib.rs
Expand Up @@ -1475,12 +1475,31 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
}
};

// FIXME: this code was adapted from the above `vis_descr` computation,
// but it's not clear if it's correct.
let vis_sugg = match self.required_visibility {
ty::Visibility::Public => "pub",
ty::Visibility::Restricted(vis_def_id) => {
if vis_def_id
== self.tcx.parent_module_from_def_id(local_def_id).to_local_def_id()
{
"???FIXME???"
} else if vis_def_id.is_top_level_module() {
"pub(crate)"
} else {
"???FIXME???"
}
}
};

self.tcx.sess.emit_err(InPublicInterface {
span,
vis_descr,
kind,
vis_sugg,
descr: descr.into(),
vis_span,
suggestion: vis_span,
});
return false;
}
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/error-codes/E0446.stderr
Expand Up @@ -2,7 +2,10 @@ error[E0446]: private type `Bar` in public interface
--> $DIR/E0446.rs:10:5
|
LL | struct Bar;
| ---------- `Bar` declared as private
| ----------
| |
| `Bar` declared as private
| help: consider adding `pub` in front of it
...
LL | type Alias1 = Bar;
| ^^^^^^^^^^^ can't leak private type
Expand All @@ -11,7 +14,10 @@ error[E0446]: private trait `PrivTr` in public interface
--> $DIR/E0446.rs:11:5
|
LL | trait PrivTr {}
| ------------ `PrivTr` declared as private
| ------------
| |
| `PrivTr` declared as private
| help: consider adding `pub` in front of it
...
LL | type Alias2 = Box<dyn PrivTr>;
| ^^^^^^^^^^^ can't leak private trait
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/privacy/issue-30079.stderr
Expand Up @@ -18,7 +18,7 @@ LL | struct Priv;
| -----------
| |
| `m2::Priv` declared as private
| help: consider adding `pub` in front of it
| help: consider adding `pub(crate)` in front of it
LL | impl ::std::ops::Deref for ::SemiPriv {
LL | type Target = Priv;
| ^^^^^^^^^^^ can't leak private type
Expand All @@ -30,7 +30,7 @@ LL | struct Priv;
| -----------
| |
| `m3::Priv` declared as private
| help: consider adding `pub` in front of it
| help: consider adding `pub(crate)` in front of it
LL | impl ::SemiPrivTrait for () {
LL | type Assoc = Priv;
| ^^^^^^^^^^ can't leak private type
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/privacy/private-in-public-assoc-ty.stderr
Expand Up @@ -75,7 +75,10 @@ error[E0446]: private trait `PrivTr` in public interface
--> $DIR/private-in-public-assoc-ty.rs:41:9
|
LL | trait PrivTr {}
| ------------ `PrivTr` declared as private
| ------------
| |
| `PrivTr` declared as private
| help: consider adding `pub` in front of it
...
LL | type Exist = impl PrivTr;
| ^^^^^^^^^^ can't leak private trait
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/privacy/where-priv-type.stderr
Expand Up @@ -75,7 +75,10 @@ LL | type AssocTy = Const<{ my_const_fn(U) }>;
| ^^^^^^^^^^^^ can't leak private type
...
LL | const fn my_const_fn(val: u8) -> u8 {
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private
| -----------------------------------
| |
| `fn(u8) -> u8 {my_const_fn}` declared as private
| help: consider adding `pub` in front of it

error: aborting due to previous error; 5 warnings emitted

Expand Down

0 comments on commit bb3e619

Please sign in to comment.