Skip to content

Commit 179ce18

Browse files
committed
resolve/metadata: Stop encoding macros as reexports
1 parent 50568b8 commit 179ce18

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10771077
res,
10781078
vis: ty::Visibility::Public,
10791079
span: ident.span,
1080+
macro_rules: false,
10801081
});
10811082
}
10821083
}
@@ -1088,17 +1089,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10881089
for child_index in children.decode((self, sess)) {
10891090
if let Some(ident) = self.opt_item_ident(child_index, sess) {
10901091
let kind = self.def_kind(child_index);
1091-
if matches!(kind, DefKind::Macro(..)) {
1092-
// FIXME: Macros are currently encoded twice, once as items and once as
1093-
// reexports. We ignore the items here and only use the reexports.
1094-
continue;
1095-
}
10961092
let def_id = self.local_def_id(child_index);
10971093
let res = Res::Def(kind, def_id);
10981094
let vis = self.get_visibility(child_index);
10991095
let span = self.get_span(child_index, sess);
1096+
let macro_rules = match kind {
1097+
DefKind::Macro(..) => match self.kind(child_index) {
1098+
EntryKind::MacroDef(_, macro_rules) => macro_rules,
1099+
_ => unreachable!(),
1100+
},
1101+
_ => false,
1102+
};
11001103

1101-
callback(ModChild { ident, res, vis, span });
1104+
callback(ModChild { ident, res, vis, span, macro_rules });
11021105

11031106
// For non-re-export structs and variants add their constructors to children.
11041107
// Re-export lists automatically contain constructors when necessary.
@@ -1110,7 +1113,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11101113
let ctor_res =
11111114
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
11121115
let vis = self.get_visibility(ctor_def_id.index);
1113-
callback(ModChild { ident, res: ctor_res, vis, span });
1116+
callback(ModChild {
1117+
ident,
1118+
res: ctor_res,
1119+
vis,
1120+
span,
1121+
macro_rules: false,
1122+
});
11141123
}
11151124
}
11161125
DefKind::Variant => {
@@ -1135,7 +1144,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11351144
vis = ty::Visibility::Restricted(crate_def_id);
11361145
}
11371146
}
1138-
callback(ModChild { ident, res: ctor_res, vis, span });
1147+
callback(ModChild {
1148+
ident,
1149+
res: ctor_res,
1150+
vis,
1151+
span,
1152+
macro_rules: false,
1153+
});
11391154
}
11401155
_ => {}
11411156
}

compiler/rustc_middle/src/metadata.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ pub struct ModChild {
2121
pub vis: ty::Visibility,
2222
/// Span of the item.
2323
pub span: Span,
24+
/// A proper `macro_rules` item (not a reexport).
25+
pub macro_rules: bool,
2426
}

compiler/rustc_resolve/src/access_levels.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
133133
ast::ItemKind::Impl(..) => return,
134134

135135
// Only exported `macro_rules!` items are public, but they always are
136-
ast::ItemKind::MacroDef(..) => {
136+
ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
137137
let is_macro_export =
138138
item.attrs.iter().any(|attr| attr.has_name(sym::macro_export));
139139
if is_macro_export { Some(AccessLevel::Public) } else { None }
@@ -155,7 +155,8 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
155155
| ast::ItemKind::Struct(..)
156156
| ast::ItemKind::Union(..)
157157
| ast::ItemKind::Trait(..)
158-
| ast::ItemKind::TraitAlias(..) => {
158+
| ast::ItemKind::TraitAlias(..)
159+
| ast::ItemKind::MacroDef(..) => {
159160
if item.vis.kind.is_pub() {
160161
self.prev_level
161162
} else {

compiler/rustc_resolve/src/build_reduced_graph.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
940940
/// Builds the reduced graph for a single item in an external crate.
941941
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
942942
let parent = self.parent_scope.module;
943-
let ModChild { ident, res, vis, span } = child;
943+
let ModChild { ident, res, vis, span, macro_rules } = child;
944944
let res = res.expect_non_local();
945945
let expansion = self.parent_scope.expansion;
946946
// Record primary definitions.
@@ -972,7 +972,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
972972
_,
973973
) => self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)),
974974
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
975-
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
975+
if !macro_rules {
976+
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
977+
}
976978
}
977979
Res::Def(
978980
DefKind::TyParam

compiler/rustc_resolve/src/imports.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1399,14 +1399,22 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13991399
let mut reexports = Vec::new();
14001400

14011401
module.for_each_child(self.r, |_, ident, _, binding| {
1402-
// Filter away ambiguous imports and anything that has def-site hygiene.
1403-
// FIXME: Implement actual cross-crate hygiene.
1404-
let is_good_import =
1405-
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
1406-
if is_good_import || binding.is_macro_def() {
1402+
// FIXME: Consider changing the binding inserted by `#[macro_export] macro_rules`
1403+
// into the crate root to actual `NameBindingKind::Import`.
1404+
if binding.is_import()
1405+
|| matches!(binding.kind, NameBindingKind::Res(_, _is_macro_export @ true))
1406+
{
14071407
let res = binding.res().expect_non_local();
1408-
if res != def::Res::Err {
1409-
reexports.push(ModChild { ident, res, vis: binding.vis, span: binding.span });
1408+
// Ambiguous imports are treated as errors at this point and are
1409+
// not exposed to other crates (see #36837 for more details).
1410+
if res != def::Res::Err && !binding.is_ambiguity() {
1411+
reexports.push(ModChild {
1412+
ident,
1413+
res,
1414+
vis: binding.vis,
1415+
span: binding.span,
1416+
macro_rules: false,
1417+
});
14101418
}
14111419
}
14121420
});

compiler/rustc_resolve/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,6 @@ impl<'a> NameBinding<'a> {
845845
)
846846
}
847847

848-
fn is_macro_def(&self) -> bool {
849-
matches!(self.kind, NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _))
850-
}
851-
852848
fn macro_kind(&self) -> Option<MacroKind> {
853849
self.res().macro_kind()
854850
}

0 commit comments

Comments
 (0)