suggest adding a #[macro_export] to a private macro

This commit is contained in:
Takayuki Maeda 2022-06-14 14:58:46 +09:00
parent 083721a1a7
commit 801725a77b
4 changed files with 62 additions and 10 deletions

View File

@ -12,7 +12,7 @@ use rustc_ast::NodeId;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned;
use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
use rustc_hir::def::{self, PartialRes};
use rustc_hir::def::{self, DefKind, PartialRes};
use rustc_middle::metadata::ModChild;
use rustc_middle::span_bug;
use rustc_middle::ty;
@ -922,11 +922,35 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
.note(&format!("consider declaring type or module `{}` with `pub`", ident))
.emit();
} else {
let note_msg =
format!("consider marking `{}` as `pub` in the imported module", ident);
struct_span_err!(self.r.session, import.span, E0364, "{}", error_msg)
.span_note(import.span, &note_msg)
.emit();
let mut err =
struct_span_err!(self.r.session, import.span, E0364, "{error_msg}");
match binding.kind {
NameBindingKind::Res(Res::Def(DefKind::Macro(_), _def_id), _)
// exclude decl_macro
if !self.r.session.features_untracked().decl_macro
|| !self
.r
.session
.source_map()
.span_to_snippet(binding.span)
.map(|snippet| snippet.starts_with("macro "))
.unwrap_or(true) =>
{
err.span_help(
binding.span,
"consider adding a `#[macro_export]` to the macro in the imported module",
);
}
_ => {
err.span_note(
import.span,
&format!(
"consider marking `{ident}` as `pub` in the imported module"
),
);
}
}
err.emit();
}
}
}

View File

@ -0,0 +1,11 @@
// edition:2018
mod foo {
macro_rules! bar {
() => {};
}
pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside
}
fn main() {}

View File

@ -0,0 +1,17 @@
error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside
--> $DIR/macro-private-reexport.rs:8:13
|
LL | pub use bar as _;
| ^^^^^^^^
|
help: consider adding a `#[macro_export]` to the macro in the imported module
--> $DIR/macro-private-reexport.rs:4:5
|
LL | / macro_rules! bar {
LL | | () => {};
LL | | }
| |_____^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0364`.

View File

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