提交 179ce18c 编写于 作者: V Vadim Petrochenkov

resolve/metadata: Stop encoding macros as reexports

上级 50568b8e
...@@ -1077,6 +1077,7 @@ fn for_each_module_child( ...@@ -1077,6 +1077,7 @@ fn for_each_module_child(
res, res,
vis: ty::Visibility::Public, vis: ty::Visibility::Public,
span: ident.span, span: ident.span,
macro_rules: false,
}); });
} }
} }
...@@ -1088,17 +1089,19 @@ fn for_each_module_child( ...@@ -1088,17 +1089,19 @@ fn for_each_module_child(
for child_index in children.decode((self, sess)) { for child_index in children.decode((self, sess)) {
if let Some(ident) = self.opt_item_ident(child_index, sess) { if let Some(ident) = self.opt_item_ident(child_index, sess) {
let kind = self.def_kind(child_index); let kind = self.def_kind(child_index);
if matches!(kind, DefKind::Macro(..)) {
// FIXME: Macros are currently encoded twice, once as items and once as
// reexports. We ignore the items here and only use the reexports.
continue;
}
let def_id = self.local_def_id(child_index); let def_id = self.local_def_id(child_index);
let res = Res::Def(kind, def_id); let res = Res::Def(kind, def_id);
let vis = self.get_visibility(child_index); let vis = self.get_visibility(child_index);
let span = self.get_span(child_index, sess); let span = self.get_span(child_index, sess);
let macro_rules = match kind {
DefKind::Macro(..) => match self.kind(child_index) {
EntryKind::MacroDef(_, macro_rules) => macro_rules,
_ => unreachable!(),
},
_ => false,
};
callback(ModChild { ident, res, vis, span }); callback(ModChild { ident, res, vis, span, macro_rules });
// For non-re-export structs and variants add their constructors to children. // For non-re-export structs and variants add their constructors to children.
// Re-export lists automatically contain constructors when necessary. // Re-export lists automatically contain constructors when necessary.
...@@ -1110,7 +1113,13 @@ fn for_each_module_child( ...@@ -1110,7 +1113,13 @@ fn for_each_module_child(
let ctor_res = let ctor_res =
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let vis = self.get_visibility(ctor_def_id.index); let vis = self.get_visibility(ctor_def_id.index);
callback(ModChild { ident, res: ctor_res, vis, span }); callback(ModChild {
ident,
res: ctor_res,
vis,
span,
macro_rules: false,
});
} }
} }
DefKind::Variant => { DefKind::Variant => {
...@@ -1135,7 +1144,13 @@ fn for_each_module_child( ...@@ -1135,7 +1144,13 @@ fn for_each_module_child(
vis = ty::Visibility::Restricted(crate_def_id); vis = ty::Visibility::Restricted(crate_def_id);
} }
} }
callback(ModChild { ident, res: ctor_res, vis, span }); callback(ModChild {
ident,
res: ctor_res,
vis,
span,
macro_rules: false,
});
} }
_ => {} _ => {}
} }
......
...@@ -21,4 +21,6 @@ pub struct ModChild { ...@@ -21,4 +21,6 @@ pub struct ModChild {
pub vis: ty::Visibility, pub vis: ty::Visibility,
/// Span of the item. /// Span of the item.
pub span: Span, pub span: Span,
/// A proper `macro_rules` item (not a reexport).
pub macro_rules: bool,
} }
...@@ -133,7 +133,7 @@ fn visit_item(&mut self, item: &'ast ast::Item) { ...@@ -133,7 +133,7 @@ fn visit_item(&mut self, item: &'ast ast::Item) {
ast::ItemKind::Impl(..) => return, ast::ItemKind::Impl(..) => return,
// Only exported `macro_rules!` items are public, but they always are // Only exported `macro_rules!` items are public, but they always are
ast::ItemKind::MacroDef(..) => { ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
let is_macro_export = let is_macro_export =
item.attrs.iter().any(|attr| attr.has_name(sym::macro_export)); item.attrs.iter().any(|attr| attr.has_name(sym::macro_export));
if is_macro_export { Some(AccessLevel::Public) } else { None } if is_macro_export { Some(AccessLevel::Public) } else { None }
...@@ -155,7 +155,8 @@ fn visit_item(&mut self, item: &'ast ast::Item) { ...@@ -155,7 +155,8 @@ fn visit_item(&mut self, item: &'ast ast::Item) {
| ast::ItemKind::Struct(..) | ast::ItemKind::Struct(..)
| ast::ItemKind::Union(..) | ast::ItemKind::Union(..)
| ast::ItemKind::Trait(..) | ast::ItemKind::Trait(..)
| ast::ItemKind::TraitAlias(..) => { | ast::ItemKind::TraitAlias(..)
| ast::ItemKind::MacroDef(..) => {
if item.vis.kind.is_pub() { if item.vis.kind.is_pub() {
self.prev_level self.prev_level
} else { } else {
......
...@@ -940,7 +940,7 @@ fn build_reduced_graph_for_block(&mut self, block: &Block) { ...@@ -940,7 +940,7 @@ fn build_reduced_graph_for_block(&mut self, block: &Block) {
/// Builds the reduced graph for a single item in an external crate. /// Builds the reduced graph for a single item in an external crate.
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) { fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
let parent = self.parent_scope.module; let parent = self.parent_scope.module;
let ModChild { ident, res, vis, span } = child; let ModChild { ident, res, vis, span, macro_rules } = child;
let res = res.expect_non_local(); let res = res.expect_non_local();
let expansion = self.parent_scope.expansion; let expansion = self.parent_scope.expansion;
// Record primary definitions. // Record primary definitions.
...@@ -972,7 +972,9 @@ fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) { ...@@ -972,7 +972,9 @@ fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
_, _,
) => self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)), ) => self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)),
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => { Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion)) if !macro_rules {
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
}
} }
Res::Def( Res::Def(
DefKind::TyParam DefKind::TyParam
......
...@@ -1399,14 +1399,22 @@ fn finalize_resolutions_in(&mut self, module: Module<'b>) { ...@@ -1399,14 +1399,22 @@ fn finalize_resolutions_in(&mut self, module: Module<'b>) {
let mut reexports = Vec::new(); let mut reexports = Vec::new();
module.for_each_child(self.r, |_, ident, _, binding| { module.for_each_child(self.r, |_, ident, _, binding| {
// Filter away ambiguous imports and anything that has def-site hygiene. // FIXME: Consider changing the binding inserted by `#[macro_export] macro_rules`
// FIXME: Implement actual cross-crate hygiene. // into the crate root to actual `NameBindingKind::Import`.
let is_good_import = if binding.is_import()
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion(); || matches!(binding.kind, NameBindingKind::Res(_, _is_macro_export @ true))
if is_good_import || binding.is_macro_def() { {
let res = binding.res().expect_non_local(); let res = binding.res().expect_non_local();
if res != def::Res::Err { // Ambiguous imports are treated as errors at this point and are
reexports.push(ModChild { ident, res, vis: binding.vis, span: binding.span }); // not exposed to other crates (see #36837 for more details).
if res != def::Res::Err && !binding.is_ambiguity() {
reexports.push(ModChild {
ident,
res,
vis: binding.vis,
span: binding.span,
macro_rules: false,
});
} }
} }
}); });
......
...@@ -845,10 +845,6 @@ fn is_importable(&self) -> bool { ...@@ -845,10 +845,6 @@ fn is_importable(&self) -> bool {
) )
} }
fn is_macro_def(&self) -> bool {
matches!(self.kind, NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _))
}
fn macro_kind(&self) -> Option<MacroKind> { fn macro_kind(&self) -> Option<MacroKind> {
self.res().macro_kind() self.res().macro_kind()
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册