Skip to content

Commit 0e8dab2

Browse files
authored
Rollup merge of #143856 - mladedav:dm/private-reexport, r=petrochenkov
Linting public reexport of private dependencies Part of public/private dependencies #44663 Partially addresses #71043 I'm adding a warning for reexports of private dependencies into `rustc_resolve`. I get that this should not be a warning, but should instead be a lint to be controlled by the feature gate, but I did not figure out how exactly to do that at that point. I tried doing the same thing as is done in `rustc_privacy`, but the linting system is not ready yet as far as I understand the error I got, so I made a warning for now instead. Some guidance on how to emit lints with `dcx` would be appreciated. This also sets the `std_detect` crate as a public dependency of `std` because some macros are reexported from there. I did not check closer, but the other option may be to allow the specific reexports instead.
2 parents eb818f2 + d627e25 commit 0e8dab2

File tree

9 files changed

+93
-22
lines changed

9 files changed

+93
-22
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@ lint_redundant_semicolons_suggestion = remove {$multiple_semicolons ->
745745
*[false] this semicolon
746746
}
747747
748+
lint_reexport_private_dependency =
749+
{$kind} `{$name}` from private dependency '{$krate}' is re-exported
750+
748751
lint_remove_mut_from_pattern = remove `mut` from the parameter
749752
750753
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
@@ -3094,6 +3094,14 @@ pub(crate) struct HiddenGlobReexports {
30943094
pub namespace: String,
30953095
}
30963096

3097+
#[derive(LintDiagnostic)]
3098+
#[diag(lint_reexport_private_dependency)]
3099+
pub(crate) struct ReexportPrivateDependency {
3100+
pub name: String,
3101+
pub kind: String,
3102+
pub krate: Symbol,
3103+
}
3104+
30973105
#[derive(LintDiagnostic)]
30983106
#[diag(lint_unnecessary_qualification)]
30993107
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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_middle::span_bug;
1515
use rustc_middle::ty::Visibility;
1616
use rustc_session::lint::BuiltinLintDiag;
1717
use rustc_session::lint::builtin::{
18-
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
19-
REDUNDANT_IMPORTS, UNUSED_IMPORTS,
18+
AMBIGUOUS_GLOB_REEXPORTS, EXPORTED_PRIVATE_DEPENDENCIES, HIDDEN_GLOB_REEXPORTS,
19+
PUB_USE_OF_PRIVATE_EXTERN_CRATE, REDUNDANT_IMPORTS, UNUSED_IMPORTS,
2020
};
2121
use rustc_session::parse::feature_err;
2222
use rustc_span::edit_distance::find_best_match_for_name;
@@ -635,10 +635,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
635635
}
636636
}
637637

638-
pub(crate) fn check_hidden_glob_reexports(
639-
&mut self,
640-
exported_ambiguities: FxHashSet<NameBinding<'ra>>,
641-
) {
638+
pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<NameBinding<'ra>>) {
642639
for module in self.arenas.local_modules().iter() {
643640
for (key, resolution) in self.resolutions(*module).borrow().iter() {
644641
let resolution = resolution.borrow();
@@ -697,6 +694,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
697694
}
698695
}
699696
}
697+
698+
if let NameBindingKind::Import { import, .. } = binding.kind
699+
&& let Some(binding_id) = import.id()
700+
&& let import_def_id = self.local_def_id(binding_id)
701+
&& self.effective_visibilities.is_exported(import_def_id)
702+
&& let Res::Def(reexported_kind, reexported_def_id) = binding.res()
703+
&& !matches!(reexported_kind, DefKind::Ctor(..))
704+
&& !reexported_def_id.is_local()
705+
&& self.tcx.is_private_dep(reexported_def_id.krate)
706+
{
707+
self.lint_buffer.buffer_lint(
708+
EXPORTED_PRIVATE_DEPENDENCIES,
709+
binding_id,
710+
binding.span,
711+
BuiltinLintDiag::ReexportPrivateDependency {
712+
kind: binding.res().descr().to_string(),
713+
name: key.ident.name.to_string(),
714+
krate: self.tcx.crate_name(reexported_def_id.krate),
715+
},
716+
);
717+
}
700718
}
701719
}
702720
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,9 +1773,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17731773
let exported_ambiguities = self.tcx.sess.time("compute_effective_visibilities", || {
17741774
EffectiveVisibilitiesVisitor::compute_effective_visibilities(self, krate)
17751775
});
1776-
self.tcx.sess.time("check_hidden_glob_reexports", || {
1777-
self.check_hidden_glob_reexports(exported_ambiguities)
1778-
});
1776+
self.tcx.sess.time("lint_reexports", || self.lint_reexports(exported_ambiguities));
17791777
self.tcx
17801778
.sess
17811779
.time("finalize_macro_resolutions", || self.finalize_macro_resolutions(krate));

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ unwind = { path = "../unwind" }
2323
hashbrown = { version = "0.15", default-features = false, features = [
2424
'rustc-dep-of-std',
2525
] }
26-
std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = [
26+
std_detect = { path = "../stdarch/crates/std_detect", public = true, default-features = false, features = [
2727
'rustc-dep-of-std',
2828
] }
2929

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)