Skip to content

Commit 0f090e5

Browse files
committed
vis note for no pub reexports glob import
1 parent 6a66ca2 commit 0f090e5

15 files changed

+83
-70
lines changed

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, ty::Visibility)>,
182183
}
183184

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

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

+15-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" }
@@ -1935,4 +1935,17 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
19351935
for id in tcx.hir().items() {
19361936
checker.check_item(id);
19371937
}
1938+
1939+
report_insufficient_visibility(tcx);
1940+
}
1941+
1942+
fn report_insufficient_visibility(tcx: TyCtxt<'_>) {
1943+
for (def_id, vis) in &tcx.resolutions(()).insufficient_vis {
1944+
tcx.emit_spanned_lint(
1945+
lint::builtin::UNUSED_IMPORTS,
1946+
tcx.hir().local_def_id_to_hir_id(*def_id),
1947+
tcx.def_span(def_id.to_def_id()),
1948+
InsufficientVisibility { max_vis: vis_to_string(*def_id, *vis, tcx) },
1949+
);
1950+
}
19381951
}

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, 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, 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,
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
// https://github.com/rust-lang/rust/issues/115966
3+
4+
mod m {
5+
pub(crate) type A = u8;
6+
}
7+
8+
pub use m::*;
9+
//~^ WARNING: glob import doesn't reexport anything because no candidate is public enough
10+
//~| NOTE: the most public imported item is `pub(crate)`
11+
//~| NOTE: #[warn(unused_imports)]` on by default
12+
fn main() {
13+
let _: A;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: glob import doesn't reexport anything because no candidate is public enough
2+
--> $DIR/no-reexports-but-used.rs:8:9
3+
|
4+
LL | pub use m::*;
5+
| ^
6+
|
7+
= note: the most public imported item is `pub(crate)`
8+
= note: `#[warn(unused_imports)]` on by default
9+
10+
warning: 1 warning emitted
11+

tests/ui/imports/reexports.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +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
13-
//~| WARNING unused import: `super::*`
12+
//~^ WARNING unused import: `super::*`
1413
}
1514
}
1615

tests/ui/imports/reexports.stderr

+7-13
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ LL | pub use super::foo;
1111
| ^^^^^^^^^^
1212

