Skip to content

Commit

Permalink
vis note for no pub reexports glob import
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Oct 28, 2023
1 parent 6a66ca2 commit 0f090e5
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 70 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub struct ResolverGlobalCtxt {
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
pub insufficient_vis: Vec<(LocalDefId, ty::Visibility)>,
}

/// Resolutions that should only be used for lowering.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_privacy/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ privacy_field_is_private_label = private field
privacy_from_private_dep_in_public_interface =
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
privacy_glob_import_doesnt_reexport =
glob import doesn't reexport anything because no candidate is public enough
.note = the most public imported item is `{$max_vis}`
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}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_privacy/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ pub struct PrivateInterfacesOrBoundsLint<'a> {
pub ty_descr: DiagnosticArgFromDisplay<'a>,
pub ty_vis_descr: &'a str,
}

#[derive(LintDiagnostic)]
#[diag(privacy_glob_import_doesnt_reexport)]
#[note]
pub struct InsufficientVisibility {
pub max_vis: String,
}
17 changes: 15 additions & 2 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use std::{fmt, mem};

use errors::{
FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface,
ItemIsPrivate, PrivateInterfacesOrBoundsLint, ReportEffectiveVisibility, UnnameableTypesLint,
UnnamedItemIsPrivate,
InsufficientVisibility, ItemIsPrivate, PrivateInterfacesOrBoundsLint,
ReportEffectiveVisibility, UnnameableTypesLint, UnnamedItemIsPrivate,
};

fluent_messages! { "../messages.ftl" }
Expand Down Expand Up @@ -1935,4 +1935,17 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
for id in tcx.hir().items() {
checker.check_item(id);
}

report_insufficient_visibility(tcx);
}

fn report_insufficient_visibility(tcx: TyCtxt<'_>) {
for (def_id, vis) in &tcx.resolutions(()).insufficient_vis {
tcx.emit_spanned_lint(
lint::builtin::UNUSED_IMPORTS,
tcx.hir().local_def_id_to_hir_id(*def_id),
tcx.def_span(def_id.to_def_id()),
InsufficientVisibility { max_vis: vis_to_string(*def_id, *vis, tcx) },
);
}
}
3 changes: 0 additions & 3 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here
resolve_generic_params_from_outer_item_ty_param = type parameter from outer item
resolve_glob_import_doesnt_reexport =
glob import doesn't reexport anything because no candidate is public enough
resolve_ident_bound_more_than_once_in_parameter_list =
identifier `{$identifier}` is bound more than once in this parameter list
.label = used as parameter more than once
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::errors::{
ItemsInTraitsAreNotImportable,
};
use crate::Determinacy::{self, *};
use crate::{fluent_generated as fluent, Namespace::*};
use crate::Namespace::*;
use crate::{module_to_string, names_to_string, ImportSuggestion};
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
Expand Down Expand Up @@ -989,12 +989,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
&& let Some(max_vis) = max_vis.get()
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
{
self.lint_buffer.buffer_lint(
UNUSED_IMPORTS,
id,
import.span,
fluent::resolve_glob_import_doesnt_reexport,
);
let def_id = self.local_def_id(id);
self.insufficient_vis.push((def_id, max_vis));
}
return None;
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,8 @@ pub struct Resolver<'a, 'tcx> {
/// All non-determined imports.
indeterminate_imports: Vec<Import<'a>>,

insufficient_vis: Vec<(LocalDefId, ty::Visibility)>,

// Spans for local variables found during pattern resolution.
// Used for suggestions during error reporting.
pat_span_map: NodeMap<Span>,
Expand Down Expand Up @@ -1340,6 +1342,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
determined_imports: Vec::new(),
indeterminate_imports: Vec::new(),

insufficient_vis: Vec::new(),

pat_span_map: Default::default(),
partial_res_map: Default::default(),
import_res_map: Default::default(),
Expand Down Expand Up @@ -1525,6 +1529,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
doc_link_resolutions: self.doc_link_resolutions,
doc_link_traits_in_scope: self.doc_link_traits_in_scope,
all_macro_rules: self.all_macro_rules,
insufficient_vis: self.insufficient_vis,
};
let ast_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args,
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/imports/no-reexports-but-used.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// run-pass
// https://github.com/rust-lang/rust/issues/115966

mod m {
pub(crate) type A = u8;
}

pub use m::*;
//~^ WARNING: glob import doesn't reexport anything because no candidate is public enough
//~| NOTE: the most public imported item is `pub(crate)`
//~| NOTE: #[warn(unused_imports)]` on by default
fn main() {
let _: A;
}
11 changes: 11 additions & 0 deletions tests/ui/imports/no-reexports-but-used.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/no-reexports-but-used.rs:8:9
|
LL | pub use m::*;
| ^
|
= note: the most public imported item is `pub(crate)`
= note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

3 changes: 1 addition & 2 deletions tests/ui/imports/reexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ mod a {
//~^ ERROR cannot be re-exported
//~| WARNING unused import: `super::foo`
pub use super::*;
//~^ WARNING glob import doesn't reexport anything because no candidate is public enough
//~| WARNING unused import: `super::*`
//~^ WARNING unused import: `super::*`
}
}

