Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a185cdb

Browse files
committed
Iterate to encode def_kind.
1 parent c58a6fa commit a185cdb

File tree

6 files changed

+57
-72
lines changed

6 files changed

+57
-72
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
130130
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
131131
static_mutability => { cdata.static_mutability(def_id.index) }
132132
generator_kind => { cdata.generator_kind(def_id.index) }
133-
def_kind => { cdata.def_kind(def_id.index) }
133+
opt_def_kind => { Some(cdata.def_kind(def_id.index)) }
134134
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
135135
def_ident_span => {
136136
cdata.try_item_ident(def_id.index, &tcx.sess).ok().map(|ident| ident.span)

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
77
use rustc_data_structures::stable_hasher::StableHasher;
88
use rustc_data_structures::sync::{join, Lrc};
99
use rustc_hir as hir;
10-
use rustc_hir::def::{CtorKind, DefKind};
10+
use rustc_hir::def::{CtorOf, DefKind};
1111
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1212
use rustc_hir::definitions::DefPathData;
1313
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
@@ -725,6 +725,14 @@ impl EncodeContext<'a, 'tcx> {
725725
let hir = tcx.hir();
726726
for local_id in hir.iter_local_def_id() {
727727
let def_id = local_id.to_def_id();
728+
let def_kind = tcx.opt_def_kind(local_id);
729+
let def_kind = if let Some(def_kind) = def_kind { def_kind } else { continue };
730+
record!(self.tables.def_kind[def_id] <- match def_kind {
731+
// Replace Ctor by the enclosing object to avoid leaking details in children crates.
732+
DefKind::Ctor(CtorOf::Struct, _) => DefKind::Struct,
733+
DefKind::Ctor(CtorOf::Variant, _) => DefKind::Variant,
734+
def_kind => def_kind,
735+
});
728736
record!(self.tables.span[def_id] <- tcx.def_span(def_id));
729737
}
730738
}
@@ -752,7 +760,6 @@ impl EncodeContext<'a, 'tcx> {
752760
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
753761
};
754762

755-
record!(self.tables.def_kind[def_id] <- DefKind::Variant);
756763
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
757764
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
758765
record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
@@ -792,7 +799,6 @@ impl EncodeContext<'a, 'tcx> {
792799
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
793800
};
794801

795-
record!(self.tables.def_kind[def_id] <- DefKind::Variant);
796802
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
797803
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
798804
self.encode_stability(def_id);
@@ -844,7 +850,6 @@ impl EncodeContext<'a, 'tcx> {
844850
expansion: tcx.hir().definitions().expansion_that_defined(local_def_id),
845851
};
846852

847-
record!(self.tables.def_kind[def_id] <- DefKind::Mod);
848853
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
849854
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
850855
record!(self.tables.attributes[def_id] <- attrs);
@@ -875,7 +880,6 @@ impl EncodeContext<'a, 'tcx> {
875880
let variant_id = tcx.hir().local_def_id_to_hir_id(variant.def_id.expect_local());
876881
let variant_data = tcx.hir().expect_variant_data(variant_id);
877882

878-
record!(self.tables.def_kind[def_id] <- DefKind::Field);
879883
record!(self.tables.kind[def_id] <- EntryKind::Field);
880884
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
881885
record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs);
@@ -901,7 +905,6 @@ impl EncodeContext<'a, 'tcx> {
901905
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
902906
};
903907

904-
record!(self.tables.def_kind[def_id] <- DefKind::Struct);
905908
record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr));
906909
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
907910
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
@@ -976,7 +979,6 @@ impl EncodeContext<'a, 'tcx> {
976979
);
977980
let rendered_const = self.lazy(RenderedConst(rendered));
978981

