Skip to content

Commit 8b868fa

Browse files
committed
Implement resolver warnings about reexporting private dependencies
1 parent 4a77a62 commit 8b868fa

File tree

7 files changed

+90
-14
lines changed

7 files changed

+90
-14
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,9 @@ lint_redundant_semicolons_suggestion = remove {$multiple_semicolons ->
744744
*[false] this semicolon
745745
}
746746
747+
lint_reexport_private_dependency =
748+
{$kind} `{$name}` from private dependency '{$krate}' is re-exported
749+
747750
lint_remove_mut_from_pattern = remove `mut` from the parameter
748751
749752
lint_removed_lint = lint `{$name}` has been removed: {$reason}

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ pub fn decorate_builtin_lint(
351351
}
352352
.decorate_lint(diag);
353353
}
354+
BuiltinLintDiag::ReexportPrivateDependency { name, kind, krate } => {
355+
lints::ReexportPrivateDependency { name, kind, krate }.decorate_lint(diag);
356+
}
354357
BuiltinLintDiag::UnusedQualifications { removal_span } => {
355358
lints::UnusedQualifications { removal_span }.decorate_lint(diag);
356359
}

compiler/rustc_lint/src/lints.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,14 @@ pub(crate) struct HiddenGlobReexports {
30803080
pub namespace: String,
30813081
}
30823082

3083+
#[derive(LintDiagnostic)]
3084+
#[diag(lint_reexport_private_dependency)]
3085+
pub(crate) struct ReexportPrivateDependency {
3086+
pub name: String,
3087+
pub kind: String,
3088+
pub krate: Symbol,
3089+
}
3090+
30833091
#[derive(LintDiagnostic)]
30843092
#[diag(lint_unnecessary_qualification)]
30853093
pub(crate) struct UnusedQualifications {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,11 @@ pub enum BuiltinLintDiag {
739739
/// The local binding that shadows the glob reexport.
740740
private_item_span: Span,
741741
},
742+
ReexportPrivateDependency {
743+
name: String,
744+
kind: String,
745+
krate: Symbol,
746+
},
742747
UnusedQualifications {
743748
/// The span of the unnecessarily-qualified path to remove.
744749
removal_span: Span,

compiler/rustc_resolve/src/imports.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_middle::metadata::{ModChild, Reexport};
1414
use rustc_middle::{span_bug, ty};
1515
use rustc_session::lint::BuiltinLintDiag;
1616
use rustc_session::lint::builtin::{
17-
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
18-
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
17+
AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS,
18+
PUB_USE_OF_PRIVATE_EXTERN_CRATE, REDUNDANT_IMPORTS, UNUSED_IMPORTS,
1919
};
2020
use rustc_session::parse::feature_err;
2121
use rustc_span::edit_distance::find_best_match_for_name;
@@ -696,6 +696,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
696696
}
697697
}
698698
}
699+
700+
if let NameBindingKind::Import { import, .. } = binding.kind
701+
&& let Some(binding_id) = import.id()
702+
&& let import_def_id = self.local_def_id(binding_id)
703+
&& self.effective_visibilities.is_exported(import_def_id)
704+
&& let Res::Def(reexported_kind, reexported_def_id) = binding.res()
705+
&& !matches!(reexported_kind, DefKind::Ctor(..))
706+
&& !reexported_def_id.is_local()
707+
&& self.tcx.is_private_dep(reexported_def_id.krate)
708+
{
709+
self.lint_buffer.buffer_lint(
710+
EXPORTED_PRIVATE_DEPENDENCIES,
711+
binding_id,
712+
binding.span,
713+
BuiltinLintDiag::ReexportPrivateDependency {
714+
kind: binding.res().descr().to_string(),
715+
name: key.ident.name.to_string(),
716+
krate: self.tcx.crate_name(reexported_def_id.krate),
717+
},
718+
);
719+
}
699720
}
700721
}
701722
}

