Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0a73143

Browse files
committedJul 12, 2024·
make pub_use_of_private_extern_crate a hard error
1 parent b286722 commit 0a73143

File tree

9 files changed

+37
-50
lines changed

9 files changed

+37
-50
lines changed
 

‎compiler/rustc_lint/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,6 @@ lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
647647
lint_pattern_in_foreign = patterns aren't allowed in foreign function declarations
648648
.label = pattern not allowed in foreign function
649649
650-
lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
651-
.suggestion = consider making the `extern crate` item publicly accessible
652-
653650
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}` in this scope
654651
.label = names from parent modules are not accessible without an explicit import
655652

‎compiler/rustc_lint/src/context/diagnostics.rs

-4
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
359359
lints::MacroUseDeprecated.decorate_lint(diag);
360360
}
361361
BuiltinLintDiag::UnusedMacroUse => lints::UnusedMacroUse.decorate_lint(diag),
362-
BuiltinLintDiag::PrivateExternCrateReexport { source: ident, extern_crate_span } => {
363-
lints::PrivateExternCrateReexport { ident, sugg: extern_crate_span.shrink_to_lo() }
364-
.decorate_lint(diag);
365-
}
366362
BuiltinLintDiag::UnusedLabel => lints::UnusedLabel.decorate_lint(diag),
367363
BuiltinLintDiag::MacroIsPrivate(ident) => {
368364
lints::MacroIsPrivate { ident }.decorate_lint(diag);

‎compiler/rustc_lint/src/lints.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2331,14 +2331,6 @@ pub struct MacroUseDeprecated;
23312331
#[diag(lint_unused_macro_use)]
23322332
pub struct UnusedMacroUse;
23332333

2334-
#[derive(LintDiagnostic)]
2335-
#[diag(lint_private_extern_crate_reexport, code = E0365)]
2336-
pub struct PrivateExternCrateReexport {
2337-
pub ident: Ident,
2338-
#[suggestion(code = "pub ", style = "verbose", applicability = "maybe-incorrect")]
2339-
pub sugg: Span,
2340-
}
2341-
23422334
#[derive(LintDiagnostic)]
23432335
#[diag(lint_unused_label)]
23442336
pub struct UnusedLabel;

‎compiler/rustc_lint_defs/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,6 @@ pub enum BuiltinLintDiag {
711711
},
712712
MacroUseDeprecated,
713713
UnusedMacroUse,
714-
PrivateExternCrateReexport {
715-
source: Ident,
716-
extern_crate_span: Span,
717-
},
718714
UnusedLabel,
719715
MacroIsPrivate(Ident),
720716
UnusedMacroDefinition(Symbol),

‎compiler/rustc_resolve/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
12
resolve_accessible_unsure = not sure whether the path is accessible or not
23
.note = the type may have associated items, but we are currently not checking them
34
5+
resolve_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
6+
.suggestion = consider making the `extern crate` item publicly accessible
7+
48
resolve_add_as_non_derive =
59
add as non-Derive macro
610
`#[{$macro_path}]`

‎compiler/rustc_resolve/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,16 @@ pub(crate) struct CannotBeReexportedCratePublicNS {
741741
pub(crate) ident: Ident,
742742
}
743743