1313
error[E0603]: module import `foo` is private
14-
--> $DIR/reexports.rs:36:15
14+
--> $DIR/reexports.rs:35:15
1515
|
1616
LL | use b::a::foo::S;
1717
| ^^^ private module import
1818
|
1919
note: the module import `foo` is defined here...
20-
--> $DIR/reexports.rs:24:17
20+
--> $DIR/reexports.rs:23:17
2121
|
2222
LL | pub use super::foo; // This is OK since the value `foo` is visible enough.
2323
| ^^^^^^^^^^
2424
note: ...and refers to the module `foo` which is defined here
25-
--> $DIR/reexports.rs:19:5
25+
--> $DIR/reexports.rs:18:5
2626
|
2727
LL | mod foo {
2828
| ^^^^^^^
2929

3030
error[E0603]: module import `foo` is private
31-
--> $DIR/reexports.rs:37:15
31+
--> $DIR/reexports.rs:36:15
3232
|
3333
LL | use b::b::foo::S as T;
3434
| ^^^ private module import
3535
|
3636
note: the module import `foo` is defined here...
37-
--> $DIR/reexports.rs:29:17
37+
--> $DIR/reexports.rs:28:17
3838
|
3939
LL | pub use super::*; // This is also OK since the value `foo` is visible enough.
4040
| ^^^^^^^^
4141
note: ...and refers to the module `foo` which is defined here
42-
--> $DIR/reexports.rs:19:5
42+
--> $DIR/reexports.rs:18:5
4343
|
4444
LL | mod foo {
4545
| ^^^^^^^
@@ -56,19 +56,13 @@ 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
60-
--> $DIR/reexports.rs:11:17
61-
|
62-
LL | pub use super::*;
63-
| ^^^^^^^^
64-
6559
warning: unused import: `super::*`
6660
--> $DIR/reexports.rs:11:17
6761
|
6862
LL | pub use super::*;
6963
| ^^^^^^^^
7064

71-
error: aborting due to 3 previous errors; 3 warnings emitted
65+
error: aborting due to 3 previous errors; 2 warnings emitted
7266

7367
Some errors have detailed explanations: E0364, E0603.
7468
For more information about an error, try `rustc --explain E0364`.

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#[deny(unused_imports)]
22
mod rank {
33
pub use self::Professor::*;
4-
//~^ ERROR glob import doesn't reexport anything
5-
//~| ERROR unused import: `self::Professor::*`
4+
//~^ ERROR unused import: `self::Professor::*`
65
pub use self::Lieutenant::{JuniorGrade, Full};
76
//~^ ERROR `JuniorGrade` is private, and cannot be re-exported
87
//~| ERROR `Full` is private, and cannot be re-exported
98
//~| ERROR unused imports: `Full`, `JuniorGrade`
109
pub use self::PettyOfficer::*;
11-
//~^ ERROR glob import doesn't reexport anything
12-
//~| ERROR unused import: `self::PettyOfficer::*`
10+
//~^ ERROR unused import: `self::PettyOfficer::*`
1311
pub use self::Crewman::*;
14-
//~^ ERROR glob import doesn't reexport anything
15-
//~| ERROR unused import: `self::Crewman::*`
12+
//~^ ERROR unused import: `self::Crewman::*`
1613

1714
enum Professor {
1815
Adjunct,
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
error[E0364]: `JuniorGrade` is private, and cannot be re-exported
2-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
2+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
33
|
44
LL | pub use self::Lieutenant::{JuniorGrade, Full};
55
| ^^^^^^^^^^^
66
|
77
note: consider marking `JuniorGrade` as `pub` in the imported module
8-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
8+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
99
|
1010
LL | pub use self::Lieutenant::{JuniorGrade, Full};
1111
| ^^^^^^^^^^^
1212

1313
error[E0364]: `Full` is private, and cannot be re-exported
14-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
14+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
1515
|
1616
LL | pub use self::Lieutenant::{JuniorGrade, Full};
1717
| ^^^^
1818
|
1919
note: consider marking `Full` as `pub` in the imported module
20-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
20+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
2121
|
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,42 +34,24 @@ 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`
44-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
38+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5: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
50-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
51-
|
52-
LL | pub use self::PettyOfficer::*;
53-
| ^^^^^^^^^^^^^^^^^^^^^
54-
5543
error: unused import: `self::PettyOfficer::*`
56-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
44+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:9:13
5745
|
5846
LL | pub use self::PettyOfficer::*;
5947
| ^^^^^^^^^^^^^^^^^^^^^
6048

61-
error: glob import doesn't reexport anything because no candidate is public enough
62-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
63-
|
64-
LL | pub use self::Crewman::*;
65-
| ^^^^^^^^^^^^^^^^
66-
6749
error: unused import: `self::Crewman::*`
68-
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
50+
--> $DIR/issue-46209-private-enum-variant-reexport.rs:11:13
6951
|
7052
LL | pub use self::Crewman::*;
7153
| ^^^^^^^^^^^^^^^^
7254

73-
error: aborting due to 9 previous errors
55+
error: aborting due to 6 previous errors
7456

7557
For more information about this error, try `rustc --explain E0364`.

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ mod m3 {
1313
#[deny(unused_imports)]
1414
mod m4 {
1515
pub use ::E::*;
16-
//~^ ERROR glob import doesn't reexport anything
17-
//~| ERROR unused import: `::E::*`
16+
//~^ ERROR unused import: `::E::*`
1817
}
1918

2019
enum E { V }

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

+2-8
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,13 +42,7 @@ note: the lint level is defined here
4242
LL | #[deny(unused_imports)]
4343
| ^^^^^^^^^^^^^^
4444

45-
error: unused import: `::E::*`
46-
--> $DIR/private-variant-reexport.rs:15:13
47-
|
48-
LL | pub use ::E::*;
49-
| ^^^^^^
50-
51-
error: aborting due to 5 previous errors
45+
error: aborting due to 4 previous errors
5246

5347
Some errors have detailed explanations: E0364, E0365.
5448
For more information about an error, try `rustc --explain E0364`.

0 commit comments

Comments
 (0)