tests/ui/privacy/pub-priv-dep/pub-priv1.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#![deny(exported_private_dependencies)]
1010

1111
// This crate is a private dependency
12-
// FIXME: This should trigger.
1312
pub extern crate priv_dep;
13+
//~^ ERROR crate `priv_dep` from private dependency 'priv_dep' is re-exported
1414
// This crate is a public dependency
15-
extern crate pub_dep;
15+
pub extern crate pub_dep;
1616
// This crate is a private dependency
1717
extern crate pm;
1818

@@ -91,16 +91,16 @@ pub struct AllowedPrivType {
9191
pub allowed: OtherType,
9292
}
9393

94-
// FIXME: This should trigger.
9594
pub use priv_dep::m;
96-
// FIXME: This should trigger.
95+
//~^ ERROR macro `m` from private dependency 'priv_dep' is re-exported
9796
pub use pm::fn_like;
98-
// FIXME: This should trigger.
97+
//~^ ERROR macro `fn_like` from private dependency 'pm' is re-exported
9998
pub use pm::PmDerive;
100-
// FIXME: This should trigger.
99+
//~^ ERROR macro `PmDerive` from private dependency 'pm' is re-exported
101100
pub use pm::pm_attr;
101+
//~^ ERROR macro `pm_attr` from private dependency 'pm' is re-exported
102102

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

106106
fn main() {}

tests/ui/privacy/pub-priv-dep/pub-priv1.stderr

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1-
error: type `OtherType` from private dependency 'priv_dep' in public interface
2-
--> $DIR/pub-priv1.rs:29:5
1+
error: crate `priv_dep` from private dependency 'priv_dep' is re-exported
2+
--> $DIR/pub-priv1.rs:12:1
33
|
4-
LL | pub field: OtherType,
5-
| ^^^^^^^^^^^^^^^^^^^^
4+
LL | pub extern crate priv_dep;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/pub-priv1.rs:9:9
99
|
1010
LL | #![deny(exported_private_dependencies)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
error: macro `m` from private dependency 'priv_dep' is re-exported
14+
--> $DIR/pub-priv1.rs:94:9
15+
|
16+
LL | pub use priv_dep::m;
17+
| ^^^^^^^^^^^
18+
19+
error: macro `fn_like` from private dependency 'pm' is re-exported
20+
--> $DIR/pub-priv1.rs:96:9
21+
|
22+
LL | pub use pm::fn_like;
23+
| ^^^^^^^^^^^
24+
25+
error: derive macro `PmDerive` from private dependency 'pm' is re-exported
26+
--> $DIR/pub-priv1.rs:98:9
27+
|
28+
LL | pub use pm::PmDerive;
29+
| ^^^^^^^^^^^^
30+
31+
error: attribute macro `pm_attr` from private dependency 'pm' is re-exported
32+
--> $DIR/pub-priv1.rs:100:9
33+
|
34+
LL | pub use pm::pm_attr;
35+
| ^^^^^^^^^^^
36+
37+
error: variant `V1` from private dependency 'priv_dep' is re-exported
38+
--> $DIR/pub-priv1.rs:103:9
39+
|
40+
LL | pub use priv_dep::E::V1;
41+
| ^^^^^^^^^^^^^^^
42+
43+
error: type `OtherType` from private dependency 'priv_dep' in public interface
44+
--> $DIR/pub-priv1.rs:29:5
45+
|
46+
LL | pub field: OtherType,
47+
| ^^^^^^^^^^^^^^^^^^^^
48+
1349
error: type `OtherType` from private dependency 'priv_dep' in public interface
1450
--> $DIR/pub-priv1.rs:36:5
1551
|
@@ -90,5 +126,5 @@ LL | impl PubTraitOnPrivate for OtherType {}
90126
|
91127
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
92128

93-
error: aborting due to 14 previous errors
129+
error: aborting due to 20 previous errors
94130

0 commit comments

Comments
 (0)