]> git.lizzy.rs Git - rust.git/commitdiff
Move `path_len` to ExternCrate
authorShotaro Yamada <sinkuu@sinkuu.xyz>
Fri, 13 Apr 2018 00:31:43 +0000 (09:31 +0900)
committerShotaro Yamada <sinkuu@sinkuu.xyz>
Fri, 13 Apr 2018 00:31:43 +0000 (09:31 +0900)
src/librustc/ich/impls_cstore.rs
src/librustc/middle/cstore.rs
src/librustc/ty/item_path.rs
src/librustc_metadata/creader.rs

index 0e33c64333dab24155535ba75c98268cefd14d96..d885bd43bc89dbce656a42db81c6e5aa475bf3cb 100644 (file)
@@ -12,7 +12,6 @@
 //! from rustc::middle::cstore in no particular order.
 
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
-use ich::StableHashingContext;
 
 use middle;
 
 impl_stable_hash_for!(struct middle::cstore::ExternCrate {
     src,
     span,
+    path_len,
     direct
 });
 
-impl<'a> HashStable<StableHashingContext<'a>> for middle::cstore::ExternCrateSource {
-    fn hash_stable<W: StableHasherResult>(
-        &self,
-        hcx: &mut StableHashingContext<'a>,
-        hasher: &mut StableHasher<W>,
-    ) {
-        use middle::cstore::ExternCrateSource::*;
-
-        ::std::mem::discriminant(self).hash_stable(hcx, hasher);
-
-        match *self {
-            Extern { def_id, path_len } => {
-                def_id.hash_stable(hcx, hasher);
-                path_len.hash_stable(hcx, hasher);
-            }
-            Use { path_len } => path_len.hash_stable(hcx, hasher),
-            Path => {}
-        }
-    }
-}
+impl_stable_hash_for!(enum middle::cstore::ExternCrateSource {
+    Extern(def_id),
+    Use,
+    Path,
+});
 
 impl_stable_hash_for!(struct middle::cstore::CrateSource {
     dylib,
index 6208a8a767c108b7d1cbb26f5481ad162c0780da..81fe723deb905885ac9330bc5fe9da39ca4a3d73 100644 (file)
@@ -153,6 +153,10 @@ pub struct ExternCrate {
     /// span of the extern crate that caused this to be loaded
     pub span: Span,
 
+    /// Number of links to reach the extern;
+    /// used to select the extern with the shortest path
+    pub path_len: usize,
+
     /// If true, then this crate is the crate named by the extern
     /// crate referenced above. If false, then this crate is a dep
     /// of the crate.
@@ -162,21 +166,14 @@ pub struct ExternCrate {
 #[derive(Copy, Clone, Debug)]
 pub enum ExternCrateSource {
     /// Crate is loaded by `extern crate`.
-    Extern {
+    Extern(
         /// def_id of the item in the current crate that caused
         /// this crate to be loaded; note that there could be multiple
         /// such ids
-        def_id: DefId,
-
-        /// Number of links to reach the extern crate `def_id`
-        /// declaration; used to select the extern crate with the shortest
-        /// path
-        path_len: usize,
-    },
+        DefId,
+    ),
     // Crate is loaded by `use`.
-    Use {
-        path_len: usize,
-    },
+    Use,
     /// Crate is implicitly loaded by an absolute or an `extern::` path.
     Path,
 }
index 771bdcc55e39749e59c3ae1bc796fa2101e7946e..8189064db6968eeecfcc353eeb03ee8326b419c6 100644 (file)
@@ -104,7 +104,7 @@ pub fn push_krate_path<T>(self, buffer: &mut T, cnum: CrateNum)
                 if cnum != LOCAL_CRATE {
                     let opt_extern_crate = self.extern_crate(cnum.as_def_id());
                     if let Some(ExternCrate {
-                        src: ExternCrateSource::Extern { def_id, .. },
+                        src: ExternCrateSource::Extern(def_id),
                         direct: true,
                         ..
                     }) = *opt_extern_crate
@@ -138,7 +138,7 @@ pub fn try_push_visible_item_path<T>(self, buffer: &mut T, external_def_id: DefI
             if cur_def.index == CRATE_DEF_INDEX {
                 match *self.extern_crate(cur_def) {
                     Some(ExternCrate {
-                        src: ExternCrateSource::Extern { def_id, .. },
+                        src: ExternCrateSource::Extern(def_id),
                         direct: true,
                         ..
                     }) => {
index 268624cf430938508adc6b38c7def2a6a7de0e06..f6692579ffa3340d400180f5c0a855c274e9a193 100644 (file)
@@ -367,14 +367,6 @@ fn update_extern_crate(&mut self,
         let cmeta = self.cstore.get_crate_data(cnum);
         let mut old_extern_crate = cmeta.extern_crate.borrow_mut();
 
-        fn path_len_reverse(src: ExternCrateSource) -> cmp::Reverse<usize> {
-            cmp::Reverse(match src {
-                ExternCrateSource::Extern { path_len, .. } |
-                ExternCrateSource::Use { path_len } => path_len,
-                _ => usize::max_value(),
-            })
-        }
-
         // Prefer:
         // - something over nothing (tuple.0);
         // - direct extern crate to indirect (tuple.1);
@@ -382,14 +374,14 @@ fn path_len_reverse(src: ExternCrateSource) -> cmp::Reverse<usize> {
         let new_rank = (
             true,
             extern_crate.direct,
-            path_len_reverse(extern_crate.src),
+            cmp::Reverse(extern_crate.path_len),
         );
         let old_rank = match *old_extern_crate {
             None => (false, false, cmp::Reverse(usize::max_value())),
             Some(ref c) => (
                 true,
                 c.direct,
-                path_len_reverse(c.src),
+                cmp::Reverse(c.path_len),
             ),
         };
         if old_rank >= new_rank {
@@ -1089,8 +1081,9 @@ fn process_extern_crate(&mut self, item: &ast::Item, definitions: &Definitions)
                 self.update_extern_crate(
                     cnum,
                     ExternCrate {
-                        src: ExternCrateSource::Extern { def_id, path_len },
+                        src: ExternCrateSource::Extern(def_id),
                         span: item.span,
+                        path_len,
                         direct: true,
                     },
                     &mut FxHashSet(),
@@ -1116,6 +1109,8 @@ fn process_path_extern(
             ExternCrate {
                 src: ExternCrateSource::Path,
                 span,
+                // to have the least priority in `update_extern_crate`
+                path_len: usize::max_value(),
                 direct: true,
             },
             &mut FxHashSet(),
@@ -1141,8 +1136,9 @@ fn process_use_extern(
         self.update_extern_crate(
             cnum,
             ExternCrate {
-                src: ExternCrateSource::Use { path_len },
+                src: ExternCrateSource::Use,
                 span,
+                path_len,
                 direct: true,
             },
             &mut FxHashSet(),