979-
record!(self.tables.def_kind[def_id] <- DefKind::AssocConst);
980982
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(
981983
container,
982984
Default::default(),
@@ -997,7 +999,6 @@ impl EncodeContext<'a, 'tcx> {
997999
} else {
9981000
bug!()
9991001
};
1000-
record!(self.tables.def_kind[def_id] <- DefKind::AssocFn);
10011002
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
10021003
fn_data,
10031004
container,
@@ -1006,7 +1007,6 @@ impl EncodeContext<'a, 'tcx> {
10061007
}
10071008
ty::AssocKind::Type => {
10081009
self.encode_explicit_item_bounds(def_id);
1009-
record!(self.tables.def_kind[def_id] <- DefKind::AssocTy);
10101010
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
10111011
}
10121012
}
@@ -1084,7 +1084,6 @@ impl EncodeContext<'a, 'tcx> {
10841084
if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind {
10851085
let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
10861086

1087-
record!(self.tables.def_kind[def_id] <- DefKind::AssocConst);
10881087
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(
10891088
container,
10901089
qualifs,
@@ -1104,15 +1103,13 @@ impl EncodeContext<'a, 'tcx> {
11041103
} else {
11051104
bug!()
11061105
};
1107-
record!(self.tables.def_kind[def_id] <- DefKind::AssocFn);
11081106
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
11091107
fn_data,
11101108
container,
11111109
has_self: impl_item.fn_has_self_parameter,
11121110
})));
11131111
}
11141112
ty::AssocKind::Type => {
1115-
record!(self.tables.def_kind[def_id] <- DefKind::AssocTy);
11161113
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
11171114
}
11181115
}
@@ -1251,19 +1248,12 @@ impl EncodeContext<'a, 'tcx> {
12511248

12521249
self.encode_ident_span(def_id, item.ident);
12531250

1254-
let (def_kind, entry_kind) = match item.kind {
1255-
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => {
1256-
(DefKind::Static, EntryKind::MutStatic)
1257-
}
1258-
hir::ItemKind::Static(_, hir::Mutability::Not, _) => {
1259-
(DefKind::Static, EntryKind::ImmStatic)
1260-
}
1251+
let entry_kind = match item.kind {
1252+
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
1253+
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
12611254
hir::ItemKind::Const(_, body_id) => {
12621255
let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
1263-
(
1264-
DefKind::Const,
1265-
EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id)),
1266-
)
1256+
EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id))
12671257
}
12681258
hir::ItemKind::Fn(ref sig, .., body) => {
12691259
let data = FnData {
@@ -1272,21 +1262,19 @@ impl EncodeContext<'a, 'tcx> {
12721262
param_names: self.encode_fn_param_names_for_body(body),
12731263
};
12741264

1275-
(DefKind::Fn, EntryKind::Fn(self.lazy(data)))
1265+
EntryKind::Fn(self.lazy(data))
12761266
}
12771267
hir::ItemKind::Mod(ref m) => {
12781268
return self.encode_info_for_mod(item.hir_id, m, &item.attrs);
12791269
}
1280-
hir::ItemKind::ForeignMod { .. } => (DefKind::ForeignMod, EntryKind::ForeignMod),
1281-
hir::ItemKind::GlobalAsm(..) => (DefKind::GlobalAsm, EntryKind::GlobalAsm),
1282-
hir::ItemKind::TyAlias(..) => (DefKind::TyAlias, EntryKind::Type),
1270+
hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod,
1271+
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
1272+
hir::ItemKind::TyAlias(..) => EntryKind::Type,
12831273
hir::ItemKind::OpaqueTy(..) => {
12841274
self.encode_explicit_item_bounds(def_id);
1285-
(DefKind::OpaqueTy, EntryKind::OpaqueTy)
1286-
}
1287-
hir::ItemKind::Enum(..) => {
1288-
(DefKind::Enum, EntryKind::Enum(self.tcx.adt_def(def_id).repr))
1275+
EntryKind::OpaqueTy
12891276
}
1277+
hir::ItemKind::Enum(..) => EntryKind::Enum(self.tcx.adt_def(def_id).repr),
12901278
hir::ItemKind::Struct(ref struct_def, _) => {
12911279
let adt_def = self.tcx.adt_def(def_id);
12921280
let variant = adt_def.non_enum_variant();
@@ -1298,31 +1286,29 @@ impl EncodeContext<'a, 'tcx> {
12981286
.ctor_hir_id()
12991287
.map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index);
13001288

1301-
let ek = EntryKind::Struct(
1289+
EntryKind::Struct(
13021290
self.lazy(VariantData {
13031291
ctor_kind: variant.ctor_kind,
13041292
discr: variant.discr,
13051293
ctor,
13061294
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
13071295
}),
13081296
adt_def.repr,
1309-
);
1310-
(DefKind::Struct, ek)
1297+
)
13111298
}
13121299
hir::ItemKind::Union(..) => {
13131300
let adt_def = self.tcx.adt_def(def_id);
13141301
let variant = adt_def.non_enum_variant();
13151302

1316-
let ek = EntryKind::Union(
1303+
EntryKind::Union(
13171304
self.lazy(VariantData {
13181305
ctor_kind: variant.ctor_kind,
13191306
discr: variant.discr,
13201307
ctor: None,
13211308
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
13221309
}),
13231310
adt_def.repr,
1324-
);
1325-
(DefKind::Union, ek)
1311+
)
13261312
}
13271313
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
13281314
let trait_ref = self.tcx.impl_trait_ref(def_id);
@@ -1352,7 +1338,7 @@ impl EncodeContext<'a, 'tcx> {
13521338
let data =
13531339
ImplData { polarity, defaultness, parent_impl: parent, coerce_unsized_info };
13541340

1355-
(DefKind::Impl, EntryKind::Impl(self.lazy(data)))
1341+
EntryKind::Impl(self.lazy(data))
13561342
}
13571343
hir::ItemKind::Trait(..) => {
13581344
let trait_def = self.tcx.trait_def(def_id);
@@ -1364,14 +1350,13 @@ impl EncodeContext<'a, 'tcx> {
13641350
specialization_kind: trait_def.specialization_kind,
13651351
};
13661352

1367-
(DefKind::Trait, EntryKind::Trait(self.lazy(data)))
1353+
EntryKind::Trait(self.lazy(data))
13681354
}
1369-
hir::ItemKind::TraitAlias(..) => (DefKind::TraitAlias, EntryKind::TraitAlias),
1355+
hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias,
13701356
hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {
13711357
bug!("cannot encode info for item {:?}", item)
13721358
}
13731359
};
1374-
record!(self.tables.def_kind[def_id] <- def_kind);
13751360
record!(self.tables.kind[def_id] <- entry_kind);
13761361
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
13771362
record!(self.tables.attributes[def_id] <- item.attrs);
@@ -1491,7 +1476,6 @@ impl EncodeContext<'a, 'tcx> {
14911476
/// Serialize the text of exported macros
14921477
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
14931478
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id();
1494-
record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang));
14951479
record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
14961480
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
14971481
record!(self.tables.attributes[def_id] <- macro_def.attrs);
@@ -1500,14 +1484,7 @@ impl EncodeContext<'a, 'tcx> {
15001484
self.encode_deprecation(def_id);
15011485
}
15021486

