diff options
| author | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-06-14 14:58:46 +0900 |
|---|---|---|
| committer | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-06-14 14:58:46 +0900 |
| commit | 801725a77b17a0641fcf40a310f491d71b059d80 (patch) | |
| tree | a0fb1adcf9c110f3ff2db5fbf8198b3bdf6d3d4e | |
| parent | 083721a1a7365d3afe1521cd2661b2201aac0450 (diff) | |
| download | rust-801725a77b17a0641fcf40a310f491d71b059d80.tar.gz rust-801725a77b17a0641fcf40a310f491d71b059d80.zip | |
suggest adding a `#[macro_export]` to a private macro
| -rw-r--r-- | compiler/rustc_resolve/src/imports.rs | 36 | ||||
| -rw-r--r-- | src/test/ui/privacy/macro-private-reexport.rs | 11 | ||||
| -rw-r--r-- | src/test/ui/privacy/macro-private-reexport.stderr | 17 | ||||
| -rw-r--r-- | src/test/ui/rust-2018/uniform-paths/macro-rules.stderr | 8 |
4 files changed, 62 insertions, 10 deletions
diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index de83a3a5932..a457169c74b 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -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, ¬e_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(); } } } diff --git a/src/test/ui/privacy/macro-private-reexport.rs b/src/test/ui/privacy/macro-private-reexport.rs new file mode 100644 index 00000000000..5b53a861d25 --- /dev/null +++ b/src/test/ui/privacy/macro-private-reexport.rs @@ -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() {} diff --git a/src/test/ui/privacy/macro-private-reexport.stderr b/src/test/ui/privacy/macro-private-reexport.stderr new file mode 100644 index 00000000000..af85cbcf3f2 --- /dev/null +++ b/src/test/ui/privacy/macro-private-reexport.stderr @@ -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`. diff --git a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr index 9e48e26b1df..9f8c928c32c 100644 --- a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr +++ b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr @@ -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 |
