Rollup merge of #118620 - petrochenkov:defeed2, r=compiler-errors
resolve: Use `def_kind` query to cleanup some code Follow up to https://github.com/rust-lang/rust/pull/118188. Similar attempts to use queries in resolver resulted in perf regressions in the past, so this needs a perf run first.
This commit is contained in:
commit
670ba478de
@ -210,6 +210,11 @@ impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for BuildReducedGraphVisitor<'a, '_, 't
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
||||||
|
fn res(&self, def_id: impl Into<DefId>) -> Res {
|
||||||
|
let def_id = def_id.into();
|
||||||
|
Res::Def(self.r.tcx.def_kind(def_id), def_id)
|
||||||
|
}
|
||||||
|
|
||||||
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
|
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
|
||||||
self.try_resolve_visibility(vis, true).unwrap_or_else(|err| {
|
self.try_resolve_visibility(vis, true).unwrap_or_else(|err| {
|
||||||
self.r.report_vis_error(err);
|
self.r.report_vis_error(err);
|
||||||
@ -628,6 +633,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
let vis = self.resolve_visibility(&item.vis);
|
let vis = self.resolve_visibility(&item.vis);
|
||||||
let local_def_id = self.r.local_def_id(item.id);
|
let local_def_id = self.r.local_def_id(item.id);
|
||||||
let def_id = local_def_id.to_def_id();
|
let def_id = local_def_id.to_def_id();
|
||||||
|
let def_kind = self.r.tcx.def_kind(def_id);
|
||||||
|
let res = Res::Def(def_kind, def_id);
|
||||||
|
|
||||||
self.r.visibilities.insert(local_def_id, vis);
|
self.r.visibilities.insert(local_def_id, vis);
|
||||||
|
|
||||||
@ -659,7 +666,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
ItemKind::Mod(..) => {
|
ItemKind::Mod(..) => {
|
||||||
let module = self.r.new_module(
|
let module = self.r.new_module(
|
||||||
Some(parent),
|
Some(parent),
|
||||||
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
|
ModuleKind::Def(def_kind, def_id, ident.name),
|
||||||
expansion.to_expn_id(),
|
expansion.to_expn_id(),
|
||||||
item.span,
|
item.span,
|
||||||
parent.no_implicit_prelude
|
parent.no_implicit_prelude
|
||||||
@ -672,16 +679,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// These items live in the value namespace.
|
// These items live in the value namespace.
|
||||||
ItemKind::Static(box ast::StaticItem { mutability, .. }) => {
|
ItemKind::Static(..) => {
|
||||||
let res = Res::Def(DefKind::Static(mutability), def_id);
|
|
||||||
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
||||||
}
|
}
|
||||||
ItemKind::Const(..) => {
|
ItemKind::Const(..) => {
|
||||||
let res = Res::Def(DefKind::Const, def_id);
|
|
||||||
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
||||||
}
|
}
|
||||||
ItemKind::Fn(..) => {
|
ItemKind::Fn(..) => {
|
||||||
let res = Res::Def(DefKind::Fn, def_id);
|
|
||||||
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
||||||
|
|
||||||
// Functions introducing procedural macros reserve a slot
|
// Functions introducing procedural macros reserve a slot
|
||||||
@ -691,14 +695,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
|
|
||||||
// These items live in the type namespace.
|
// These items live in the type namespace.
|
||||||
ItemKind::TyAlias(..) => {
|
ItemKind::TyAlias(..) => {
|
||||||
let res = Res::Def(DefKind::TyAlias, def_id);
|
|
||||||
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemKind::Enum(_, _) => {
|
ItemKind::Enum(_, _) => {
|
||||||
let module = self.r.new_module(
|
let module = self.r.new_module(
|
||||||
Some(parent),
|
Some(parent),
|
||||||
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
|
ModuleKind::Def(def_kind, def_id, ident.name),
|
||||||
expansion.to_expn_id(),
|
expansion.to_expn_id(),
|
||||||
item.span,
|
item.span,
|
||||||
parent.no_implicit_prelude,
|
parent.no_implicit_prelude,
|
||||||
@ -708,14 +711,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ItemKind::TraitAlias(..) => {
|
ItemKind::TraitAlias(..) => {
|
||||||
let res = Res::Def(DefKind::TraitAlias, def_id);
|
|
||||||
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
||||||
}
|
}
|
||||||
|
|
||||||
// These items live in both the type and value namespaces.
|
// These items live in both the type and value namespaces.
|
||||||
ItemKind::Struct(ref vdata, _) => {
|
ItemKind::Struct(ref vdata, _) => {
|
||||||
// Define a name in the type namespace.
|
// Define a name in the type namespace.
|
||||||
let res = Res::Def(DefKind::Struct, def_id);
|
|
||||||
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
||||||
|
|
||||||
// Record field names for error reporting.
|
// Record field names for error reporting.
|
||||||
@ -724,7 +725,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
|
|
||||||
// If this is a tuple or unit struct, define a name
|
// If this is a tuple or unit struct, define a name
|
||||||
// in the value namespace as well.
|
// in the value namespace as well.
|
||||||
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(vdata) {
|
if let Some(ctor_node_id) = vdata.ctor_node_id() {
|
||||||
// If the structure is marked as non_exhaustive then lower the visibility
|
// If the structure is marked as non_exhaustive then lower the visibility
|
||||||
// to within the crate.
|
// to within the crate.
|
||||||
let mut ctor_vis = if vis.is_public()
|
let mut ctor_vis = if vis.is_public()
|
||||||
@ -750,8 +751,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
ret_fields.push(field_vis.to_def_id());
|
ret_fields.push(field_vis.to_def_id());
|
||||||
}
|
}
|
||||||
let ctor_def_id = self.r.local_def_id(ctor_node_id);
|
let ctor_def_id = self.r.local_def_id(ctor_node_id);
|
||||||
let ctor_res =
|
let ctor_res = self.res(ctor_def_id);
|
||||||
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id.to_def_id());
|
|
||||||
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
|
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
|
||||||
self.r.visibilities.insert(ctor_def_id, ctor_vis);
|
self.r.visibilities.insert(ctor_def_id, ctor_vis);
|
||||||
// We need the field visibility spans also for the constructor for E0603.
|
// We need the field visibility spans also for the constructor for E0603.
|
||||||
@ -764,7 +764,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ItemKind::Union(ref vdata, _) => {
|
ItemKind::Union(ref vdata, _) => {
|
||||||
let res = Res::Def(DefKind::Union, def_id);
|
|
||||||
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
||||||
|
|
||||||
// Record field names for error reporting.
|
// Record field names for error reporting.
|
||||||
@ -776,7 +775,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
// Add all the items within to a new module.
|
// Add all the items within to a new module.
|
||||||
let module = self.r.new_module(
|
let module = self.r.new_module(
|
||||||
Some(parent),
|
Some(parent),
|
||||||
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
|
ModuleKind::Def(def_kind, def_id, ident.name),
|
||||||
expansion.to_expn_id(),
|
expansion.to_expn_id(),
|
||||||
item.span,
|
item.span,
|
||||||
parent.no_implicit_prelude,
|
parent.no_implicit_prelude,
|
||||||
@ -888,17 +887,16 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
|
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
|
||||||
let local_def_id = self.r.local_def_id(item.id);
|
let local_def_id = self.r.local_def_id(item.id);
|
||||||
let def_id = local_def_id.to_def_id();
|
let def_id = local_def_id.to_def_id();
|
||||||
let (def_kind, ns) = match item.kind {
|
let ns = match item.kind {
|
||||||
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
|
ForeignItemKind::Fn(..) => ValueNS,
|
||||||
ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS),
|
ForeignItemKind::Static(..) => ValueNS,
|
||||||
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
|
ForeignItemKind::TyAlias(..) => TypeNS,
|
||||||
ForeignItemKind::MacCall(_) => unreachable!(),
|
ForeignItemKind::MacCall(..) => unreachable!(),
|
||||||
};
|
};
|
||||||
let parent = self.parent_scope.module;
|
let parent = self.parent_scope.module;
|
||||||
let expansion = self.parent_scope.expansion;
|
let expansion = self.parent_scope.expansion;
|
||||||
let vis = self.resolve_visibility(&item.vis);
|
let vis = self.resolve_visibility(&item.vis);
|
||||||
let res = Res::Def(def_kind, def_id);
|
self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion));
|
||||||
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
|
|
||||||
self.r.visibilities.insert(local_def_id, vis);
|
self.r.visibilities.insert(local_def_id, vis);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1180,24 +1178,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
let parent_scope = self.parent_scope;
|
let parent_scope = self.parent_scope;
|
||||||
let expansion = parent_scope.expansion;
|
let expansion = parent_scope.expansion;
|
||||||
let def_id = self.r.local_def_id(item.id);
|
let def_id = self.r.local_def_id(item.id);
|
||||||
let (macro_kind, ident, span, macro_rules) = match &item.kind {
|
let (res, ident, span, macro_rules) = match &item.kind {
|
||||||
ItemKind::MacroDef(def) => {
|
ItemKind::MacroDef(def) => (self.res(def_id), item.ident, item.span, def.macro_rules),
|
||||||
let macro_kind = self.r.macro_map[&def_id.to_def_id()].ext.macro_kind();
|
|
||||||
(macro_kind, item.ident, item.span, def.macro_rules)
|
|
||||||
}
|
|
||||||
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
|
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
|
||||||
Some((macro_kind, ident, span)) => {
|
Some((macro_kind, ident, span)) => {
|
||||||
|
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
|
||||||
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
|
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
|
||||||
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
|
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
|
||||||
self.r.proc_macro_stubs.insert(def_id);
|
self.r.proc_macro_stubs.insert(def_id);
|
||||||
(macro_kind, ident, span, false)
|
(res, ident, span, false)
|
||||||
}
|
}
|
||||||
None => return parent_scope.macro_rules,
|
None => return parent_scope.macro_rules,
|
||||||
},
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
|
|
||||||
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
|
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
|
||||||
|
|
||||||
if macro_rules {
|
if macro_rules {
|
||||||
@ -1363,22 +1358,21 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctxt == AssocCtxt::Trait {
|
if ctxt == AssocCtxt::Trait {
|
||||||
let (def_kind, ns) = match item.kind {
|
let ns = match item.kind {
|
||||||
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
|
AssocItemKind::Const(..) => ValueNS,
|
||||||
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
|
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
|
||||||
if sig.decl.has_self() {
|
if sig.decl.has_self() {
|
||||||
self.r.has_self.insert(local_def_id);
|
self.r.has_self.insert(local_def_id);
|
||||||
}
|
}
|
||||||
(DefKind::AssocFn, ValueNS)
|
ValueNS
|
||||||
}
|
}
|
||||||
AssocItemKind::Type(..) => (DefKind::AssocTy, TypeNS),
|
AssocItemKind::Type(..) => TypeNS,
|
||||||
AssocItemKind::MacCall(_) => bug!(), // handled above
|
AssocItemKind::MacCall(_) => bug!(), // handled above
|
||||||
};
|
};
|
||||||
|
|
||||||
let parent = self.parent_scope.module;
|
let parent = self.parent_scope.module;
|
||||||
let expansion = self.parent_scope.expansion;
|
let expansion = self.parent_scope.expansion;
|
||||||
let res = Res::Def(def_kind, def_id);
|
self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion));
|
||||||
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
visit::walk_assoc_item(self, item, ctxt);
|
visit::walk_assoc_item(self, item, ctxt);
|
||||||
@ -1457,9 +1451,8 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
|
|
||||||
// Define a name in the type namespace.
|
// Define a name in the type namespace.
|
||||||
let def_id = self.r.local_def_id(variant.id);
|
let def_id = self.r.local_def_id(variant.id);
|
||||||
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
|
|
||||||
let vis = self.resolve_visibility(&variant.vis);
|
let vis = self.resolve_visibility(&variant.vis);
|
||||||
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
|
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
|
||||||
self.r.visibilities.insert(def_id, vis);
|
self.r.visibilities.insert(def_id, vis);
|
||||||
|
|
||||||
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
|
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
|
||||||
@ -1471,10 +1464,9 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Define a constructor name in the value namespace.
|
// Define a constructor name in the value namespace.
|
||||||
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
|
if let Some(ctor_node_id) = variant.data.ctor_node_id() {
|
||||||
let ctor_def_id = self.r.local_def_id(ctor_node_id);
|
let ctor_def_id = self.r.local_def_id(ctor_node_id);
|
||||||
let ctor_res =
|
let ctor_res = self.res(ctor_def_id);
|
||||||
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));
|
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
|
||||||
self.r.visibilities.insert(ctor_def_id, ctor_vis);
|
self.r.visibilities.insert(ctor_def_id, ctor_vis);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user