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

resolve/metadata: Stop encoding macros as reexports

上级 50568b8e
......@@ -1077,6 +1077,7 @@ fn for_each_module_child(
res,
vis: ty::Visibility::Public,
span: ident.span,
macro_rules: false,
});
}
}
......@@ -1088,17 +1089,19 @@ fn for_each_module_child(
for child_index in children.decode((self, sess)) {
if let Some(ident) = self.opt_item_ident(child_index, sess) {
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 res = Res::Def(kind, def_id);
let vis = self.get_visibility(child_index);
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.
// Re-export lists automatically contain constructors when necessary.
......@@ -1110,7 +1113,13 @@ fn for_each_module_child(
let ctor_res =
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
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 => {
......@@ -1135,7 +1144,13 @@ fn for_each_module_child(
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 {
pub vis: ty::Visibility,
/// Span of the item.
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) {
ast::ItemKind::Impl(..) => return,
// 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 =
item.attrs.iter().any(|attr| attr.has_name(sym::macro_export));
if is_macro_export { Some(AccessLevel::Public) } else { None }
......@@ -155,7 +155,8 @@ fn visit_item(&mut self, item: &'ast ast::Item) {
| ast::ItemKind::Struct(..)
| ast::ItemKind::Union(..)
| ast::ItemKind::Trait(..)
| ast::ItemKind::TraitAlias(..) => {
| ast::ItemKind::TraitAlias(..)
| ast::ItemKind::MacroDef(..) => {
if item.vis.kind.is_pub() {
self.prev_level
} else {
......
......@@ -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.
fn build_reduced_graph_for_external_crate_res(&mut self, child: ModChild) {
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 expansion = self.parent_scope.expansion;
// Record primary definitions.
......@@ -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)),
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(
DefKind::TyParam
......
......@@ -1399,14 +1399,22 @@ fn finalize_resolutions_in(&mut self, module: Module<'b>) {
let mut reexports = Vec::new();
module.for_each_child(self.r, |_, ident, _, binding| {
// Filter away ambiguous imports and anything that has def-site hygiene.
// FIXME: Implement actual cross-crate hygiene.
let is_good_import =
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
if is_good_import || binding.is_macro_def() {
// FIXME: Consider changing the binding inserted by `#[macro_export] macro_rules`
// into the crate root to actual `NameBindingKind::Import`.
if binding.is_import()
|| matches!(binding.kind, NameBindingKind::Res(_, _is_macro_export @ true))
{
let res = binding.res().expect_non_local();
if res != def::Res::Err {
reexports.push(ModChild { ident, res, vis: binding.vis, span: binding.span });
// Ambiguous imports are treated as errors at this point and are
// 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 {
)
}
fn is_macro_def(&self) -> bool {
matches!(self.kind, NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _))
}
fn macro_kind(&self) -> Option<MacroKind> {
self.res().macro_kind()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册