Calculate visibilities once in resolve

Then use them through a query based on resolver outputs
This commit is contained in:
Vadim Petrochenkov 2020-10-17 02:28:11 +03:00
parent cb2462c53f
commit cee5521a03
26 changed files with 323 additions and 409 deletions

View file

@ -613,12 +613,21 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
/// Constructs the reduced graph for one item.
fn build_reduced_graph_for_item(&mut self, item: &'b Item) {
if matches!(item.kind, ItemKind::Mod(..)) && item.ident.name == kw::Invalid {
// Fake crate root item from expand.
return;
}
let parent_scope = &self.parent_scope;
let parent = parent_scope.module;
let expansion = parent_scope.expansion;
let ident = item.ident;
let sp = item.span;
let vis = self.resolve_visibility(&item.vis);
let local_def_id = self.r.local_def_id(item.id);
let def_id = local_def_id.to_def_id();
self.r.visibilities.insert(local_def_id, vis);
match item.kind {
ItemKind::Use(ref use_tree) => {
@ -651,10 +660,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} else if orig_name == Some(kw::SelfLower) {
self.r.graph_root
} else {
let def_id = self.r.local_def_id(item.id);
let crate_id =
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, def_id);
self.r.extern_crate_map.insert(def_id, crate_id);
let crate_id = self.r.crate_loader.process_extern_crate(
item,
&self.r.definitions,
local_def_id,
);
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
};
@ -705,25 +716,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.define(parent, ident, TypeNS, imported_binding);
}
ItemKind::Mod(..) if ident.name == kw::Invalid => {} // Crate root
ItemKind::Mod(..) => {
let def_id = self.r.local_def_id(item.id);
let module_kind = ModuleKind::Def(DefKind::Mod, def_id.to_def_id(), ident.name);
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
let module = self.r.arenas.alloc_module(ModuleData {
no_implicit_prelude: parent.no_implicit_prelude || {
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
},
..ModuleData::new(
Some(parent),
module_kind,
def_id.to_def_id(),
expansion,
item.span,
)
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
});
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(def_id, module);
self.r.module_map.insert(local_def_id, module);
// Descend into the module.
self.parent_scope.module = module;
@ -731,15 +733,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in the value namespace.
ItemKind::Static(..) => {
let res = Res::Def(DefKind::Static, self.r.local_def_id(item.id).to_def_id());
let res = Res::Def(DefKind::Static, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Const(..) => {
let res = Res::Def(DefKind::Const, self.r.local_def_id(item.id).to_def_id());
let res = Res::Def(DefKind::Const, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Fn(..) => {
let res = Res::Def(DefKind::Fn, self.r.local_def_id(item.id).to_def_id());
let res = Res::Def(DefKind::Fn, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
// Functions introducing procedural macros reserve a slot
@ -749,13 +751,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in the type namespace.
ItemKind::TyAlias(..) => {
let res = Res::Def(DefKind::TyAlias, self.r.local_def_id(item.id).to_def_id());
let res = Res::Def(DefKind::TyAlias, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
}
ItemKind::Enum(_, _) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
self.r.variant_vis.insert(def_id, vis);
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
let module = self.r.new_module(
parent,
@ -769,14 +769,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
ItemKind::TraitAlias(..) => {
let res = Res::Def(DefKind::TraitAlias, self.r.local_def_id(item.id).to_def_id());
let res = Res::Def(DefKind::TraitAlias, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
}
// These items live in both the type and value namespaces.
ItemKind::Struct(ref vdata, _) => {
// Define a name in the type namespace.
let def_id = self.r.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Struct, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
@ -810,17 +809,19 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
ret_fields.push(field_vis);
}
let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_res = Res::Def(
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)),
self.r.local_def_id(ctor_node_id).to_def_id(),
ctor_def_id.to_def_id(),
);
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
self.r.visibilities.insert(ctor_def_id, ctor_vis);
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis, ret_fields));
}
}
ItemKind::Union(ref vdata, _) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Union, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
@ -829,8 +830,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
ItemKind::Trait(..) => {
let def_id = self.r.local_def_id(item.id).to_def_id();
// Add all the items within to a new module.
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
let module = self.r.new_module(
@ -845,6 +844,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
// These items do not add names to modules.
ItemKind::Impl { of_trait: Some(..), .. } => {
self.r.trait_impl_items.insert(local_def_id);
}
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
ItemKind::MacroDef(..) | ItemKind::MacCall(_) => unreachable!(),
@ -853,22 +855,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
let (res, ns) = match item.kind {
ForeignItemKind::Fn(..) => {
(Res::Def(DefKind::Fn, self.r.local_def_id(item.id).to_def_id()), ValueNS)
}
ForeignItemKind::Static(..) => {
(Res::Def(DefKind::Static, self.r.local_def_id(item.id).to_def_id()), ValueNS)
}
ForeignItemKind::TyAlias(..) => {
(Res::Def(DefKind::ForeignTy, self.r.local_def_id(item.id).to_def_id()), TypeNS)
}
let local_def_id = self.r.local_def_id(item.id);
let def_id = local_def_id.to_def_id();
let (def_kind, ns) = match item.kind {
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
ForeignItemKind::Static(..) => (DefKind::Static, ValueNS),
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
ForeignItemKind::MacCall(_) => unreachable!(),
};
let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion;
let vis = self.resolve_visibility(&item.vis);
let res = Res::Def(def_kind, def_id);
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
self.r.visibilities.insert(local_def_id, vis);
}
fn build_reduced_graph_for_block(&mut self, block: &Block) {
@ -1205,6 +1205,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.check_reserved_macro_name(ident, res);
self.insert_unused_macro(ident, def_id, item.id, span);
}
self.r.visibilities.insert(def_id, vis);
MacroRulesScope::Binding(self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
parent_macro_rules_scope: parent_scope.macro_rules,
binding,
@ -1224,6 +1225,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.insert_unused_macro(ident, def_id, item.id, span);
}
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
self.r.visibilities.insert(def_id, vis);
self.parent_scope.macro_rules
}
}
@ -1297,36 +1299,64 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
}
fn visit_assoc_item(&mut self, item: &'b AssocItem, ctxt: AssocCtxt) {
let parent = self.parent_scope.module;
if let AssocItemKind::MacCall(_) = item.kind {
self.visit_invoc(item.id);
return;
}
if let AssocCtxt::Impl = ctxt {
self.resolve_visibility(&item.vis);
visit::walk_assoc_item(self, item, ctxt);
return;
}
let local_def_id = self.r.local_def_id(item.id);
let def_id = local_def_id.to_def_id();
let vis = match ctxt {
AssocCtxt::Trait => {
let (def_kind, ns) = match item.kind {
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
AssocItemKind::Fn(_, ref sig, _, _) => {
if sig.decl.has_self() {
self.r.has_self.insert(def_id);
}
(DefKind::AssocFn, ValueNS)
}
AssocItemKind::TyAlias(..) => (DefKind::AssocTy, TypeNS),
AssocItemKind::MacCall(_) => bug!(), // handled above
};
// Add the item to the trait info.
let item_def_id = self.r.local_def_id(item.id).to_def_id();
let (res, ns) = match item.kind {
AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
AssocItemKind::Fn(_, ref sig, _, _) => {
if sig.decl.has_self() {
self.r.has_self.insert(item_def_id);
}
(Res::Def(DefKind::AssocFn, item_def_id), ValueNS)
let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion;
let res = Res::Def(def_kind, def_id);
// Trait item visibility is inherited from its trait when not specified explicitly.
let vis = match &item.vis.kind {
ast::VisibilityKind::Inherited => {
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
}
_ => self.resolve_visibility(&item.vis),
};
// FIXME: For historical reasons the binding visibility is set to public,
// use actual visibility here instead, using enum variants as an example.
let vis_hack = ty::Visibility::Public;
self.r.define(parent, item.ident, ns, (res, vis_hack, item.span, expansion));
Some(vis)
}
AssocCtxt::Impl => {
// Trait impl item visibility is inherited from its trait when not specified
// explicitly. In that case we cannot determine it here in early resolve,
// so we leave a hole in the visibility table to be filled later.
// Inherent impl item visibility is never inherited from other items.
if matches!(item.vis.kind, ast::VisibilityKind::Inherited)
&& self
.r
.trait_impl_items
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local())
{
None
} else {
Some(self.resolve_visibility(&item.vis))
}
}
AssocItemKind::TyAlias(..) => (Res::Def(DefKind::AssocTy, item_def_id), TypeNS),
AssocItemKind::MacCall(_) => bug!(), // handled above
};
let vis = ty::Visibility::Public;
let expansion = self.parent_scope.expansion;
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
if let Some(vis) = vis {
self.r.visibilities.insert(local_def_id, vis);
}
visit::walk_assoc_item(self, item, ctxt);
}
@ -1394,7 +1424,8 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
if sf.is_placeholder {
self.visit_invoc(sf.id);
} else {
self.resolve_visibility(&sf.vis);
let vis = self.resolve_visibility(&sf.vis);
self.r.visibilities.insert(self.r.local_def_id(sf.id), vis);
visit::walk_struct_field(self, sf);
}
}
@ -1408,22 +1439,30 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
}
let parent = self.parent_scope.module;
let vis = self.r.variant_vis[&parent.def_id().expect("enum without def-id")];
let vis = match variant.vis.kind {
// Variant visibility is inherited from its enum when not specified explicitly.
ast::VisibilityKind::Inherited => {
self.r.visibilities[&parent.def_id().unwrap().expect_local()]
}
_ => self.resolve_visibility(&variant.vis),
};
let expn_id = self.parent_scope.expansion;
let ident = variant.ident;
// Define a name in the type namespace.
let def_id = self.r.local_def_id(variant.id).to_def_id();
let res = Res::Def(DefKind::Variant, def_id);
let def_id = self.r.local_def_id(variant.id);
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
self.r.visibilities.insert(def_id, vis);
// If the variant is marked as non_exhaustive then lower the visibility to within the
// crate.
let mut ctor_vis = vis;
let has_non_exhaustive = self.r.session.contains_name(&variant.attrs, sym::non_exhaustive);
if has_non_exhaustive && vis == ty::Visibility::Public {
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
}
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
let ctor_vis = if vis == ty::Visibility::Public
&& self.r.session.contains_name(&variant.attrs, sym::non_exhaustive)
{
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
} else {
vis
};
// Define a constructor name in the value namespace.
// Braced variants, unlike structs, generate unusable names in
@ -1431,12 +1470,15 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
// It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway.
let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id);
let ctor_def_id = self.r.local_def_id(ctor_node_id).to_def_id();
let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_kind = CtorKind::from_ast(&variant.data);
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
if ctor_def_id != def_id {
self.r.visibilities.insert(ctor_def_id, ctor_vis);
}
// Record field names for error reporting.
self.insert_field_names_local(ctor_def_id, &variant.data);
self.insert_field_names_local(ctor_def_id.to_def_id(), &variant.data);
visit::walk_variant(self, variant);
}