744+
#[derive(Diagnostic)]
745+
#[diag(resolve_private_extern_crate_reexport, code = E0365)]
746+
pub struct PrivateExternCrateReexport {
747+
#[primary_span]
748+
pub(crate) span: Span,
749+
pub(crate) ident: Ident,
750+
#[suggestion(code = "pub ", style = "verbose", applicability = "maybe-incorrect")]
751+
pub(crate) sugg: Span,
752+
}
753+
744754
#[derive(Subdiagnostic)]
745755
#[help(resolve_consider_adding_macro_export)]
746756
pub(crate) struct ConsiderAddingMacroExport {

‎compiler/rustc_resolve/src/imports.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::errors::{
55
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
66
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
77
ConsiderAddingMacroExport, ConsiderMarkingAsPub, IsNotDirectlyImportable,
8-
ItemsInTraitsAreNotImportable,
8+
ItemsInTraitsAreNotImportable, PrivateExternCrateReexport,
99
};
1010
use crate::Determinacy::{self, *};
1111
use crate::{module_to_string, names_to_string, ImportSuggestion};
@@ -25,8 +25,7 @@ use rustc_middle::metadata::Reexport;
2525
use rustc_middle::span_bug;
2626
use rustc_middle::ty;
2727
use rustc_session::lint::builtin::{
28-
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
29-
UNUSED_IMPORTS,
28+
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, UNUSED_IMPORTS,
3029
};
3130
use rustc_session::lint::BuiltinLintDiag;
3231
use rustc_span::edit_distance::find_best_match_for_name;
@@ -1249,15 +1248,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12491248
if !any_successful_reexport {
12501249
let (ns, binding) = reexport_error.unwrap();
12511250
if let Some(extern_crate_id) = pub_use_of_private_extern_crate_hack(import, binding) {
1252-
self.lint_buffer.buffer_lint(
1253-
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
1254-
import_id,
1255-
import.span,
1256-
BuiltinLintDiag::PrivateExternCrateReexport {
1257-
source: ident,
1258-
extern_crate_span: self.tcx.source_span(self.local_def_id(extern_crate_id)),
1259-
},
1260-
);
1251+
let err = self.dcx().create_err(PrivateExternCrateReexport {
1252+
span: import.span,
1253+
ident,
1254+
sugg: self.tcx.source_span(self.local_def_id(extern_crate_id)).shrink_to_lo(),
1255+
});
1256+
err.emit();
12611257
} else {
12621258
if ns == TypeNS {
12631259
let err = if crate_private_reexport {

‎tests/ui/pub/pub-reexport-priv-extern-crate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
extern crate core;
22
pub use core as reexported_core; //~ ERROR `core` is private and cannot be re-exported
3-
//~^ WARN this was previously accepted
43

54
mod foo1 {
65
extern crate core;

‎tests/ui/pub/pub-reexport-priv-extern-crate.stderr

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1+
error[E0365]: extern crate `core` is private and cannot be re-exported
2+
--> $DIR/pub-reexport-priv-extern-crate.rs:2:9
3+
|
4+
LL | pub use core as reexported_core;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
help: consider making the `extern crate` item publicly accessible
8+
|
9+
LL | pub extern crate core;
10+
| +++
11+
112
error[E0603]: crate import `core` is private
2-
--> $DIR/pub-reexport-priv-extern-crate.rs:10:15
13+
--> $DIR/pub-reexport-priv-extern-crate.rs:9:15
314
|
415
LL | use foo1::core;
516
| ^^^^ private crate import
617
|
718
note: the crate import `core` is defined here
8-
--> $DIR/pub-reexport-priv-extern-crate.rs:6:5
19+
--> $DIR/pub-reexport-priv-extern-crate.rs:5:5
920
|
1021
LL | extern crate core;
1122
| ^^^^^^^^^^^^^^^^^^
1223

1324
error[E0603]: crate import `core` is private
14-
--> $DIR/pub-reexport-priv-extern-crate.rs:17:24
25+
--> $DIR/pub-reexport-priv-extern-crate.rs:16:24
1526
|
1627
LL | pub use foo2::bar::core;
1728
| ^^^^ private crate import
1829
|
1930
note: the crate import `core` is defined here
20-
--> $DIR/pub-reexport-priv-extern-crate.rs:12:9
31+
--> $DIR/pub-reexport-priv-extern-crate.rs:11:9
2132
|
2233
LL | extern crate core;
2334
| ^^^^^^^^^^^^^^^^^^
2435

25-
error[E0365]: extern crate `core` is private and cannot be re-exported
26-
--> $DIR/pub-reexport-priv-extern-crate.rs:2:9
27-
|
28-
LL | pub use core as reexported_core;
29-
| ^^^^^^^^^^^^^^^^^^^^^^^
30-
|
31-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
32-
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>
33-
= note: `#[deny(pub_use_of_private_extern_crate)]` on by default
34-
help: consider making the `extern crate` item publicly accessible
35-
|
36-
LL | pub extern crate core;
37-
| +++
38-
3936
error: aborting due to 3 previous errors
4037

4138
Some errors have detailed explanations: E0365, E0603.

0 commit comments

Comments
 (0)
Please sign in to comment.