Skip to content

Commit 8568070

Browse files
authored
Rollup merge of #145166 - makai410:teach-pub-crate, r=lcnr
suggest using `pub(crate)` for E0364 - This introduces `vis_span` into `ImportData` for diagnostic purposes. Closes: #145140
2 parents bdd3bc8 + 0c8485f commit 8568070

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+
in case 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);
@@ -966,6 +967,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
966967
span: item.span,
967968
module_path: Vec::new(),
968969
vis,
970+
vis_span: item.vis.span,
969971
});
970972
if used {
971973
self.r.import_use_map.insert(import, Used::Other);
@@ -1100,6 +1102,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11001102
span,
11011103
module_path: Vec::new(),
11021104
vis: Visibility::Restricted(CRATE_DEF_ID),
1105+
vis_span: item.vis.span,
11031106
})
11041107
};
11051108

@@ -1270,6 +1273,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12701273
span,
12711274
module_path: Vec::new(),
12721275
vis,
1276+
vis_span: item.vis.span,
12731277
});
12741278
self.r.import_use_map.insert(import, Used::Other);
12751279
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
@@ -775,6 +775,17 @@ pub(crate) struct ConsiderAddingMacroExport {
775775
pub(crate) span: Span,
776776
}
777777

778+
#[derive(Subdiagnostic)]
779+
#[suggestion(
780+
resolve_consider_marking_as_pub_crate,
781+
code = "pub(crate)",
782+
applicability = "maybe-incorrect"
783+
)]
784+
pub(crate) struct ConsiderMarkingAsPubCrate {
785+
#[primary_span]
786+
pub(crate) vis_span: Span,
787+
}
788+
778789
#[derive(Subdiagnostic)]
779790
#[note(resolve_consider_marking_as_pub)]
780791
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,
@@ -184,6 +184,9 @@ pub(crate) struct ImportData<'ra> {
184184
/// |`use foo` | `ModuleOrUniformRoot::CurrentScope` | - |
185185
pub imported_module: Cell<Option<ModuleOrUniformRoot<'ra>>>,
186186
pub vis: Visibility,
187+
188+
/// Span of the visibility.
189+
pub vis_span: Span,
187190
}
188191

189192
/// All imports are unique and allocated on a same arena,
@@ -1368,6 +1371,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13681371
err.subdiagnostic( ConsiderAddingMacroExport {
13691372
span: binding.span,
13701373
});
1374+
err.subdiagnostic( ConsiderMarkingAsPubCrate {
1375+
vis_span: import.vis_span,
1376+
});
13711377
}
13721378
_ => {
13731379
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: in case 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: in case 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)