View file

@ -76,6 +76,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
let def_data = match &i.kind {
ItemKind::Impl { .. } => DefPathData::Impl,
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
// Fake crate root item from expand.
return visit::walk_item(self, i);
}
ItemKind::Mod(..)

View file

@ -944,7 +944,8 @@ pub struct Resolver<'a> {
/// Maps glob imports to the names of items actually imported.
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
/// Visibilities in "lowered" form, for all entities that have them.
visibilities: FxHashMap<LocalDefId, ty::Visibility>,
used_imports: FxHashSet<(NodeId, Namespace)>,
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
@ -1008,10 +1009,6 @@ pub struct Resolver<'a> {
/// Features enabled for this crate.
active_features: FxHashSet<Symbol>,
/// Stores enum visibilities to properly build a reduced graph
/// when visiting the correspondent variants.
variant_vis: DefIdMap<ty::Visibility>,
lint_buffer: LintBuffer,
next_node_id: NodeId,
@ -1028,6 +1025,9 @@ pub struct Resolver<'a> {
invocation_parents: FxHashMap<ExpnId, LocalDefId>,
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
/// Some way to know that we are in a *trait* impl in `visit_assoc_item`.
/// FIXME: Replace with a more general AST map (together with some other fields).
trait_impl_items: FxHashSet<LocalDefId>,
}
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@ -1195,7 +1195,8 @@ impl<'a> Resolver<'a> {
metadata_loader: &'a MetadataLoaderDyn,
arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a> {
let root_def_id = DefId::local(CRATE_DEF_INDEX);
let root_local_def_id = LocalDefId { local_def_index: CRATE_DEF_INDEX };
let root_def_id = root_local_def_id.to_def_id();
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Invalid);
let graph_root = arenas.alloc_module(ModuleData {
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
@ -1213,11 +1214,14 @@ impl<'a> Resolver<'a> {
)
});
let mut module_map = FxHashMap::default();
module_map.insert(LocalDefId { local_def_index: CRATE_DEF_INDEX }, graph_root);
module_map.insert(root_local_def_id, graph_root);
let definitions = Definitions::new(crate_name, session.local_crate_disambiguator());
let root = definitions.get_root_def();
let mut visibilities = FxHashMap::default();
visibilities.insert(root_local_def_id, ty::Visibility::Public);
let mut def_id_to_span = IndexVec::default();
assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root);
let mut def_id_to_node_id = IndexVec::default();
@ -1290,7 +1294,7 @@ impl<'a> Resolver<'a> {
ast_transform_scopes: FxHashMap::default(),
glob_map: Default::default(),
visibilities,
used_imports: FxHashSet::default(),
maybe_unused_trait_imports: Default::default(),
maybe_unused_extern_crates: Vec::new(),
@ -1339,7 +1343,6 @@ impl<'a> Resolver<'a> {
.map(|(feat, ..)| *feat)
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
.collect(),
variant_vis: Default::default(),
lint_buffer: LintBuffer::default(),
next_node_id: NodeId::from_u32(1),
def_id_to_span,
@ -1348,6 +1351,7 @@ impl<'a> Resolver<'a> {
placeholder_field_indices: Default::default(),
invocation_parents,
next_disambiguator: Default::default(),
trait_impl_items: Default::default(),
}
}
@ -1371,6 +1375,7 @@ impl<'a> Resolver<'a> {
pub fn into_outputs(self) -> ResolverOutputs {
let definitions = self.definitions;
let visibilities = self.visibilities;
let extern_crate_map = self.extern_crate_map;
let export_map = self.export_map;
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
@ -1379,6 +1384,7 @@ impl<'a> Resolver<'a> {
ResolverOutputs {
definitions: definitions,
cstore: Box::new(self.crate_loader.into_cstore()),
visibilities,
extern_crate_map,
export_map,
glob_map,
@ -1396,6 +1402,7 @@ impl<'a> Resolver<'a> {
ResolverOutputs {
definitions: self.definitions.clone(),
cstore: Box::new(self.cstore().clone()),
visibilities: self.visibilities.clone(),
extern_crate_map: self.extern_crate_map.clone(),
export_map: self.export_map.clone(),
glob_map: self.glob_map.clone(),