Skip to content

Commit 337ced2

Browse files
committed
rustc_metadata: Merge get_ctor_def_id and get_ctor_kind
Also avoid decoding the whole `ty::AssocItem` to get a `has_self` flag
1 parent e100ec5 commit 337ced2

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11611161
// Re-export lists automatically contain constructors when necessary.
11621162
match kind {
11631163
DefKind::Struct => {
1164-
if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
1165-
let ctor_kind = self.get_ctor_kind(child_index);
1164+
if let Some((ctor_def_id, ctor_kind)) =
1165+
self.get_ctor_def_id_and_kind(child_index)
1166+
{
11661167
let ctor_res =
11671168
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
11681169
let vis = self.get_visibility(ctor_def_id.index);
@@ -1174,8 +1175,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11741175
// value namespace, they are reserved for possible future use.
11751176
// It's ok to use the variant's id as a ctor id since an
11761177
// error will be reported on any use of such resolution anyway.
1177-
let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
1178-
let ctor_kind = self.get_ctor_kind(child_index);
1178+
let (ctor_def_id, ctor_kind) = self
1179+
.get_ctor_def_id_and_kind(child_index)
1180+
.unwrap_or((def_id, CtorKind::Fictive));
11791181
let ctor_res =
11801182
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
11811183
let mut vis = self.get_visibility(ctor_def_id.index);
@@ -1296,6 +1298,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12961298
}
12971299
}
12981300

1301+
fn get_fn_has_self_parameter(&self, id: DefIndex) -> bool {
1302+
match self.kind(id) {
1303+
EntryKind::AssocFn(data) => data.decode(self).has_self,
1304+
_ => false,
1305+
}
1306+
}
1307+
12991308
fn get_associated_item(&self, id: DefIndex, sess: &Session) -> ty::AssocItem {
13001309
let def_key = self.def_key(id);
13011310
let parent = self.local_def_id(def_key.parent.unwrap());
@@ -1326,22 +1335,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13261335
self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self)
13271336
}
13281337

1329-
fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
1338+
fn get_ctor_def_id_and_kind(&self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
13301339
match self.kind(node_id) {
1331-
EntryKind::Struct(data, _) | EntryKind::Union(data, _) | EntryKind::Variant(data) => {
1332-
data.decode(self).ctor_kind
1333-
}
1334-
_ => CtorKind::Fictive,
1335-
}
1336-
}
1337-
1338-
fn get_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
1339-
match self.kind(node_id) {
1340-
EntryKind::Struct(data, _) => {
1341-
data.decode(self).ctor.map(|index| self.local_def_id(index))
1342-
}
1343-
EntryKind::Variant(data) => {
1344-
data.decode(self).ctor.map(|index| self.local_def_id(index))
1340+
EntryKind::Struct(data, _) | EntryKind::Variant(data) => {
1341+
let vdata = data.decode(self);
1342+
vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
13451343
}
13461344
_ => None,
13471345
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ impl CStore {
388388
}
389389

390390
pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> {
391-
self.get_crate_data(def.krate).get_ctor_def_id(def.index).map(|ctor_def_id| {
392-
(ctor_def_id, self.get_crate_data(def.krate).get_ctor_kind(def.index))
393-
})
391+
self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index)
394392
}
395393

396394
pub fn visibility_untracked(&self, def: DefId) -> Visibility {
@@ -439,8 +437,8 @@ impl CStore {
439437
)
440438
}
441439

442-
pub fn associated_item_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::AssocItem {
443-
self.get_crate_data(def.krate).get_associated_item(def.index, sess)
440+
pub fn fn_has_self_parameter_untracked(&self, def: DefId) -> bool {
441+
self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index)
444442
}
445443

446444
pub fn crate_source_untracked(&self, cnum: CrateNum) -> CrateSource {

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10161016
self.insert_field_names(def_id, field_names);
10171017
}
10181018
Res::Def(DefKind::AssocFn, def_id) => {
1019-
if cstore
1020-
.associated_item_cloned_untracked(def_id, self.r.session)
1021-
.fn_has_self_parameter
1022-
{
1019+
if cstore.fn_has_self_parameter_untracked(def_id) {
10231020
self.r.has_self.insert(def_id);
10241021
}
10251022
}

0 commit comments

Comments
 (0)