Skip to content

Commit d9d91c9

Browse files
Auto merge of #145166 - makai410:teach-pub-crate, r=<try>
suggest using `pub(crate)` for E0364
2 parents b1b26b8 + f3d1425 commit d9d91c9

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ resolve_consider_adding_a_derive =
9393
resolve_consider_adding_macro_export =
9494
consider adding a `#[macro_export]` to the macro in the imported module
9595
96+
resolve_consider_marking_as_pub_crate =
97+
or if you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
98+
9699
resolve_consider_declaring_with_pub =
97100
consider declaring type or module `{$ident}` with `pub`
98101

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
474474
root_span,
475475
root_id,
476476
vis,
477+
vis_span: item.vis.span,
477478
});
478479

479480
self.r.indeterminate_imports.push(import);
@@ -963,6 +964,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
963964
span: item.span,
964965
module_path: Vec::new(),
965966
vis,
967+
vis_span: item.vis.span,
966968
});
967969
if used {
968970
self.r.import_use_map.insert(import, Used::Other);
@@ -1102,6 +1104,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11021104
span,
11031105
module_path: Vec::new(),
11041106
vis: Visibility::Restricted(CRATE_DEF_ID),
1107+
vis_span: item.vis.span,
11051108
})
11061109
};
11071110

@@ -1271,6 +1274,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12711274
span,
12721275
module_path: Vec::new(),
12731276
vis,
1277+
vis_span: item.vis.span,
12741278
});
12751279
self.r.import_use_map.insert(import, Used::Other);
12761280
let import_binding = self.r.import(binding, import);

compiler/rustc_resolve/src/errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,17 @@ pub(crate) struct ConsiderAddingMacroExport {
769769
pub(crate) span: Span,
770770
}
771771

772+
#[derive(Subdiagnostic)]
773+
#[suggestion(
774+
resolve_consider_marking_as_pub_crate,
775+
code = "pub(crate)",
776+
applicability = "maybe-incorrect"
777+
)]
778+
pub(crate) struct ConsiderMarkingAsPubCrate {
779+
#[primary_span]
780+
pub(crate) vis_span: Span,
781+
}
782+
772783
#[derive(Subdiagnostic)]
773784
#[note(resolve_consider_marking_as_pub)]
774785
pub(crate) struct ConsiderMarkingAsPub {

compiler/rustc_resolve/src/imports.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::diagnostics::{DiagMode, Suggestion, import_candidates};
3030
use crate::errors::{
3131
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
3232
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
33-
ConsiderAddingMacroExport, ConsiderMarkingAsPub,
33+
ConsiderAddingMacroExport, ConsiderMarkingAsPub, ConsiderMarkingAsPubCrate,
3434
};
3535
use crate::{
3636
AmbiguityError, AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion,
@@ -188,6 +188,9 @@ pub(crate) struct ImportData<'ra> {
188188
/// |`use foo` | `ModuleOrUniformRoot::CurrentScope` | - |
189189
pub imported_module: Cell<Option<ModuleOrUniformRoot<'ra>>>,
190190
pub vis: Visibility,
191+
192+
/// Span of the visibility.
193+
pub vis_span: Span,
191194
}
192195

193196
/// All imports are unique and allocated on a same arena,
@@ -1373,6 +1376,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13731376
err.subdiagnostic( ConsiderAddingMacroExport {
13741377
span: binding.span,
13751378
});
1379+
err.subdiagnostic( ConsiderMarkingAsPubCrate {
1380+
vis_span: import.vis_span,
1381+
});
13761382
}
13771383
_ => {
13781384
err.subdiagnostic( ConsiderMarkingAsPub {

tests/ui/privacy/macro-private-reexport.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ LL | / macro_rules! bar {
1111
LL | | () => {};
1212
LL | | }
1313
| |_____^
14+
help: or if you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
15+
|
16+
LL | pub(crate) use bar as _;
17+
| +++++++
1418

1519
error[E0364]: `baz` is private, and cannot be re-exported
1620
--> $DIR/macro-private-reexport.rs:14:13

tests/ui/rust-2018/uniform-paths/macro-rules.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ help: consider adding a `#[macro_export]` to the macro in the imported module
99
|
1010
LL | macro_rules! legacy_macro { () => () }
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
help: or if you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
13+
|
14+
LL | pub(crate) use legacy_macro as _;
15+
| +++++++
1216

1317
error: aborting due to 1 previous error
1418

0 commit comments

Comments
 (0)