Skip to content

Commit 244dd83

Browse files
committed
vis note for no pub reexports glob import
1 parent 1670ff6 commit 244dd83

File tree

10 files changed

+97
-21
lines changed

10 files changed

+97
-21
lines changed

compiler/rustc_lint/src/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,10 @@ pub trait LintContext {
926926
if elided { "'static " } else { "'static" },
927927
Applicability::MachineApplicable
928928
);
929+
},
930+
BuiltinLintDiagnostics::InsufficientVisibility { max_vis, span } => {
931+
let msg = format!("the most public imported item is `{max_vis}`");
932+
db.span_note(span, msg);
929933
}
930934
}
931935
// Rewrap `db`, and pass control to the user.

compiler/rustc_lint_defs/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@ pub enum BuiltinLintDiagnostics {
583583
elided: bool,
584584
span: Span,
585585
},
586+
InsufficientVisibility {
587+
span: Span,
588+
max_vis: String,
589+
},
586590
}
587591

588592
/// Lints that are buffered up early on in the `Session` before the

compiler/rustc_middle/src/ty/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@ pub enum Visibility<Id = LocalDefId> {
296296
Restricted(Id),
297297
}
298298

299+
impl Visibility {
300+
pub fn to_string<'tcx>(self, def_id: LocalDefId, tcx: TyCtxt<'tcx>) -> String {
301+
match self {
302+
ty::Visibility::Restricted(restricted_id) => {
303+
if restricted_id.is_top_level_module() {
304+
"pub(crate)".to_string()
305+
} else if restricted_id == tcx.parent_module_from_def_id(def_id).to_local_def_id() {
306+
"pub(self)".to_string()
307+
} else {
308+
format!("pub({})", tcx.item_name(restricted_id.to_def_id()))
309+
}
310+
}
311+
ty::Visibility::Public => "pub".to_string(),
312+
}
313+
}
314+
}
315+
299316
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
300317
pub enum BoundConstness {
301318
/// `T: Trait`

compiler/rustc_privacy/src/lib.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -888,29 +888,14 @@ pub struct TestReachabilityVisitor<'tcx, 'a> {
888888
effective_visibilities: &'a EffectiveVisibilities,
889889
}
890890

891-
fn vis_to_string<'tcx>(def_id: LocalDefId, vis: ty::Visibility, tcx: TyCtxt<'tcx>) -> String {
892-
match vis {
893-
ty::Visibility::Restricted(restricted_id) => {
894-
if restricted_id.is_top_level_module() {
895-
"pub(crate)".to_string()
896-
} else if restricted_id == tcx.parent_module_from_def_id(def_id).to_local_def_id() {
897-
"pub(self)".to_string()
898-
} else {
899-
format!("pub({})", tcx.item_name(restricted_id.to_def_id()))
900-
}
901-
}
902-
ty::Visibility::Public => "pub".to_string(),
903-
}
904-
}
905-
906891
impl<'tcx, 'a> TestReachabilityVisitor<'tcx, 'a> {
907892
fn effective_visibility_diagnostic(&mut self, def_id: LocalDefId) {
908893
if self.tcx.has_attr(def_id, sym::rustc_effective_visibility) {
909894
let mut error_msg = String::new();
910895
let span = self.tcx.def_span(def_id.to_def_id());
911896
if let Some(effective_vis) = self.effective_visibilities.effective_vis(def_id) {
912897
for level in Level::all_levels() {
913-
let vis_str = vis_to_string(def_id, *effective_vis.at_level(level), self.tcx);
898+
let vis_str = (*effective_vis.at_level(level)).to_string(def_id, self.tcx);
914899
if level != Level::Direct {
915900
error_msg.push_str(", ");
916901
}
@@ -1506,11 +1491,11 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
15061491
tcx: self.tcx,
15071492
})
15081493
.into(),
1509-
item_vis_descr: &vis_to_string(self.item_def_id, reachable_at_vis, self.tcx),
1494+
item_vis_descr: &reachable_at_vis.to_string(self.item_def_id, self.tcx),
15101495
ty_span: vis_span,
15111496
ty_kind: kind,
15121497
ty_descr: descr.into(),
1513-
ty_vis_descr: &vis_to_string(local_def_id, vis, self.tcx),
1498+
ty_vis_descr: &vis.to_string(local_def_id, self.tcx),
15141499
},
15151500
);
15161501
}
@@ -1589,8 +1574,8 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx, '_> {
15891574
span,
15901575
kind: self.tcx.def_descr(def_id.to_def_id()),
15911576
descr: (&LazyDefPathStr { def_id: def_id.to_def_id(), tcx: self.tcx }).into(),
1592-
reachable_vis: &vis_to_string(def_id, *reachable_at_vis, self.tcx),
1593-
reexported_vis: &vis_to_string(def_id, *reexported_at_vis, self.tcx),
1577+
reachable_vis: &*reachable_at_vis.to_string(def_id, self.tcx),
1578+
reexported_vis: &*reexported_at_vis.to_string(def_id, self.tcx),
15941579
},
15951580
);
15961581
}

