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 2 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
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ resolve_forward_declared_generic_param =
resolve_found_an_item_configured_out =
found an item that was configured out
resolve_from_private_dep_in_public_reexport =
{$kind} `{$descr}` from private dependency '{$krate}' is reexported
resolve_generic_arguments_in_macro_path =
generic arguments in macro path
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_errors::codes::*;
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
use rustc_hir::def::{self, DefKind, PartialRes};
use rustc_hir::def_id::DefId;
use rustc_macros::Diagnostic;
use rustc_middle::metadata::{ModChild, Reexport};
use rustc_middle::{span_bug, ty};
use rustc_session::lint::BuiltinLintDiag;
Expand Down Expand Up @@ -40,6 +41,14 @@ use crate::{

type Res = def::Res<NodeId>;

#[derive(Diagnostic)]
#[diag(resolve_from_private_dep_in_public_reexport)]
struct FromPrivateDependencyInPublicInterface<'a> {
pub kind: &'a str,
pub descr: &'a str,
pub krate: Symbol,
}

/// Contains data for specific kinds of imports.
#[derive(Clone)]
pub(crate) enum ImportKind<'ra> {
Expand Down Expand Up @@ -546,6 +555,41 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.finalize_resolutions_in(*module);
}

for (_module_def_id, module_children) in
self.tcx.with_stable_hashing_context(|hcx| self.module_children.to_sorted(&hcx, false))
{
for import in module_children {
if !import.vis.is_public() {
continue;
}

let def::Res::<_>::Def(reexported_kind, reexported_def_id) = import.res else {
continue;
};

if let DefKind::Ctor(..) = reexported_kind {
continue;
}

if reexported_def_id.is_local() {
continue;
}

if !self.tcx.is_private_dep(reexported_def_id.krate) {
continue;
}

self.dcx()
.create_warn(FromPrivateDependencyInPublicInterface {
kind: import.res.descr(),
descr: import.ident.as_str(),
krate: self.tcx.crate_name(reexported_def_id.krate),
})
.with_span(import.ident.span)
.emit();
}
}

let mut seen_spans = FxHashSet::default();
let mut errors = vec![];
let mut prev_root_id: NodeId = NodeId::ZERO;
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;
//~^ WARN crate `priv_dep` from private dependency 'priv_dep' is reexported
// 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.
//~^ WARN macro `m` from private dependency 'priv_dep' is reexported
pub use pm::fn_like;
// FIXME: This should trigger.
//~^ WARN macro `fn_like` from private dependency 'pm' is reexported
pub use pm::PmDerive;
// FIXME: This should trigger.
//~^ WARN macro `PmDerive` from private dependency 'pm' is reexported
pub use pm::pm_attr;
//~^ WARN macro `pm_attr` from private dependency 'pm' is reexported

// FIXME: This should trigger.
pub use priv_dep::E::V1;
//~^ WARN variant `V1` from private dependency 'priv_dep' is reexported

fn main() {}
38 changes: 37 additions & 1 deletion tests/ui/privacy/pub-priv-dep/pub-priv1.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
warning: crate `priv_dep` from private dependency 'priv_dep' is reexported
--> $DIR/pub-priv1.rs:12:18
|
LL | pub extern crate priv_dep;
| ^^^^^^^^

warning: macro `m` from private dependency 'priv_dep' is reexported
--> $DIR/pub-priv1.rs:94:19
|
LL | pub use priv_dep::m;
| ^

warning: macro `fn_like` from private dependency 'pm' is reexported
--> $DIR/pub-priv1.rs:96:13
|
LL | pub use pm::fn_like;
| ^^^^^^^

warning: derive macro `PmDerive` from private dependency 'pm' is reexported
--> $DIR/pub-priv1.rs:98:13
|
LL | pub use pm::PmDerive;
| ^^^^^^^^

warning: attribute macro `pm_attr` from private dependency 'pm' is reexported
--> $DIR/pub-priv1.rs:100:13
|
LL | pub use pm::pm_attr;
| ^^^^^^^

warning: variant `V1` from private dependency 'priv_dep' is reexported
--> $DIR/pub-priv1.rs:103:22
|
LL | pub use priv_dep::E::V1;
| ^^

error: type `OtherType` from private dependency 'priv_dep' in public interface
--> $DIR/pub-priv1.rs:29: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 14 previous errors; 6 warnings emitted

Loading