Skip to content

Linting public reexport of private dependencies #143856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ lint_redundant_semicolons_suggestion = remove {$multiple_semicolons ->
*[false] this semicolon
}
lint_reexport_private_dependency =
{$kind} `{$name}` from private dependency '{$krate}' is re-exported
lint_remove_mut_from_pattern = remove `mut` from the parameter
lint_removed_lint = lint `{$name}` has been removed: {$reason}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ pub fn decorate_builtin_lint(
}
.decorate_lint(diag);
}
BuiltinLintDiag::ReexportPrivateDependency { name, kind, krate } => {
lints::ReexportPrivateDependency { name, kind, krate }.decorate_lint(diag);
}
BuiltinLintDiag::UnusedQualifications { removal_span } => {
lints::UnusedQualifications { removal_span }.decorate_lint(diag);
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3080,6 +3080,14 @@ pub(crate) struct HiddenGlobReexports {
pub namespace: String,
}

#[derive(LintDiagnostic)]
#[diag(lint_reexport_private_dependency)]
pub(crate) struct ReexportPrivateDependency {
pub name: String,
pub kind: String,
pub krate: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(lint_unnecessary_qualification)]
pub(crate) struct UnusedQualifications {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ pub enum BuiltinLintDiag {
/// The local binding that shadows the glob reexport.
private_item_span: Span,
},
ReexportPrivateDependency {
name: String,
kind: String,
krate: Symbol,
},
UnusedQualifications {
/// The span of the unnecessarily-qualified path to remove.
removal_span: Span,
Expand Down
30 changes: 24 additions & 6 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use rustc_middle::metadata::{ModChild, Reexport};
use rustc_middle::{span_bug, ty};
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS,
PUB_USE_OF_PRIVATE_EXTERN_CRATE, REDUNDANT_IMPORTS, UNUSED_IMPORTS,
};
use rustc_session::parse::feature_err;
use rustc_span::edit_distance::find_best_match_for_name;
Expand Down Expand Up @@ -634,10 +634,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

pub(crate) fn check_hidden_glob_reexports(
&mut self,
exported_ambiguities: FxHashSet<NameBinding<'ra>>,
) {
pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<NameBinding<'ra>>) {
for module in self.arenas.local_modules().iter() {
for (key, resolution) in self.resolutions(*module).borrow().iter() {
let resolution = resolution.borrow();
Expand Down Expand Up @@ -699,6 +696,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
}

if let NameBindingKind::Import { import, .. } = binding.kind
&& let Res::Def(reexported_kind, reexported_def_id) = binding.res()
&& !matches!(reexported_kind, DefKind::Ctor(..))
&& !reexported_def_id.is_local()
&& self.tcx.is_private_dep(reexported_def_id.krate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it makes sense to put the expensive checks last (effective_visibilities.is_exported, then is_private_dep) and cheap but effective checks first (reexported_def_id.is_local()).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe this is fine, the is_exported check will reject 90+% of imports right away.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you have already changed it, and with is_private_dep first.
Sorry, please revert :D

&& let Some(binding_id) = import.id()
&& let import_def_id = self.local_def_id(binding_id)
&& self.effective_visibilities.is_exported(import_def_id)
{
self.lint_buffer.buffer_lint(
EXPORTED_PRIVATE_DEPENDENCIES,
binding_id,
binding.span,
BuiltinLintDiag::ReexportPrivateDependency {
kind: binding.res().descr().to_string(),
name: key.ident.name.to_string(),
krate: self.tcx.crate_name(reexported_def_id.krate),
},
);
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1775,9 +1775,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let exported_ambiguities = self.tcx.sess.time("compute_effective_visibilities", || {
EffectiveVisibilitiesVisitor::compute_effective_visibilities(self, krate)
});
self.tcx.sess.time("check_hidden_glob_reexports", || {
self.check_hidden_glob_reexports(exported_ambiguities)
});
self.tcx.sess.time("lint_reexports", || self.lint_reexports(exported_ambiguities));
self.tcx
.sess
.time("finalize_macro_resolutions", || self.finalize_macro_resolutions(krate));
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unwind = { path = "../unwind" }
hashbrown = { version = "0.15", default-features = false, features = [
'rustc-dep-of-std',
] }
std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = [
std_detect = { path = "../stdarch/crates/std_detect", public = true, default-features = false, features = [
'rustc-dep-of-std',
] }

Expand Down
14 changes: 7 additions & 7 deletions tests/ui/privacy/pub-priv-dep/pub-priv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#![deny(exported_private_dependencies)]

// This crate is a private dependency
// FIXME: This should trigger.
pub extern crate priv_dep;
//~^ ERROR crate `priv_dep` from private dependency 'priv_dep' is re-exported
// This crate is a public dependency
extern crate pub_dep;
pub extern crate pub_dep;
// This crate is a private dependency
extern crate pm;

Expand Down Expand Up @@ -91,16 +91,16 @@ pub struct AllowedPrivType {
pub allowed: OtherType,
}

// FIXME: This should trigger.
pub use priv_dep::m;
// FIXME: This should trigger.
//~^ ERROR macro `m` from private dependency 'priv_dep' is re-exported
pub use pm::fn_like;
// FIXME: This should trigger.
//~^ ERROR macro `fn_like` from private dependency 'pm' is re-exported
pub use pm::PmDerive;
// FIXME: This should trigger.
//~^ ERROR macro `PmDerive` from private dependency 'pm' is re-exported
pub use pm::pm_attr;
//~^ ERROR macro `pm_attr` from private dependency 'pm' is re-exported

// FIXME: This should trigger.
pub use priv_dep::E::V1;
//~^ ERROR variant `V1` from private dependency 'priv_dep' is re-exported

fn main() {}
46 changes: 41 additions & 5 deletions tests/ui/privacy/pub-priv-dep/pub-priv1.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
error: type `OtherType` from private dependency 'priv_dep' in public interface
--> $DIR/pub-priv1.rs:29:5
error: crate `priv_dep` from private dependency 'priv_dep' is re-exported
--> $DIR/pub-priv1.rs:12:1
|
LL | pub field: OtherType,
| ^^^^^^^^^^^^^^^^^^^^
LL | pub extern crate priv_dep;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/pub-priv1.rs:9:9
|
LL | #![deny(exported_private_dependencies)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: macro `m` from private dependency 'priv_dep' is re-exported
--> $DIR/pub-priv1.rs:94:9
|
LL | pub use priv_dep::m;
| ^^^^^^^^^^^

error: macro `fn_like` from private dependency 'pm' is re-exported
--> $DIR/pub-priv1.rs:96:9
|
LL | pub use pm::fn_like;
| ^^^^^^^^^^^

error: derive macro `PmDerive` from private dependency 'pm' is re-exported
--> $DIR/pub-priv1.rs:98:9
|
LL | pub use pm::PmDerive;
| ^^^^^^^^^^^^

error: attribute macro `pm_attr` from private dependency 'pm' is re-exported
--> $DIR/pub-priv1.rs:100:9
|
LL | pub use pm::pm_attr;
| ^^^^^^^^^^^

error: variant `V1` from private dependency 'priv_dep' is re-exported
--> $DIR/pub-priv1.rs:103:9
|
LL | pub use priv_dep::E::V1;
| ^^^^^^^^^^^^^^^

error: type `OtherType` from private dependency 'priv_dep' in public interface
--> $DIR/pub-priv1.rs:29:5
|
LL | pub field: OtherType,
| ^^^^^^^^^^^^^^^^^^^^

error: type `OtherType` from private dependency 'priv_dep' in public interface
--> $DIR/pub-priv1.rs:36:5
|
Expand Down Expand Up @@ -90,5 +126,5 @@ LL | impl PubTraitOnPrivate for OtherType {}
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 14 previous errors
error: aborting due to 20 previous errors