Skip to content

Commit

Permalink
Auto merge of #127656 - RalfJung:pub_use_of_private_extern_crate, r=
Browse files Browse the repository at this point in the history
WIP: make pub_use_of_private_extern_crate a hard error

This got tracked in #34537 which recently got closed so I assume we want this to be a hard error? It's been a lint for many years.

Should probably be cratered though.

TODO:
- actually remove the lint

r? `@petrochenkov`
  • Loading branch information
bors committed Jul 12, 2024
2 parents 05eac57 + c29d5a3 commit 0c9c19b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 50 deletions.
3 changes: 0 additions & 3 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,6 @@ lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
lint_pattern_in_foreign = patterns aren't allowed in foreign function declarations
.label = pattern not allowed in foreign function
lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
.suggestion = consider making the `extern crate` item publicly accessible
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}` in this scope
.label = names from parent modules are not accessible without an explicit import
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
lints::MacroUseDeprecated.decorate_lint(diag);
}
BuiltinLintDiag::UnusedMacroUse => lints::UnusedMacroUse.decorate_lint(diag),
BuiltinLintDiag::PrivateExternCrateReexport { source: ident, extern_crate_span } => {
lints::PrivateExternCrateReexport { ident, sugg: extern_crate_span.shrink_to_lo() }
.decorate_lint(diag);
}
BuiltinLintDiag::UnusedLabel => lints::UnusedLabel.decorate_lint(diag),
BuiltinLintDiag::MacroIsPrivate(ident) => {
lints::MacroIsPrivate { ident }.decorate_lint(diag);
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,14 +2331,6 @@ pub struct MacroUseDeprecated;
#[diag(lint_unused_macro_use)]
pub struct UnusedMacroUse;

#[derive(LintDiagnostic)]
#[diag(lint_private_extern_crate_reexport, code = E0365)]
pub struct PrivateExternCrateReexport {
pub ident: Ident,
#[suggestion(code = "pub ", style = "verbose", applicability = "maybe-incorrect")]
pub sugg: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_unused_label)]
pub struct UnusedLabel;
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,6 @@ pub enum BuiltinLintDiag {
},
MacroUseDeprecated,
UnusedMacroUse,
PrivateExternCrateReexport {
source: Ident,
extern_crate_span: Span,
},
UnusedLabel,
MacroIsPrivate(Ident),
UnusedMacroDefinition(Symbol),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ resolve_param_in_ty_of_const_param =
resolve_pattern_doesnt_bind_name = pattern doesn't bind `{$name}`
resolve_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
.suggestion = consider making the `extern crate` item publicly accessible
resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it
.help = you can define integration tests in a directory named `tests`
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ pub(crate) struct CannotBeReexportedCratePublicNS {
pub(crate) ident: Ident,
}

#[derive(Diagnostic)]
#[diag(resolve_private_extern_crate_reexport, code = E0365)]
pub struct PrivateExternCrateReexport {
#[primary_span]
pub(crate) span: Span,
pub(crate) ident: Ident,
#[suggestion(code = "pub ", style = "verbose", applicability = "maybe-incorrect")]
pub(crate) sugg: Span,
}

#[derive(Subdiagnostic)]
#[help(resolve_consider_adding_macro_export)]
pub(crate) struct ConsiderAddingMacroExport {
Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::{
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
ConsiderAddingMacroExport, ConsiderMarkingAsPub, IsNotDirectlyImportable,
ItemsInTraitsAreNotImportable,
ItemsInTraitsAreNotImportable, PrivateExternCrateReexport,
};
use crate::Determinacy::{self, *};
use crate::{module_to_string, names_to_string, ImportSuggestion};
Expand All @@ -25,8 +25,7 @@ use rustc_middle::metadata::Reexport;
use rustc_middle::span_bug;
use rustc_middle::ty;
use rustc_session::lint::builtin::{
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE,
UNUSED_IMPORTS,
AMBIGUOUS_GLOB_REEXPORTS, HIDDEN_GLOB_REEXPORTS, UNUSED_IMPORTS,
};
use rustc_session::lint::BuiltinLintDiag;
use rustc_span::edit_distance::find_best_match_for_name;
Expand Down Expand Up @@ -1249,15 +1248,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !any_successful_reexport {
let (ns, binding) = reexport_error.unwrap();
if let Some(extern_crate_id) = pub_use_of_private_extern_crate_hack(import, binding) {
self.lint_buffer.buffer_lint(
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
import_id,
import.span,
BuiltinLintDiag::PrivateExternCrateReexport {
source: ident,
extern_crate_span: self.tcx.source_span(self.local_def_id(extern_crate_id)),
},
);
let err = self.dcx().create_err(PrivateExternCrateReexport {
span: import.span,
ident,
sugg: self.tcx.source_span(self.local_def_id(extern_crate_id)).shrink_to_lo(),
});
err.emit();
} else {
if ns == TypeNS {
let err = if crate_private_reexport {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/pub/pub-reexport-priv-extern-crate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate core;
pub use core as reexported_core; //~ ERROR `core` is private and cannot be re-exported
//~^ WARN this was previously accepted

mod foo1 {
extern crate core;
Expand Down
33 changes: 15 additions & 18 deletions tests/ui/pub/pub-reexport-priv-extern-crate.stderr
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
error[E0365]: extern crate `core` is private and cannot be re-exported
--> $DIR/pub-reexport-priv-extern-crate.rs:2:9
|
LL | pub use core as reexported_core;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider making the `extern crate` item publicly accessible
|
LL | pub extern crate core;
| +++

error[E0603]: crate import `core` is private
--> $DIR/pub-reexport-priv-extern-crate.rs:10:15
--> $DIR/pub-reexport-priv-extern-crate.rs:9:15
|
LL | use foo1::core;
| ^^^^ private crate import
|
note: the crate import `core` is defined here
--> $DIR/pub-reexport-priv-extern-crate.rs:6:5
--> $DIR/pub-reexport-priv-extern-crate.rs:5:5
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^

error[E0603]: crate import `core` is private
--> $DIR/pub-reexport-priv-extern-crate.rs:17:24
--> $DIR/pub-reexport-priv-extern-crate.rs:16:24
|
LL | pub use foo2::bar::core;
| ^^^^ private crate import
|
note: the crate import `core` is defined here
--> $DIR/pub-reexport-priv-extern-crate.rs:12:9
--> $DIR/pub-reexport-priv-extern-crate.rs:11:9
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^

error[E0365]: extern crate `core` is private and cannot be re-exported
--> $DIR/pub-reexport-priv-extern-crate.rs:2:9
|
LL | pub use core as reexported_core;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>
= note: `#[deny(pub_use_of_private_extern_crate)]` on by default
help: consider making the `extern crate` item publicly accessible
|
LL | pub extern crate core;
| +++

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0365, E0603.
Expand Down

0 comments on commit 0c9c19b

Please sign in to comment.