Skip to content

Commit 415ba41

Browse files
committed
Auto merge of rust-lang#118620 - petrochenkov:defeed2, r=<try>
[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.
2 parents 0e2dac8 + dbbf484 commit 415ba41

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

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

+28-36
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
628628
let vis = self.resolve_visibility(&item.vis);
629629
let local_def_id = self.r.local_def_id(item.id);
630630
let def_id = local_def_id.to_def_id();
631+
let def_kind = self.r.tcx.def_kind(def_id);
632+
let res = Res::Def(def_kind, def_id);
631633

632634
self.r.visibilities.insert(local_def_id, vis);
633635

@@ -659,7 +661,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
659661
ItemKind::Mod(..) => {
660662
let module = self.r.new_module(
661663
Some(parent),
662-
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
664+
ModuleKind::Def(def_kind, def_id, ident.name),
663665
expansion.to_expn_id(),
664666
item.span,
665667
parent.no_implicit_prelude
@@ -672,16 +674,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
672674
}
673675

674676
// These items live in the value namespace.
675-
ItemKind::Static(box ast::StaticItem { mutability, .. }) => {
676-
let res = Res::Def(DefKind::Static(mutability), def_id);
677+
ItemKind::Static(..) => {
677678
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
678679
}
679680
ItemKind::Const(..) => {
680-
let res = Res::Def(DefKind::Const, def_id);
681681
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
682682
}
683683
ItemKind::Fn(..) => {
684-
let res = Res::Def(DefKind::Fn, def_id);
685684
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
686685

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

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

698696
ItemKind::Enum(_, _) => {
699697
let module = self.r.new_module(
700698
Some(parent),
701-
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
699+
ModuleKind::Def(def_kind, def_id, ident.name),
702700
expansion.to_expn_id(),
703701
item.span,
704702
parent.no_implicit_prelude,
@@ -708,14 +706,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
708706
}
709707

710708
ItemKind::TraitAlias(..) => {
711-
let res = Res::Def(DefKind::TraitAlias, def_id);
712709
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
713710
}
714711

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

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

725721
// If this is a tuple or unit struct, define a name
726722
// in the value namespace as well.
727-
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(vdata) {
723+
if let Some(ctor_node_id) = vdata.ctor_node_id() {
728724
// If the structure is marked as non_exhaustive then lower the visibility
729725
// to within the crate.
730726
let mut ctor_vis = if vis.is_public()
@@ -750,8 +746,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
750746
ret_fields.push(field_vis.to_def_id());
751747
}
752748
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());
749+
let ctor_def_kind = self.r.tcx.def_kind(ctor_def_id);
750+
let ctor_res = Res::Def(ctor_def_kind, ctor_def_id.to_def_id());
755751
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
756752
self.r.visibilities.insert(ctor_def_id, ctor_vis);
757753
// We need the field visibility spans also for the constructor for E0603.
@@ -764,7 +760,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
764760
}
765761

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

770765
// Record field names for error reporting.
@@ -776,7 +771,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
776771
// Add all the items within to a new module.
777772
let module = self.r.new_module(
778773
Some(parent),
779-
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
774+
ModuleKind::Def(def_kind, def_id, ident.name),
780775
expansion.to_expn_id(),
781776
item.span,
782777
parent.no_implicit_prelude,
@@ -888,16 +883,16 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
888883
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
889884
let local_def_id = self.r.local_def_id(item.id);
890885
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!(),
886+
let ns = match item.kind {
887+
ForeignItemKind::Fn(..) => ValueNS,
888+
ForeignItemKind::Static(..) => ValueNS,
889+
ForeignItemKind::TyAlias(..) => TypeNS,
890+
ForeignItemKind::MacCall(..) => unreachable!(),
896891
};
897892
let parent = self.parent_scope.module;
898893
let expansion = self.parent_scope.expansion;
899894
let vis = self.resolve_visibility(&item.vis);
900-
let res = Res::Def(def_kind, def_id);
895+
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id);
901896
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
902897
self.r.visibilities.insert(local_def_id, vis);
903898
}
@@ -1180,24 +1175,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
11801175
let parent_scope = self.parent_scope;
11811176
let expansion = parent_scope.expansion;
11821177
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-
}
1178+
let (ident, span, macro_rules) = match &item.kind {
1179+
ItemKind::MacroDef(def) => (item.ident, item.span, def.macro_rules),
11881180
ItemKind::Fn(..) => match self.proc_macro_stub(item) {
11891181
Some((macro_kind, ident, span)) => {
11901182
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
11911183
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
11921184
self.r.proc_macro_stubs.insert(def_id);
1193-
(macro_kind, ident, span, false)
1185+
(ident, span, false)
11941186
}
11951187
None => return parent_scope.macro_rules,
11961188
},
11971189
_ => unreachable!(),
11981190
};
11991191

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

12031195
if macro_rules {
@@ -1363,21 +1355,21 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
13631355
}
13641356

13651357
if ctxt == AssocCtxt::Trait {
1366-
let (def_kind, ns) = match item.kind {
1367-
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
1358+
let ns = match item.kind {
1359+
AssocItemKind::Const(..) => ValueNS,
13681360
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
13691361
if sig.decl.has_self() {
13701362
self.r.has_self.insert(local_def_id);
13711363
}
1372-
(DefKind::AssocFn, ValueNS)
1364+
ValueNS
13731365
}
1374-
AssocItemKind::Type(..) => (DefKind::AssocTy, TypeNS),
1366+
AssocItemKind::Type(..) => TypeNS,
13751367
AssocItemKind::MacCall(_) => bug!(), // handled above
13761368
};
13771369

13781370
let parent = self.parent_scope.module;
13791371
let expansion = self.parent_scope.expansion;
1380-
let res = Res::Def(def_kind, def_id);
1372+
let res = Res::Def(self.r.tcx.def_kind(def_id), def_id);
13811373
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
13821374
}
13831375

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

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

14731465
// Define a constructor name in the value namespace.
1474-
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&variant.data) {
1466+
if let Some(ctor_node_id) = variant.data.ctor_node_id() {
14751467
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());
1468+
let ctor_def_kind = self.r.tcx.def_kind(ctor_def_id);
1469+
let ctor_res = Res::Def(ctor_def_kind, ctor_def_id.to_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)