Expand Down
20 changes: 7 additions & 13 deletions tests/ui/imports/reexports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ LL | pub use super::foo;
| ^^^^^^^^^^

error[E0603]: module import `foo` is private
--> $DIR/reexports.rs:36:15
--> $DIR/reexports.rs:35:15
|
LL | use b::a::foo::S;
| ^^^ private module import
|
note: the module import `foo` is defined here...
--> $DIR/reexports.rs:24:17
--> $DIR/reexports.rs:23:17
|
LL | pub use super::foo; // This is OK since the value `foo` is visible enough.
| ^^^^^^^^^^
note: ...and refers to the module `foo` which is defined here
--> $DIR/reexports.rs:19:5
--> $DIR/reexports.rs:18:5
|
LL | mod foo {
| ^^^^^^^

error[E0603]: module import `foo` is private
--> $DIR/reexports.rs:37:15
--> $DIR/reexports.rs:36:15
|
LL | use b::b::foo::S as T;
| ^^^ private module import
|
note: the module import `foo` is defined here...
--> $DIR/reexports.rs:29:17
--> $DIR/reexports.rs:28:17
|
LL | pub use super::*; // This is also OK since the value `foo` is visible enough.
| ^^^^^^^^
note: ...and refers to the module `foo` which is defined here
--> $DIR/reexports.rs:19:5
--> $DIR/reexports.rs:18:5
|
LL | mod foo {
| ^^^^^^^
Expand All @@ -56,19 +56,13 @@ note: the lint level is defined here
LL | #![warn(unused_imports)]
| ^^^^^^^^^^^^^^

warning: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/reexports.rs:11:17
|
LL | pub use super::*;
| ^^^^^^^^

warning: unused import: `super::*`
--> $DIR/reexports.rs:11:17
|
LL | pub use super::*;
| ^^^^^^^^

error: aborting due to 3 previous errors; 3 warnings emitted
error: aborting due to 3 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0364, E0603.
For more information about an error, try `rustc --explain E0364`.
9 changes: 3 additions & 6 deletions tests/ui/privacy/issue-46209-private-enum-variant-reexport.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#[deny(unused_imports)]
mod rank {
pub use self::Professor::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::Professor::*`
//~^ ERROR unused import: `self::Professor::*`
pub use self::Lieutenant::{JuniorGrade, Full};
//~^ ERROR `JuniorGrade` is private, and cannot be re-exported
//~| ERROR `Full` is private, and cannot be re-exported
//~| ERROR unused imports: `Full`, `JuniorGrade`
pub use self::PettyOfficer::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::PettyOfficer::*`
//~^ ERROR unused import: `self::PettyOfficer::*`
pub use self::Crewman::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `self::Crewman::*`
//~^ ERROR unused import: `self::Crewman::*`

enum Professor {
Adjunct,
Expand Down
36 changes: 9 additions & 27 deletions tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
error[E0364]: `JuniorGrade` is private, and cannot be re-exported
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^
|
note: consider marking `JuniorGrade` as `pub` in the imported module
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^

error[E0364]: `Full` is private, and cannot be re-exported
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^
|
note: consider marking `Full` as `pub` in the imported module
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:45
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^

error: glob import doesn't reexport anything because no candidate is public enough
error: unused import: `self::Professor::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
|
LL | pub use self::Professor::*;
Expand All @@ -34,42 +34,24 @@ note: the lint level is defined here
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unused import: `self::Professor::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13
|
LL | pub use self::Professor::*;
| ^^^^^^^^^^^^^^^^^^

error: unused imports: `Full`, `JuniorGrade`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:6:32
--> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32
|
LL | pub use self::Lieutenant::{JuniorGrade, Full};
| ^^^^^^^^^^^ ^^^^

error: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13
|
LL | pub use self::PettyOfficer::*;
| ^^^^^^^^^^^^^^^^^^^^^

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

error: glob import doesn't reexport anything because no candidate is public enough
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
|
LL | pub use self::Crewman::*;
| ^^^^^^^^^^^^^^^^

error: unused import: `self::Crewman::*`
--> $DIR/issue-46209-private-enum-variant-reexport.rs:13:13
--> $DIR/issue-46209-private-enum-variant-reexport.rs:11:13
|
LL | pub use self::Crewman::*;
| ^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0364`.
3 changes: 1 addition & 2 deletions tests/ui/privacy/private-variant-reexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ mod m3 {
#[deny(unused_imports)]
mod m4 {
pub use ::E::*;
//~^ ERROR glob import doesn't reexport anything
//~| ERROR unused import: `::E::*`
//~^ ERROR unused import: `::E::*`
}

enum E { V }
Expand Down
10 changes: 2 additions & 8 deletions tests/ui/privacy/private-variant-reexport.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LL | pub use ::E::V::{self};
|
= note: consider declaring type or module `V` with `pub`

error: glob import doesn't reexport anything because no candidate is public enough
error: unused import: `::E::*`
--> $DIR/private-variant-reexport.rs:15:13
|
LL | pub use ::E::*;
Expand All @@ -42,13 +42,7 @@ note: the lint level is defined here
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unused import: `::E::*`
--> $DIR/private-variant-reexport.rs:15:13
|
LL | pub use ::E::*;
| ^^^^^^

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

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

0 comments on commit 0f090e5

Please sign in to comment.