Skip to content

Commit

Permalink
Auto merge of rust-lang#118620 - petrochenkov:defeed2, r=<try>
Browse files Browse the repository at this point in the history
[WIP] resolve: Use def_kind query to cleanup some code

Follow up to rust-lang#118188.

Similar attempts to use queries in resolver resulted in perf regressions in the past, so this needs a perf run first.
  • Loading branch information
bors committed Dec 4, 2023
2 parents 0e2dac8 + dbbf484 commit 415ba41
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
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();
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);

Expand Down Expand Up @@ -659,7 +661,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
ItemKind::Mod(..) => {
let module = self.r.new_module(
Some(parent),
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
ModuleKind::Def(def_kind, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude
Expand All @@ -672,16 +674,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

// These items live in the value namespace.
ItemKind::Static(box ast::StaticItem { mutability, .. }) => {
let res = Res::Def(DefKind::Static(mutability), def_id);
ItemKind::Static(..) => {
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Const(..) => {
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, def_id);
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));

// Functions introducing procedural macros reserve a slot
Expand All @@ -691,14 +690,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {

// These items live in the type namespace.
ItemKind::TyAlias(..) => {
let res = Res::Def(DefKind::TyAlias, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
}

ItemKind::Enum(_, _) => {
let module = self.r.new_module(
Some(parent),
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
ModuleKind::Def(def_kind, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,
Expand All @@ -708,14 +706,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

ItemKind::TraitAlias(..) => {
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 res = Res::Def(DefKind::Struct, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
Expand All @@ -724,7 +720,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {

// If this is a tuple or unit struct, define a name
// 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
// to within the crate.
let mut ctor_vis = if vis.is_public()
Expand All @@ -750,8 +746,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
ret_fields.push(field_vis.to_def_id());
}
let ctor_def_id = self.r.local_def_id(ctor_node_id);
let ctor_res =
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id.to_def_id());
let ctor_def_kind = self.r.tcx.def_kind(ctor_def_id);
let ctor_res = Res::Def(ctor_def_kind, 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);
// We need the field visibility spans also for the constructor for E0603.
Expand All @@ -764,7 +760,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

ItemKind::Union(ref vdata, _) => {
let res = Res::Def(DefKind::Union, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

// Record field names for error reporting.
Expand All @@ -776,7 +771,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
// Add all the items within to a new module.
let module = self.r.new_module(
Some(parent),
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
ModuleKind::Def(def_kind, def_id, ident.name),
expansion.to_expn_id(),
item.span,
parent.no_implicit_prelude,
Expand Down Expand Up @@ -888,16 +883,16 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
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(_, mt, _) => (DefKind::Static(mt), ValueNS),
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
ForeignItemKind::MacCall(_) => unreachable!(),
let ns = match item.kind {
ForeignItemKind::Fn(..) => ValueNS,
ForeignItemKind::Static(..) => ValueNS,
ForeignItemKind::TyAlias(..) => 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);
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id);
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
self.r.visibilities.insert(local_def_id, vis);
}
Expand Down Expand Up @@ -1180,24 +1175,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
let parent_scope = self.parent_scope;
let expansion = parent_scope.expansion;
let def_id = self.r.local_def_id(item.id);
let (macro_kind, ident, span, macro_rules) = match &item.kind {
ItemKind::MacroDef(def) => {
let macro_kind = self.r.macro_map[&def_id.to_def_id()].ext.macro_kind();
(macro_kind, item.ident, item.span, def.macro_rules)
}
let (ident, span, macro_rules) = match &item.kind {
ItemKind::MacroDef(def) => (item.ident, item.span, def.macro_rules),
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
Some((macro_kind, ident, span)) => {
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.proc_macro_stubs.insert(def_id);
(macro_kind, ident, span, false)
(ident, span, false)
}
None => return parent_scope.macro_rules,
},
_ => unreachable!(),
};

let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id.to_def_id());
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);

if macro_rules {
Expand Down Expand Up @@ -1363,21 +1355,21 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
}

if ctxt == AssocCtxt::Trait {
let (def_kind, ns) = match item.kind {
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
let ns = match item.kind {
AssocItemKind::Const(..) => ValueNS,
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
if sig.decl.has_self() {
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
};

let parent = self.parent_scope.module;
let expansion = self.parent_scope.expansion;
let res = Res::Def(def_kind, def_id);
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id);
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
}

Expand Down Expand Up @@ -1457,7 +1449,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {

// Define a name in the type namespace.
let def_id = self.r.local_def_id(variant.id);
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id.to_def_id());
let vis = self.resolve_visibility(&variant.vis);
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
self.r.visibilities.insert(def_id, vis);
Expand All @@ -1471,10 +1463,10 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
};

// 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_res =
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
let ctor_def_kind = self.r.tcx.def_kind(ctor_def_id);
let ctor_res = Res::Def(ctor_def_kind, ctor_def_id.to_def_id());
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
self.r.visibilities.insert(ctor_def_id, ctor_vis);
}
Expand Down

0 comments on commit 415ba41

Please sign in to comment.