Skip to content

Commit 96e0a95

Browse files
committed
vis note for no pub reexports glob import
1 parent da1e0d1 commit 96e0a95

14 files changed

+99
-30
lines changed

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,11 @@ rustc_queries! {
11181118
eval_always
11191119
desc { "checking effective visibilities" }
11201120
}
1121+
1122+
query report_insufficient_visibility(_: ()) -> () {
1123+
desc { "report these items with insufficient visibility." }
1124+
}
1125+
11211126
query check_private_in_public(_: ()) -> () {
11221127
eval_always
11231128
desc { "checking for private elements in public interfaces" }

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub struct ResolverGlobalCtxt {
179179
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
180180
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
181181
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
182+
pub insufficient_vis: Vec<(LocalDefId, Span, ty::Visibility)>,
182183
}
183184

184185
/// Resolutions that should only be used for lowering.

compiler/rustc_passes/src/hir_id_validator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
1212
crate::hir_stats::print_hir_stats(tcx);
1313
}
1414

15+
tcx.ensure().report_insufficient_visibility(());
16+
1517
#[cfg(debug_assertions)]
1618
{
1719
let errors = Lock::new(Vec::new());

compiler/rustc_privacy/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ privacy_field_is_private_label = private field
55
privacy_from_private_dep_in_public_interface =
66
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
77
8+
privacy_glob_import_doesnt_reexport =
9+
glob import doesn't reexport anything because no candidate is public enough
10+
.note = the most public imported item is `{$max_vis}`
11+
812
privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interface
913
.label = can't leak {$vis_descr} {$kind}
1014
.visibility_label = `{$descr}` declared as {$vis_descr}

compiler/rustc_privacy/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,10 @@ pub struct PrivateInterfacesOrBoundsLint<'a> {
104104
pub ty_descr: DiagnosticArgFromDisplay<'a>,
105105
pub ty_vis_descr: &'a str,
106106
}
107+
108+
#[derive(LintDiagnostic)]
109+
#[diag(privacy_glob_import_doesnt_reexport)]
110+
#[note]
111+
pub struct InsufficientVisibility {
112+
pub max_vis: String,
113+
}

compiler/rustc_privacy/src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use std::{fmt, mem};
4545

4646
use errors::{
4747
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
48-
ItemIsPrivate, PrivateInterfacesOrBoundsLint, ReportEffectiveVisibility, UnnameableTypesLint,
49-
UnnamedItemIsPrivate,
48+
InsufficientVisibility, ItemIsPrivate, PrivateInterfacesOrBoundsLint,
49+
ReportEffectiveVisibility, UnnameableTypesLint, UnnamedItemIsPrivate,
5050
};
5151

5252
fluent_messages! { "../messages.ftl" }
@@ -1810,6 +1810,7 @@ pub fn provide(providers: &mut Providers) {
18101810
effective_visibilities,
18111811
check_private_in_public,
18121812
check_mod_privacy,
1813+
report_insufficient_visibility,
18131814
..*providers
18141815
};
18151816
}
@@ -1936,3 +1937,14 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
19361937
checker.check_item(id);
19371938
}
19381939
}
1940+
1941+
fn report_insufficient_visibility(tcx: TyCtxt<'_>, (): ()) {
1942+
for (def_id, span, vis) in &tcx.resolutions(()).insufficient_vis {
1943+
tcx.emit_spanned_lint(
1944+
lint::builtin::UNUSED_IMPORTS,
1945+
tcx.hir().local_def_id_to_hir_id(*def_id),
1946+
*span,
1947+
InsufficientVisibility { max_vis: vis_to_string(*def_id, *vis, tcx) },
1948+
);
1949+
}
1950+
}

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

+3-7
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};
@@ -989,12 +989,8 @@ 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(
993-
UNUSED_IMPORTS,
994-
id,
995-
import.span,
996-
fluent::resolve_glob_import_doesnt_reexport,
997-
);
992+
let def_id = self.local_def_id(id);
993+
self.insufficient_vis.push((def_id, import.span, max_vis));
998994
}
999995
return None;
1000996
}

