Skip to content

suggest using pub(crate) for E0364 #145166

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 1 commit 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
3 changes: 3 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ resolve_consider_adding_a_derive =
resolve_consider_adding_macro_export =
consider adding a `#[macro_export]` to the macro in the imported module

resolve_consider_marking_as_pub_crate =
in case you want to use the macro within this crate only, reduce the visibility to `pub(crate)`

resolve_consider_declaring_with_pub =
consider declaring type or module `{$ident}` with `pub`

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
root_span,
root_id,
vis,
vis_span: item.vis.span,
});

self.r.indeterminate_imports.push(import);
Expand Down Expand Up @@ -966,6 +967,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
span: item.span,
module_path: Vec::new(),
vis,
vis_span: item.vis.span,
});
if used {
self.r.import_use_map.insert(import, Used::Other);
Expand Down Expand Up @@ -1105,6 +1107,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
span,
module_path: Vec::new(),
vis: Visibility::Restricted(CRATE_DEF_ID),
vis_span: item.vis.span,
})
};

Expand Down Expand Up @@ -1274,6 +1277,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
span,
module_path: Vec::new(),
vis,
vis_span: item.vis.span,
});
self.r.import_use_map.insert(import, Used::Other);
let import_binding = self.r.import(binding, import);
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,17 @@ pub(crate) struct ConsiderAddingMacroExport {
pub(crate) span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
resolve_consider_marking_as_pub_crate,
code = "pub(crate)",
applicability = "maybe-incorrect"
)]
pub(crate) struct ConsiderMarkingAsPubCrate {
#[primary_span]
pub(crate) vis_span: Span,
}

#[derive(Subdiagnostic)]
#[note(resolve_consider_marking_as_pub)]
pub(crate) struct ConsiderMarkingAsPub {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::diagnostics::{DiagMode, Suggestion, import_candidates};
use crate::errors::{
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
ConsiderAddingMacroExport, ConsiderMarkingAsPub,
ConsiderAddingMacroExport, ConsiderMarkingAsPub, ConsiderMarkingAsPubCrate,
};
use crate::{
AmbiguityError, AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion,
Expand Down Expand Up @@ -188,6 +188,9 @@ pub(crate) struct ImportData<'ra> {
/// |`use foo` | `ModuleOrUniformRoot::CurrentScope` | - |
pub imported_module: Cell<Option<ModuleOrUniformRoot<'ra>>>,
pub vis: Visibility,

/// Span of the visibility.
pub vis_span: Span,
}

/// All imports are unique and allocated on a same arena,
Expand Down Expand Up @@ -1373,6 +1376,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
err.subdiagnostic( ConsiderAddingMacroExport {
span: binding.span,
});
err.subdiagnostic( ConsiderMarkingAsPubCrate {
vis_span: import.vis_span,
});
}
_ => {
err.subdiagnostic( ConsiderMarkingAsPub {
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/privacy/macro-private-reexport.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ LL | / macro_rules! bar {
LL | | () => {};
LL | | }
| |_____^
help: in case you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
|
LL | pub(crate) use bar as _;
| +++++++

error[E0364]: `baz` is private, and cannot be re-exported
--> $DIR/macro-private-reexport.rs:14:13
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/rust-2018/uniform-paths/macro-rules.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ help: consider adding a `#[macro_export]` to the macro in the imported module
|
LL | macro_rules! legacy_macro { () => () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: in case you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
|
LL | pub(crate) use legacy_macro as _;
| +++++++

error: aborting due to 1 previous error

Expand Down
Loading