]> git.lizzy.rs Git - rust.git/commitdiff
rustc_metadata: Merge `get_ctor_def_id` and `get_ctor_kind`
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Tue, 21 Dec 2021 09:41:02 +0000 (17:41 +0800)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 22 Dec 2021 03:05:54 +0000 (11:05 +0800)
Also avoid decoding the whole `ty::AssocItem` to get a `has_self` flag

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

index 09c6cb010b04dc7b3693c3b5056e476405423b2e..5a67c91adcbc2bb90d4e2ec0392d4e9a0d417a31 100644 (file)
@@ -1161,8 +1161,9 @@ fn each_child_of_item(&self, id: DefIndex, mut callback: impl FnMut(Export), ses
                     // Re-export lists automatically contain constructors when necessary.
                     match kind {
                         DefKind::Struct => {
-                            if let Some(ctor_def_id) = self.get_ctor_def_id(child_index) {
-                                let ctor_kind = self.get_ctor_kind(child_index);
+                            if let Some((ctor_def_id, ctor_kind)) =
+                                self.get_ctor_def_id_and_kind(child_index)
+                            {
                                 let ctor_res =
                                     Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
                                 let vis = self.get_visibility(ctor_def_id.index);
@@ -1174,8 +1175,9 @@ fn each_child_of_item(&self, id: DefIndex, mut callback: impl FnMut(Export), ses
                             // value namespace, they are reserved for possible future use.
                             // It's ok to use the variant's id as a ctor id since an
                             // error will be reported on any use of such resolution anyway.
-                            let ctor_def_id = self.get_ctor_def_id(child_index).unwrap_or(def_id);
-                            let ctor_kind = self.get_ctor_kind(child_index);
+                            let (ctor_def_id, ctor_kind) = self
+                                .get_ctor_def_id_and_kind(child_index)
+                                .unwrap_or((def_id, CtorKind::Fictive));
                             let ctor_res =
                                 Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
                             let mut vis = self.get_visibility(ctor_def_id.index);
@@ -1296,6 +1298,13 @@ fn mir_const_qualif(&self, id: DefIndex) -> mir::ConstQualifs {
         }
     }
 
+    fn get_fn_has_self_parameter(&self, id: DefIndex) -> bool {
+        match self.kind(id) {
+            EntryKind::AssocFn(data) => data.decode(self).has_self,
+            _ => false,
+        }
+    }
+
     fn get_associated_item(&self, id: DefIndex, sess: &Session) -> ty::AssocItem {
         let def_key = self.def_key(id);
         let parent = self.local_def_id(def_key.parent.unwrap());
@@ -1326,22 +1335,11 @@ fn get_item_variances(&'a self, id: DefIndex) -> impl Iterator<Item = ty::Varian
         self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self)
     }
 
-    fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
+    fn get_ctor_def_id_and_kind(&self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
         match self.kind(node_id) {
-            EntryKind::Struct(data, _) | EntryKind::Union(data, _) | EntryKind::Variant(data) => {
-                data.decode(self).ctor_kind
-            }
-            _ => CtorKind::Fictive,
-        }
-    }
-
-    fn get_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
-        match self.kind(node_id) {
-            EntryKind::Struct(data, _) => {
-                data.decode(self).ctor.map(|index| self.local_def_id(index))
-            }
-            EntryKind::Variant(data) => {
-                data.decode(self).ctor.map(|index| self.local_def_id(index))
+            EntryKind::Struct(data, _) | EntryKind::Variant(data) => {
+                let vdata = data.decode(self);
+                vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind))
             }
             _ => None,
         }
index 4e5d21049a0d98597bb419df916ad002b851fa29..72a04e7042a113c0b5acf81ff88ffd137e4b486a 100644 (file)
@@ -388,9 +388,7 @@ pub fn struct_field_visibilities_untracked(&self, def: DefId) -> Vec<Visibility>
     }
 
     pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> {
-        self.get_crate_data(def.krate).get_ctor_def_id(def.index).map(|ctor_def_id| {
-            (ctor_def_id, self.get_crate_data(def.krate).get_ctor_kind(def.index))
-        })
+        self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index)
     }
 
     pub fn visibility_untracked(&self, def: DefId) -> Visibility {
@@ -439,8 +437,8 @@ pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {
         )
     }
 
-    pub fn associated_item_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::AssocItem {
-        self.get_crate_data(def.krate).get_associated_item(def.index, sess)
+    pub fn fn_has_self_parameter_untracked(&self, def: DefId) -> bool {
+        self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index)
     }
 
     pub fn crate_source_untracked(&self, cnum: CrateNum) -> CrateSource {
index 74edc3a2d5e6ff4aab7c25e957a171beafdb59f9..d57591186d87cacd58e8479f0cbb498f986afdd4 100644 (file)
@@ -1016,10 +1016,7 @@ fn build_reduced_graph_for_external_crate_res(&mut self, child: Export) {
                 self.insert_field_names(def_id, field_names);
             }
             Res::Def(DefKind::AssocFn, def_id) => {
-                if cstore
-                    .associated_item_cloned_untracked(def_id, self.r.session)
-                    .fn_has_self_parameter
-                {
+                if cstore.fn_has_self_parameter_untracked(def_id) {
                     self.r.has_self.insert(def_id);
                 }
             }