diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 65901eedb21..213b320ed1a 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -210,6 +210,11 @@ impl<'a, 'tcx> AsMut> for BuildReducedGraphVisitor<'a, '_, 't } impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { + fn res(&self, def_id: impl Into) -> 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 { self.try_resolve_visibility(vis, true).unwrap_or_else(|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 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); @@ -659,7 +666,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 @@ -672,16 +679,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 @@ -691,14 +695,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, @@ -708,14 +711,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. @@ -724,7 +725,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() @@ -750,8 +751,7 @@ 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_res = self.res(ctor_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. @@ -764,7 +764,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. @@ -776,7 +775,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, @@ -888,17 +887,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); - self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); + self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion)); 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 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 (res, ident, span, macro_rules) = match &item.kind { + ItemKind::MacroDef(def) => (self.res(def_id), item.ident, item.span, def.macro_rules), ItemKind::Fn(..) => match self.proc_macro_stub(item) { 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)); 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) + (res, ident, span, false) } None => return parent_scope.macro_rules, }, _ => 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); if macro_rules { @@ -1363,22 +1358,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); - self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion)); + self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion)); } 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. 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); - 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); // 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. - 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_res = self.res(ctor_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); }