Skip to content

Commit b2d0e78

Browse files
authored
Rollup merge of rust-lang#98087 - TaKO8Ki:suggest-adding-macro-export, r=oli-obk
Suggest adding a `#[macro_export]` to a private macro fixes rust-lang#97628
2 parents 97b9347 + d29915a commit b2d0e78

File tree

8 files changed

+105
-29
lines changed

8 files changed

+105
-29
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use crate::imports::{Import, ImportKind};
1010
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
1111
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
1212
use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot};
13-
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
13+
use crate::{
14+
MacroData, NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError,
15+
};
1416
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
1517

1618
use rustc_ast::visit::{self, AssocCtxt, Visitor};
@@ -20,7 +22,6 @@ use rustc_ast_lowering::ResolverAstLowering;
2022
use rustc_attr as attr;
2123
use rustc_data_structures::sync::Lrc;
2224
use rustc_errors::{struct_span_err, Applicability};
23-
use rustc_expand::base::SyntaxExtension;
2425
use rustc_expand::expand::AstFragment;
2526
use rustc_hir::def::{self, *};
2627
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
@@ -180,26 +181,32 @@ impl<'a> Resolver<'a> {
180181
}
181182
}
182183

183-
pub(crate) fn get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
184+
pub(crate) fn get_macro(&mut self, res: Res) -> Option<MacroData> {
184185
match res {
185186
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
186-
Res::NonMacroAttr(_) => Some(self.non_macro_attr.clone()),
187+
Res::NonMacroAttr(_) => {
188+
Some(MacroData { ext: self.non_macro_attr.clone(), macro_rules: false })
189+
}
187190
_ => None,
188191
}
189192
}
190193

191-
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> Lrc<SyntaxExtension> {
192-
if let Some(ext) = self.macro_map.get(&def_id) {
193-
return ext.clone();
194+
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> MacroData {
195+
if let Some(macro_data) = self.macro_map.get(&def_id) {
196+
return macro_data.clone();
194197
}
195198

196-
let ext = Lrc::new(match self.cstore().load_macro_untracked(def_id, &self.session) {
197-
LoadedMacro::MacroDef(item, edition) => self.compile_macro(&item, edition).0,
198-
LoadedMacro::ProcMacro(ext) => ext,
199-
});
199+
let (ext, macro_rules) = match self.cstore().load_macro_untracked(def_id, &self.session) {
200+
LoadedMacro::MacroDef(item, edition) => (
201+
Lrc::new(self.compile_macro(&item, edition).0),
202+
matches!(item.kind, ItemKind::MacroDef(def) if def.macro_rules),
203+
),
204+
LoadedMacro::ProcMacro(extz) => (Lrc::new(extz), false),
205+
};
200206

201-
self.macro_map.insert(def_id, ext.clone());
202-
ext
207+
let macro_data = MacroData { ext, macro_rules };
208+
self.macro_map.insert(def_id, macro_data.clone());
209+
macro_data
203210
}
204211

205212
pub(crate) fn build_reduced_graph(
@@ -1251,7 +1258,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
12511258
};
12521259

12531260
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id());
1254-
self.r.macro_map.insert(def_id.to_def_id(), ext);
1261+
self.r.macro_map.insert(def_id.to_def_id(), MacroData { ext, macro_rules });
12551262
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12561263

12571264
if macro_rules {

compiler/rustc_resolve/src/ident.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<'a> Resolver<'a> {
241241
{
242242
// The macro is a proc macro derive
243243
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
244-
let ext = self.get_macro_by_def_id(def_id);
244+
let ext = self.get_macro_by_def_id(def_id).ext;
245245
if ext.builtin_name.is_none()
246246
&& ext.macro_kind() == MacroKind::Derive
247247
&& parent.expansion.outer_expn_is_descendant_of(*ctxt)

compiler/rustc_resolve/src/imports.rs

+23-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,28 @@ 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.get_macro_by_def_id(def_id).macro_rules =>
931+
{
932+
err.span_help(
933+
binding.span,
934+
"consider adding a `#[macro_export]` to the macro in the imported module",
935+
);
936+
}
937+
_ => {
938+
err.span_note(
939+
import.span,
940+
&format!(
941+
"consider marking `{ident}` as `pub` in the imported module"
942+
),
943+
);
944+
}
945+
}
946+
err.emit();
930947
}
931948
}
932949
}

compiler/rustc_resolve/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,12 @@ struct DeriveData {
866866
has_derive_copy: bool,
867867
}
868868

869+
#[derive(Clone)]
870+
struct MacroData {
871+
ext: Lrc<SyntaxExtension>,
872+
macro_rules: bool,
873+
}
874+
869875
/// The main resolver class.
870876
///
871877
/// This is the visitor that walks the whole crate.
@@ -965,7 +971,7 @@ pub struct Resolver<'a> {
965971
registered_attrs: FxHashSet<Ident>,
966972
registered_tools: RegisteredTools,
967973
macro_use_prelude: FxHashMap<Symbol, &'a NameBinding<'a>>,
968-
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
974+
macro_map: FxHashMap<DefId, MacroData>,
969975
dummy_ext_bang: Lrc<SyntaxExtension>,
970976
dummy_ext_derive: Lrc<SyntaxExtension>,
971977
non_macro_attr: Lrc<SyntaxExtension>,
@@ -1522,7 +1528,7 @@ impl<'a> Resolver<'a> {
15221528
}
15231529

15241530
fn is_builtin_macro(&mut self, res: Res) -> bool {
1525-
self.get_macro(res).map_or(false, |ext| ext.builtin_name.is_some())
1531+
self.get_macro(res).map_or(false, |macro_data| macro_data.ext.builtin_name.is_some())
15261532
}
15271533

15281534
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {

compiler/rustc_resolve/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ impl<'a> Resolver<'a> {
658658
res
659659
};
660660

661-
res.map(|res| (self.get_macro(res), res))
661+
res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext), res))
662662
}
663663

664664
pub(crate) fn finalize_macro_resolutions(&mut self) {
@@ -853,7 +853,7 @@ impl<'a> Resolver<'a> {
853853
// Reserve some names that are not quite covered by the general check
854854
// performed on `Resolver::builtin_attrs`.
855855
if ident.name == sym::cfg || ident.name == sym::cfg_attr {
856-
let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind());
856+
let macro_kind = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kind());
857857
if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
858858
self.session.span_err(
859859
ident.span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2021
2+
3+
#![feature(decl_macro)]
4+
5+
mod foo {
6+
macro_rules! bar {
7+
() => {};
8+
}
9+
10+
pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside
11+
12+
macro baz() {}
13+
14+
pub use baz as _; //~ ERROR `baz` is private, and cannot be re-exported
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside
2+
--> $DIR/macro-private-reexport.rs:10: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:6:5
9+
|
10+
LL | / macro_rules! bar {
11+
LL | | () => {};
12+
LL | | }
13+
| |_____^
14+
15+
error[E0364]: `baz` is private, and cannot be re-exported
16+
--> $DIR/macro-private-reexport.rs:14:13
17+
|
18+
LL | pub use baz as _;
19+
| ^^^^^^^^
20+
|
21+
note: consider marking `baz` as `pub` in the imported module
22+
--> $DIR/macro-private-reexport.rs:14:13
23+
|
24+
LL | pub use baz as _;
25+
| ^^^^^^^^
26+
27+
error: aborting due to 2 previous errors
28+
29+
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)