Skip to content

Commit 801725a

Browse files
committed
suggest adding a #[macro_export] to a private macro
1 parent 083721a commit 801725a

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

compiler/rustc_resolve/src/imports.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::NodeId;
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_data_structures::intern::Interned;
1414
use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
15-
use rustc_hir::def::{self, PartialRes};
15+
use rustc_hir::def::{self, DefKind, PartialRes};
1616
use rustc_middle::metadata::ModChild;
1717
use rustc_middle::span_bug;
1818
use rustc_middle::ty;
@@ -922,11 +922,35 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
922922
.note(&format!("consider declaring type or module `{}` with `pub`", ident))
923923
.emit();
924924
} else {
925-
let note_msg =
926-
format!("consider marking `{}` as `pub` in the imported module", ident);
927-
struct_span_err!(self.r.session, import.span, E0364, "{}", error_msg)
928-
.span_note(import.span, &note_msg)
929-
.emit();
925+
let mut err =
926+
struct_span_err!(self.r.session, import.span, E0364, "{error_msg}");
927+
match binding.kind {
928+
NameBindingKind::Res(Res::Def(DefKind::Macro(_), _def_id), _)
929+
// exclude decl_macro
930+
if !self.r.session.features_untracked().decl_macro
931+
|| !self
932+
.r
933+
.session
934+
.source_map()
935+
.span_to_snippet(binding.span)
936+
.map(|snippet| snippet.starts_with("macro "))
937+
.unwrap_or(true) =>
938+
{
939+
err.span_help(
940+
binding.span,
941+
"consider adding a `#[macro_export]` to the macro in the imported module",
942+
);
943+
}
944+
_ => {
945+
err.span_note(
946+
import.span,
947+
&format!(
948+
"consider marking `{ident}` as `pub` in the imported module"
949+
),
950+
);
951+
}
952+
}
953+
err.emit();
930954
}
931955
}
932956
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// edition:2018
2+
3+
mod foo {
4+
macro_rules! bar {
5+
() => {};
6+
}
7+
8+
pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside
2+
--> $DIR/macro-private-reexport.rs:8:13
3+
|
4+
LL | pub use bar as _;
5+
| ^^^^^^^^
6+
|
7+
help: consider adding a `#[macro_export]` to the macro in the imported module
8+
--> $DIR/macro-private-reexport.rs:4:5
9+
|
10+
LL | / macro_rules! bar {
11+
LL | | () => {};
12+
LL | | }
13+
| |_____^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0364`.

src/test/ui/rust-2018/uniform-paths/macro-rules.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0364]: `legacy_macro` is only public within the crate, and cannot be re-e
44
LL | pub use legacy_macro as _;
55
| ^^^^^^^^^^^^^^^^^
66
|
7-
note: consider marking `legacy_macro` as `pub` in the imported module
8-
--> $DIR/macro-rules.rs:11:13
7+
help: consider adding a `#[macro_export]` to the macro in the imported module
8+
--> $DIR/macro-rules.rs:7:5
99
|
10-
LL | pub use legacy_macro as _;
11-
| ^^^^^^^^^^^^^^^^^
10+
LL | macro_rules! legacy_macro { () => () }
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error[E0659]: `legacy_macro` is ambiguous
1414
--> $DIR/macro-rules.rs:31:13

0 commit comments

Comments
 (0)