compiler/rustc_resolve/src/imports.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
989989
&& let Some(max_vis) = max_vis.get()
990990
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
991991
{
992-
self.lint_buffer.buffer_lint(
992+
self.lint_buffer.buffer_lint_with_diagnostic(
993993
UNUSED_IMPORTS,
994994
id,
995995
import.span,
996996
fluent::resolve_glob_import_doesnt_reexport,
997+
BuiltinLintDiagnostics::InsufficientVisibility {
998+
max_vis: max_vis.to_string(self.local_def_id(id), self.tcx),
999+
span: import.span,
1000+
},
9971001
);
9981002
}
9991003
return None;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// https://github.com/rust-lang/rust/issues/115966
3+
4+
mod m {
5+
pub(crate) type A = u8;
6+
}
7+
8+
#[warn(unused_imports)] //~ NOTE: the lint level is defined here
9+
pub use m::*;
10+
//~^ WARNING: glob import doesn't reexport anything because no candidate is public enough
11+
//~| NOTE: the most public imported item is `pub(crate)`
12+
13+
fn main() {
14+
let _: A;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: glob import doesn't reexport anything because no candidate is public enough
2+
--> $DIR/no-pub-reexports-but-used.rs:9:9
3+
|
4+
LL | pub use m::*;
5+
| ^^^^
6+
|
7+
note: the most public imported item is `pub(crate)`
8+
--> $DIR/no-pub-reexports-but-used.rs:9:9
9+
|
10+
LL | pub use m::*;
11+
| ^^^^
12+
note: the lint level is defined here
13+
--> $DIR/no-pub-reexports-but-used.rs:8:8
14+
|
15+
LL | #[warn(unused_imports)]
16+
| ^^^^^^^^^^^^^^
17+
18+
warning: 1 warning emitted
19+

tests/ui/imports/reexports.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ LL | #![warn(unused_imports)]
5959
warning: glob import doesn't reexport anything because no candidate is public enough
6060
--> $DIR/reexports.rs:11:17
6161
|
62+
LL | pub use super::*;
63+
| ^^^^^^^^
64+
|
65+
note: the most public imported item is `pub(a)`
66+
--> $DIR/reexports.rs:11:17
67+
|
6268
LL | pub use super::*;
6369
| ^^^^^^^^
6470

tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ error: glob import doesn't reexport anything because no candidate is public enou
2828
LL | pub use self::Professor::*;
2929
| ^^^^^^^^^^^^^^^^^^
3030
|
31+
note: the most public imported item is `pub(self)`
32+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
33+
|
34+
LL | pub use self::Professor::*;
35+
| ^^^^^^^^^^^^^^^^^^
3136
note: the lint level is defined here
3237
--> $DIR/issue-46209-private-enum-variant-reexport.rs:1:8
3338
|
@@ -49,6 +54,12 @@ LL | pub use self::Lieutenant::{JuniorGrade, Full};
4954
error: glob import doesn't reexport anything because no candidate is public enough
5055
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
5156
|
57+
LL | pub use self::PettyOfficer::*;
58+
| ^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
note: the most public imported item is `pub(self)`
61+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
62+
|
5263
LL | pub use self::PettyOfficer::*;
5364
| ^^^^^^^^^^^^^^^^^^^^^
5465

@@ -61,6 +72,12 @@ LL | pub use self::PettyOfficer::*;
6172
error: glob import doesn't reexport anything because no candidate is public enough
6273
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
6374
|
75+
LL | pub use self::Crewman::*;
76+
| ^^^^^^^^^^^^^^^^
77+
|
78+
note: the most public imported item is `pub(crate)`
79+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
80+
|
6481
LL | pub use self::Crewman::*;
6582
| ^^^^^^^^^^^^^^^^
6683

tests/ui/privacy/private-variant-reexport.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ error: glob import doesn't reexport anything because no candidate is public enou
3636
LL | pub use ::E::*;
3737
| ^^^^^^
3838
|
39+
note: the most public imported item is `pub(crate)`
40+
--> $DIR/private-variant-reexport.rs:15:13
41+
|
42+
LL | pub use ::E::*;
43+
| ^^^^^^
3944
note: the lint level is defined here
4045
--> $DIR/private-variant-reexport.rs:13:8
4146
|

0 commit comments

Comments
 (0)