compiler/rustc_resolve/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,8 @@ pub struct Resolver<'a, 'tcx> {
958958
/// All non-determined imports.
959959
indeterminate_imports: Vec<Import<'a>>,
960960

961+
insufficient_vis: Vec<(LocalDefId, Span, ty::Visibility)>,
962+
961963
// Spans for local variables found during pattern resolution.
962964
// Used for suggestions during error reporting.
963965
pat_span_map: NodeMap<Span>,
@@ -1340,6 +1342,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13401342
determined_imports: Vec::new(),
13411343
indeterminate_imports: Vec::new(),
13421344

1345+
insufficient_vis: Vec::new(),
1346+
13431347
pat_span_map: Default::default(),
13441348
partial_res_map: Default::default(),
13451349
import_res_map: Default::default(),
@@ -1525,6 +1529,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15251529
doc_link_resolutions: self.doc_link_resolutions,
15261530
doc_link_traits_in_scope: self.doc_link_traits_in_scope,
15271531
all_macro_rules: self.all_macro_rules,
1532+
insufficient_vis: self.insufficient_vis,
15281533
};
15291534
let ast_lowering = ty::ResolverAstLowering {
15301535
legacy_const_generic_args: self.legacy_const_generic_args,
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,15 @@
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+
note: the lint level is defined here
9+
--> $DIR/no-pub-reexports-but-used.rs:8:8
10+
|
11+
LL | #[warn(unused_imports)]
12+
| ^^^^^^^^^^^^^^
13+
14+
warning: 1 warning emitted
15+

tests/ui/imports/reexports.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,19 @@ 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: unused import: `super::*`
6060
--> $DIR/reexports.rs:11:17
6161
|
6262
LL | pub use super::*;
6363
| ^^^^^^^^
6464

65-
warning: unused import: `super::*`
65+
warning: glob import doesn't reexport anything because no candidate is public enough
6666
--> $DIR/reexports.rs:11:17
6767
|
6868
LL | pub use super::*;
6969
| ^^^^^^^^
70+
|
71+
= note: the most public imported item is `pub(a)`
7072

7173
error: aborting due to 3 previous errors; 3 warnings emitted
7274

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

+20-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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: unused import: `self::Professor::*`
2626
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
2727
|
2828
LL | pub use self::Professor::*;
@@ -34,41 +34,47 @@ note: the lint level is defined here
3434
LL | #[deny(unused_imports)]
3535
| ^^^^^^^^^^^^^^
3636

37-
error: unused import: `self::Professor::*`
38-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
39-
|
40-
LL | pub use self::Professor::*;
41-
| ^^^^^^^^^^^^^^^^^^
42-
4337
error: unused imports: `Full`, `JuniorGrade`
4438
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
4539
|
4640
LL | pub use self::Lieutenant::{JuniorGrade, Full};
4741
| ^^^^^^^^^^^ ^^^^
4842

49-
error: glob import doesn't reexport anything because no candidate is public enough
43+
error: unused import: `self::PettyOfficer::*`
5044
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
5145
|
5246
LL | pub use self::PettyOfficer::*;
5347
| ^^^^^^^^^^^^^^^^^^^^^
5448

55-
error: unused import: `self::PettyOfficer::*`
49+
error: unused import: `self::Crewman::*`
50+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
51+
|
52+
LL | pub use self::Crewman::*;
53+
| ^^^^^^^^^^^^^^^^
54+
55+
error: glob import doesn't reexport anything because no candidate is public enough
56+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
57+
|
58+
LL | pub use self::Professor::*;
59+
| ^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: the most public imported item is `pub(self)`
62+
63+
error: glob import doesn't reexport anything because no candidate is public enough
5664
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
5765
|
5866
LL | pub use self::PettyOfficer::*;
5967
| ^^^^^^^^^^^^^^^^^^^^^
68+
|
69+
= note: the most public imported item is `pub(self)`
6070

6171
error: glob import doesn't reexport anything because no candidate is public enough
6272
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
6373
|
6474
LL | pub use self::Crewman::*;
6575
| ^^^^^^^^^^^^^^^^
66-
67-
error: unused import: `self::Crewman::*`
68-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
6976
|
70-
LL | pub use self::Crewman::*;
71-
| ^^^^^^^^^^^^^^^^
77+
= note: the most public imported item is `pub(crate)`
7278

7379
error: aborting due to 9 previous errors
7480

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ 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: unused import: `::E::*`
3434
--> $DIR/private-variant-reexport.rs:15:13
3535
|
3636
LL | pub use ::E::*;
@@ -42,11 +42,13 @@ note: the lint level is defined here
4242
LL | #[deny(unused_imports)]
4343
| ^^^^^^^^^^^^^^
4444

45-
error: unused import: `::E::*`
45+
error: glob import doesn't reexport anything because no candidate is public enough
4646
--> $DIR/private-variant-reexport.rs:15:13
4747
|
4848
LL | pub use ::E::*;
4949
| ^^^^^^
50+
|
51+
= note: the most public imported item is `pub(crate)`
5052

5153
error: aborting due to 5 previous errors
5254

0 commit comments

Comments
 (0)