Skip to content

Commit 670ba47

Browse files
authored
Rollup merge of #118620 - petrochenkov:defeed2, r=compiler-errors
resolve: Use `def_kind` query to cleanup some code Follow up to #118188. Similar attempts to use queries in resolver resulted in perf regressions in the past, so this needs a perf run first.
2 parents 5701093 + fb9ca0f commit 670ba47

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

Diff for: compiler/rustc_resolve/src/build_reduced_graph.rs

+31-39
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for BuildReducedGraphVisitor<'a, '_, 't
210210
}
211211

212212
impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
213+
fn res(&self, def_id: impl Into<DefId>) -> Res {
214+
let def_id = def_id.into();
215+
Res::Def(self.r.tcx.def_kind(def_id), def_id)
216+
}
217+
213218
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
214219
self.try_resolve_visibility(vis, true).unwrap_or_else(|err| {
215220
self.r.report_vis_error(err);
@@ -628,6 +633,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
628633
let vis = self.resolve_visibility(&item.vis);
629634
let local_def_id = self.r.local_def_id(item.id);
630635
let def_id = local_def_id.to_def_id();
636+
let def_kind = self.r.tcx.def_kind(def_id);
637+
let res = Res::Def(def_kind, def_id);
631638

632639
self.r.visibilities.insert(local_def_id, vis);
633640

@@ -659,7 +666,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
659666
ItemKind::Mod(..) => {
660667
let module = self.r.new_module(
661668
Some(parent),
662-
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
669+
ModuleKind::Def(def_kind, def_id, ident.name),
663670
expansion.to_expn_id(),
664671
item.span,
665672
parent.no_implicit_prelude
@@ -672,16 +679,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
672679
}
673680

674681
// These items live in the value namespace.
675-
ItemKind::Static(box ast::StaticItem { mutability, .. }) => {
676-
let res = Res::Def(DefKind::Static(mutability), def_id);
682+
ItemKind::Static(..) => {
677683
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
678684
}
679685
ItemKind::Const(..) => {
680-
let res = Res::Def(DefKind::Const, def_id);
681686
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
682687
}
683688
ItemKind::Fn(..) => {
684-
let res = Res::Def(DefKind::Fn, def_id);
685689
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
686690

687691
// Functions introducing procedural macros reserve a slot
@@ -691,14 +695,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
691695

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

698701
ItemKind::Enum(_, _) => {
699702
let module = self.r.new_module(
700703
Some(parent),
701-
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
704+
ModuleKind::Def(def_kind, def_id, ident.name),
702705
expansion.to_expn_id(),
703706
item.span,
704707
parent.no_implicit_prelude,
@@ -708,14 +711,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
708711
}
709712

710713
ItemKind::TraitAlias(..) => {
711-
let res = Res::Def(DefKind::TraitAlias, def_id);
712714
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
713715
}
714716

715717
// These items live in both the type and value namespaces.
716718
ItemKind::Struct(ref vdata, _) => {
717719
// Define a name in the type namespace.
718-
let res = Res::Def(DefKind::Struct, def_id);
719720
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
720721

721722
// Record field names for error reporting.
@@ -724,7 +725,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
724725

725726
// If this is a tuple or unit struct, define a name
726727
// in the value namespace as well.
727-
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(vdata) {
728+
if let Some(ctor_node_id) = vdata.ctor_node_id() {
728729
// If the structure is marked as non_exhaustive then lower the visibility
729730
// to within the crate.
730731
let mut ctor_vis = if vis.is_public()
@@ -750,8 +751,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
750751
ret_fields.push(field_vis.to_def_id());
751752
}
752753
let ctor_def_id = self.r.local_def_id(ctor_node_id);
753-
let ctor_res =
754-
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id.to_def_id());
754+
let ctor_res = self.res(ctor_def_id);
755755
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
756756
self.r.visibilities.insert(ctor_def_id, ctor_vis);
757757
// We need the field visibility spans also for the constructor for E0603.
@@ -764,7 +764,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
764764
}
765765

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

770769
// Record field names for error reporting.
@@ -776,7 +775,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
776775
// Add all the items within to a new module.
777776
let module = self.r.new_module(
778777
Some(parent),
779-
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
778+
ModuleKind::Def(def_kind, def_id, ident.name),
780779
expansion.to_expn_id(),
781780
item.span,
782781
parent.no_implicit_prelude,
@@ -888,17 +887,16 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
888887
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
889888
let local_def_id = self.r.local_def_id(item.id);
890889
let def_id = local_def_id.to_def_id();
891-
let (def_kind, ns) = match item.kind {
892-
ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS),
893-
ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS),
894-
ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS),
895-
ForeignItemKind::MacCall(_) => unreachable!(),
890+
let ns = match item.kind {
891+
ForeignItemKind::Fn(..) => ValueNS,
892+
ForeignItemKind::Static(..) => ValueNS,
893+
ForeignItemKind::TyAlias(..) => TypeNS,
894+
ForeignItemKind::MacCall(..) => unreachable!(),
896895
};
897896
let parent = self.parent_scope.module;
898897
let expansion = self.parent_scope.expansion;
899898
let vis = self.resolve_visibility(&item.vis);
900-
let res = Res::Def(def_kind, def_id);
901-
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
899+
self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion));
902900
self.r.visibilities.insert(local_def_id, vis);
903901
}
904902

@@ -1180,24 +1178,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
11801178
let parent_scope = self.parent_scope;
11811179
let expansion = parent_scope.expansion;
11821180
let def_id = self.r.local_def_id(item.id);
1183-
let (macro_kind, ident, span, macro_rules) = match &item.kind {
1184-
ItemKind::MacroDef(def) => {
1185-
let macro_kind = self.r.macro_map[&def_id.to_def_id()].ext.macro_kind();
1186-
(macro_kind, item.ident, item.span, def.macro_rules)
1187-
}
1181+
let (res, ident, span, macro_rules) = match &item.kind {
1182+
ItemKind::MacroDef(def) => (self.res(def_id), item.ident, item.span, def.macro_rules),
11881183
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
11891184
Some((macro_kind, ident, span)) => {
1185+
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
11901186
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
11911187
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
11921188
self.r.proc_macro_stubs.insert(def_id);
1193-
(macro_kind, ident, span, false)
1189+
(res, ident, span, false)
11941190
}
11951191
None => return parent_scope.macro_rules,
11961192
},
11971193
_ => unreachable!(),
11981194
};
11991195

1200-
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
12011196
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);
12021197

12031198
if macro_rules {
@@ -1363,22 +1358,21 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13631358
}
13641359

13651360
if ctxt == AssocCtxt::Trait {
1366-
let (def_kind, ns) = match item.kind {
1367-
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
1361+
let ns = match item.kind {
1362+
AssocItemKind::Const(..) => ValueNS,
13681363
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
13691364
if sig.decl.has_self() {
13701365
self.r.has_self.insert(local_def_id);
13711366
}
1372-
(DefKind::AssocFn, ValueNS)
1367+
ValueNS
13731368
}
1374-
AssocItemKind::Type(..) => (DefKind::AssocTy, TypeNS),
1369+
AssocItemKind::Type(..) => TypeNS,
13751370
AssocItemKind::MacCall(_) => bug!(), // handled above
13761371
};
13771372

13781373
let parent = self.parent_scope.module;
13791374
let expansion = self.parent_scope.expansion;
1380-
let res = Res::Def(def_kind, def_id);
1381-
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
1375+
self.r.define(parent, item.ident, ns, (self.res(def_id), vis, item.span, expansion));
13821376
}
13831377

13841378
visit::walk_assoc_item(self, item, ctxt);
@@ -1457,9 +1451,8 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14571451

14581452
// Define a name in the type namespace.
14591453
let def_id = self.r.local_def_id(variant.id);
1460-
let res = Res::Def(DefKind::Variant, def_id.to_def_id());
14611454
let vis = self.resolve_visibility(&variant.vis);
1462-
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
1455+
self.r.define(parent, ident, TypeNS, (self.res(def_id), vis, variant.span, expn_id));
14631456
self.r.visibilities.insert(def_id, vis);
14641457

14651458
// 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> {
14711464
};
14721465

14731466
// Define a constructor name in the value namespace.
1474-
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
1467+
if let Some(ctor_node_id) = variant.data.ctor_node_id() {
14751468
let ctor_def_id = self.r.local_def_id(ctor_node_id);
1476-
let ctor_res =
1477-
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id.to_def_id());
1469+
let ctor_res = self.res(ctor_def_id);
14781470
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
14791471
self.r.visibilities.insert(ctor_def_id, ctor_vis);
14801472
}

0 commit comments

Comments
 (0)