1503-
fn encode_info_for_generic_param(
1504-
&mut self,
1505-
def_id: DefId,
1506-
def_kind: DefKind,
1507-
kind: EntryKind,
1508-
encode_type: bool,
1509-
) {
1510-
record!(self.tables.def_kind[def_id] <- def_kind);
1487+
fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) {
15111488
record!(self.tables.kind[def_id] <- kind);
15121489
if encode_type {
15131490
self.encode_item_type(def_id);
@@ -1525,12 +1502,10 @@ impl EncodeContext<'a, 'tcx> {
15251502
match ty.kind() {
15261503
ty::Generator(..) => {
15271504
let data = self.tcx.generator_kind(def_id).unwrap();
1528-
record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Generator);
15291505
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator(data));
15301506
}
15311507

15321508
ty::Closure(..) => {
1533-
record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Closure);
15341509
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure);
15351510
}
15361511

@@ -1559,7 +1534,6 @@ impl EncodeContext<'a, 'tcx> {
15591534
let const_data = self.encode_rendered_const_for_body(body_id);
15601535
let qualifs = self.tcx.mir_const_qualif(def_id);
15611536

1562-
record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst);
15631537
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
15641538
self.encode_item_type(def_id.to_def_id());
15651539
self.encode_generics(def_id.to_def_id());
@@ -1822,19 +1796,15 @@ impl EncodeContext<'a, 'tcx> {
18221796
},
18231797
param_names: self.encode_fn_param_names(names),
18241798
};
1825-
record!(self.tables.def_kind[def_id] <- DefKind::Fn);
18261799
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
18271800
}
18281801
hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => {
1829-
record!(self.tables.def_kind[def_id] <- DefKind::Static);
18301802
record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic);
18311803
}
18321804
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => {
1833-
record!(self.tables.def_kind[def_id] <- DefKind::Static);
18341805
record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic);
18351806
}
18361807
hir::ForeignItemKind::Type => {
1837-
record!(self.tables.def_kind[def_id] <- DefKind::ForeignTy);
18381808
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);
18391809
}
18401810
}
@@ -1912,7 +1882,6 @@ impl EncodeContext<'a, 'tcx> {
19121882
GenericParamKind::Type { ref default, .. } => {
19131883
self.encode_info_for_generic_param(
19141884
def_id.to_def_id(),
1915-
DefKind::TyParam,
19161885
EntryKind::TypeParam,
19171886
default.is_some(),
19181887
);
@@ -1923,7 +1892,6 @@ impl EncodeContext<'a, 'tcx> {
19231892
GenericParamKind::Const { .. } => {
19241893
self.encode_info_for_generic_param(
19251894
def_id.to_def_id(),
1926-
DefKind::ConstParam,
19271895
EntryKind::ConstParam,
19281896
true,
19291897
);

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use self::collector::NodeCollector;
22

33
use crate::hir::{Owner, OwnerNodes};
4-
use crate::ty::query::Providers;
54
use crate::ty::TyCtxt;
65
use rustc_ast as ast;
76
use rustc_data_structures::svh::Svh;
@@ -187,14 +186,14 @@ impl<'hir> Map<'hir> {
187186
self.tcx.definitions.iter_local_def_id()
188187
}
189188

190-
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
189+
pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
191190
// FIXME(eddyb) support `find` on the crate root.
192191
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
193-
return DefKind::Mod;
192+
return Some(DefKind::Mod);
194193
}
195194

196195
let hir_id = self.local_def_id_to_hir_id(local_def_id);
197-
match self.get(hir_id) {
196+
let def_kind = match self.find(hir_id)? {
198197
Node::Item(item) => match item.kind {
199198
ItemKind::Static(..) => DefKind::Static,
200199
ItemKind::Const(..) => DefKind::Const,
@@ -265,8 +264,14 @@ impl<'hir> Map<'hir> {
265264
| Node::Lifetime(_)
266265
| Node::Visibility(_)
267266
| Node::Block(_)
268-
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
269-
}
267+
| Node::Crate(_) => return None,
268+
};
269+
Some(def_kind)
270+
}
271+
272+
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
273+
self.opt_def_kind(local_def_id)
274+
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", local_def_id))
270275
}
271276

272277
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
@@ -1110,7 +1115,3 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
11101115
None => format!("unknown node{}", id_str),
11111116
}
11121117
}
1113-
1114-
pub fn provide(providers: &mut Providers) {
1115-
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
1116-
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ pub fn provide(providers: &mut Providers) {
9494
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
9595
}
9696
};
97-
map::provide(providers);
97+
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
9898
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ rustc_queries! {
878878
cache_on_disk_if { true }
879879
}
880880

881-
query def_kind(def_id: DefId) -> DefKind {
881+
query opt_def_kind(def_id: DefId) -> Option<DefKind> {
882882
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
883883
}
884884

0 commit comments

Comments
 (0)