Skip to content

Commit ee5551b

Browse files
committed
vis note for no pub reexports glob import
1 parent 07921b5 commit ee5551b

File tree

12 files changed

+130
-33
lines changed

12 files changed

+130
-33
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::RedundantImportVisibility { max_vis, span } => {
931+
db.span_note(span, format!("the most public imported item is `{max_vis}`"));
932+
db.help("reduce the glob import's visibility or increase visibility of imported items");
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+
RedundantImportVisibility {
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(self, def_id: LocalDefId, tcx: TyCtxt<'_>) -> 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/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here
127127
128128
resolve_generic_params_from_outer_item_ty_param = type parameter from outer item
129129
130-
resolve_glob_import_doesnt_reexport =
131-
glob import doesn't reexport anything because no candidate is public enough
132-
133130
resolve_ident_bound_more_than_once_in_parameter_list =
134131
identifier `{$identifier}` is bound more than once in this parameter list
135132
.label = used as parameter more than once

compiler/rustc_resolve/src/imports.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::errors::{
88
ItemsInTraitsAreNotImportable,
99
};
1010
use crate::Determinacy::{self, *};
11-
use crate::{fluent_generated as fluent, Namespace::*};
11+
use crate::Namespace::*;
1212
use crate::{module_to_string, names_to_string, ImportSuggestion};
1313
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
1414
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
@@ -248,6 +248,19 @@ struct UnresolvedImportError {
248248
candidates: Option<Vec<ImportSuggestion>>,
249249
}
250250

251+
struct RedundantImportVisibilityMsg {
252+
vis: String,
253+
}
254+
255+
impl RedundantImportVisibilityMsg {
256+
fn to_string(self) -> String {
257+
format!(
258+
"glob import doesn't reexport anything with visibility `{}` because no imported item is public enough",
259+
self.vis
260+
)
261+
}
262+
}
263+
251264
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
252265
// are permitted for backward-compatibility under a deprecation lint.
253266
fn pub_use_of_private_extern_crate_hack(import: Import<'_>, binding: NameBinding<'_>) -> bool {
@@ -987,13 +1000,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
9871000
}
9881001
if !is_prelude
9891002
&& let Some(max_vis) = max_vis.get()
990-
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
1003+
&& let import_vis = import.expect_vis()
1004+
&& !max_vis.is_at_least(import_vis, self.tcx)
9911005
{
992-
self.lint_buffer.buffer_lint(
1006+
let def_id = self.local_def_id(id);
1007+
let msg = RedundantImportVisibilityMsg {
1008+
vis: import_vis.to_string(def_id, self.tcx),
1009+
};
1010+
self.lint_buffer.buffer_lint_with_diagnostic(
9931011
UNUSED_IMPORTS,
9941012
id,
9951013
import.span,
996-
fluent::resolve_glob_import_doesnt_reexport,
1014+
msg.to_string(),
1015+
BuiltinLintDiagnostics::RedundantImportVisibility {
1016+
max_vis: max_vis.to_string(def_id, self.tcx),
1017+
span: import.span,
1018+
},
9971019
);
9981020
}
9991021
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 with visibility `pub` because no imported item 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,20 @@
1+
warning: glob import doesn't reexport anything with visibility `pub` because no imported item 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+
= help: reduce the glob import's visibility or increase visibility of imported items
13+
note: the lint level is defined here
14+
--> $DIR/no-pub-reexports-but-used.rs:8:8
15+
|
16+
LL | #[warn(unused_imports)]
17+
| ^^^^^^^^^^^^^^
18+
19+
warning: 1 warning emitted
20+

tests/ui/imports/reexports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod a {
99
//~^ ERROR cannot be re-exported
1010
//~| WARNING unused import: `super::foo`
1111
pub use super::*;
12-
//~^ WARNING glob import doesn't reexport anything because no candidate is public enough
12+
//~^ WARNING glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
1313
//~| WARNING unused import: `super::*`
1414
}
1515
}

tests/ui/imports/reexports.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,18 @@ note: the lint level is defined here
5656
LL | #![warn(unused_imports)]
5757
| ^^^^^^^^^^^^^^
5858

59-
warning: glob import doesn't reexport anything because no candidate is public enough
59+
warning: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
6060
--> $DIR/reexports.rs:11:17
6161
|
6262
LL | pub use super::*;
6363
| ^^^^^^^^
64+
|
65+
note: the most public imported item is `pub(a)`
66+
--> $DIR/reexports.rs:11:17
67+
|
68+
LL | pub use super::*;
69+
| ^^^^^^^^
70+
= help: reduce the glob import's visibility or increase visibility of imported items
6471

6572
warning: unused import: `super::*`
6673
--> $DIR/reexports.rs:11:17

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ note: consider marking `Full` as `pub` in the imported module
2222
LL | pub use self::Lieutenant::{JuniorGrade, Full};
2323
| ^^^^
2424

25-
error: glob import doesn't reexport anything because no candidate is public enough
25+
error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
2626
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
2727
|
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+
| ^^^^^^^^^^^^^^^^^^
36+
= help: reduce the glob import's visibility or increase visibility of imported items
3137
note: the lint level is defined here
3238
--> $DIR/issue-46209-private-enum-variant-reexport.rs:1:8
3339
|
@@ -46,23 +52,37 @@ error: unused imports: `Full`, `JuniorGrade`
4652
LL | pub use self::Lieutenant::{JuniorGrade, Full};
4753
| ^^^^^^^^^^^ ^^^^
4854

49-
error: glob import doesn't reexport anything because no candidate is public enough
55+
error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
56+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
57+
|
58+
LL | pub use self::PettyOfficer::*;
59+
| ^^^^^^^^^^^^^^^^^^^^^
60+
|
61+
note: the most public imported item is `pub(self)`
5062
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
5163
|
5264
LL | pub use self::PettyOfficer::*;
5365
| ^^^^^^^^^^^^^^^^^^^^^
66+
= help: reduce the glob import's visibility or increase visibility of imported items
5467

5568
error: unused import: `self::PettyOfficer::*`
5669
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
5770
|
5871
LL | pub use self::PettyOfficer::*;
5972
| ^^^^^^^^^^^^^^^^^^^^^
6073

61-
error: glob import doesn't reexport anything because no candidate is public enough
74+
error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
75+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
76+
|
77+
LL | pub use self::Crewman::*;
78+
| ^^^^^^^^^^^^^^^^
79+
|
80+
note: the most public imported item is `pub(crate)`
6281
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
6382
|
6483
LL | pub use self::Crewman::*;
6584
| ^^^^^^^^^^^^^^^^
85+
= help: reduce the glob import's visibility or increase visibility of imported items
6686

6787
error: unused import: `self::Crewman::*`
6888
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ LL | pub use ::E::V::{self};
3030
|
3131
= note: consider declaring type or module `V` with `pub`
3232

33-
error: glob import doesn't reexport anything because no candidate is public enough
33+
error: glob import doesn't reexport anything with visibility `pub` because no imported item is public enough
3434
--> $DIR/private-variant-reexport.rs:15:13
3535
|
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+
| ^^^^^^
44+
= help: reduce the glob import's visibility or increase visibility of imported items
3945
note: the lint level is defined here
4046
--> $DIR/private-variant-reexport.rs:13:8
4147
|

0 commit comments

Comments
 (0)