Skip to content

Commit 2c7e83f

Browse files
committed
Move path_len to ExternCrate
1 parent e986510 commit 2c7e83f

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

src/librustc/ich/impls_cstore.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! from rustc::middle::cstore in no particular order.
1313
1414
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
15-
use ich::StableHashingContext;
1615

1716
use middle;
1817

@@ -50,29 +49,15 @@ impl_stable_hash_for!(enum middle::cstore::LinkagePreference {
5049
impl_stable_hash_for!(struct middle::cstore::ExternCrate {
5150
src,
5251
span,
52+
path_len,
5353
direct
5454
});
5555

56-
impl<'a> HashStable<StableHashingContext<'a>> for middle::cstore::ExternCrateSource {
57-
fn hash_stable<W: StableHasherResult>(
58-
&self,
59-
hcx: &mut StableHashingContext<'a>,
60-
hasher: &mut StableHasher<W>,
61-
) {
62-
use middle::cstore::ExternCrateSource::*;
63-
64-
::std::mem::discriminant(self).hash_stable(hcx, hasher);
65-
66-
match *self {
67-
Extern { def_id, path_len } => {
68-
def_id.hash_stable(hcx, hasher);
69-
path_len.hash_stable(hcx, hasher);
70-
}
71-
Use { path_len } => path_len.hash_stable(hcx, hasher),
72-
Path => {}
73-
}
74-
}
75-
}
56+
impl_stable_hash_for!(enum middle::cstore::ExternCrateSource {
57+
Extern(def_id),
58+
Use,
59+
Path,
60+
});
7661

7762
impl_stable_hash_for!(struct middle::cstore::CrateSource {
7863
dylib,

src/librustc/middle/cstore.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ pub struct ExternCrate {
153153
/// span of the extern crate that caused this to be loaded
154154
pub span: Span,
155155

156+
/// Number of links to reach the extern;
157+
/// used to select the extern with the shortest path
158+
pub path_len: usize,
159+
156160
/// If true, then this crate is the crate named by the extern
157161
/// crate referenced above. If false, then this crate is a dep
158162
/// of the crate.
@@ -162,21 +166,14 @@ pub struct ExternCrate {
162166
#[derive(Copy, Clone, Debug)]
163167
pub enum ExternCrateSource {
164168
/// Crate is loaded by `extern crate`.
165-
Extern {
169+
Extern(
166170
/// def_id of the item in the current crate that caused
167171
/// this crate to be loaded; note that there could be multiple
168172
/// such ids
169-
def_id: DefId,
170-
171-
/// Number of links to reach the extern crate `def_id`
172-
/// declaration; used to select the extern crate with the shortest
173-
/// path
174-
path_len: usize,
175-
},
173+
DefId,
174+
),
176175
// Crate is loaded by `use`.
177-
Use {
178-
path_len: usize,
179-
},
176+
Use,
180177
/// Crate is implicitly loaded by an absolute or an `extern::` path.
181178
Path,
182179
}

src/librustc/ty/item_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
104104
if cnum != LOCAL_CRATE {
105105
let opt_extern_crate = self.extern_crate(cnum.as_def_id());
106106
if let Some(ExternCrate {
107-
src: ExternCrateSource::Extern { def_id, .. },
107+
src: ExternCrateSource::Extern(def_id),
108108
direct: true,
109109
..
110110
}) = *opt_extern_crate
@@ -138,7 +138,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
138138
if cur_def.index == CRATE_DEF_INDEX {
139139
match *self.extern_crate(cur_def) {
140140
Some(ExternCrate {
141-
src: ExternCrateSource::Extern { def_id, .. },
141+
src: ExternCrateSource::Extern(def_id),
142142
direct: true,
143143
..
144144
}) => {

src/librustc_metadata/creader.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -367,29 +367,21 @@ impl<'a> CrateLoader<'a> {
367367
let cmeta = self.cstore.get_crate_data(cnum);
368368
let mut old_extern_crate = cmeta.extern_crate.borrow_mut();
369369

370-
fn path_len_reverse(src: ExternCrateSource) -> cmp::Reverse<usize> {
371-
cmp::Reverse(match src {
372-
ExternCrateSource::Extern { path_len, .. } |
373-
ExternCrateSource::Use { path_len } => path_len,
374-
_ => usize::max_value(),
375-
})
376-
}
377-
378370
// Prefer:
379371
// - something over nothing (tuple.0);
380372
// - direct extern crate to indirect (tuple.1);
381373
// - shorter paths to longer (tuple.2).
382374
let new_rank = (
383375
true,
384376
extern_crate.direct,
385-
path_len_reverse(extern_crate.src),
377+
cmp::Reverse(extern_crate.path_len),
386378
);
387379
let old_rank = match *old_extern_crate {
388380
None => (false, false, cmp::Reverse(usize::max_value())),
389381
Some(ref c) => (
390382
true,
391383
c.direct,
392-
path_len_reverse(c.src),
384+
cmp::Reverse(c.path_len),
393385
),
394386
};
395387
if old_rank >= new_rank {
@@ -1089,8 +1081,9 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10891081
self.update_extern_crate(
10901082
cnum,
10911083
ExternCrate {
1092-
src: ExternCrateSource::Extern { def_id, path_len },
1084+
src: ExternCrateSource::Extern(def_id),
10931085
span: item.span,
1086+
path_len,
10941087
direct: true,
10951088
},
10961089
&mut FxHashSet(),
@@ -1116,6 +1109,8 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
11161109
ExternCrate {
11171110
src: ExternCrateSource::Path,
11181111
span,
1112+
// to have the least priority in `update_extern_crate`
1113+
path_len: usize::max_value(),
11191114
direct: true,
11201115
},
11211116
&mut FxHashSet(),
@@ -1141,8 +1136,9 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
11411136
self.update_extern_crate(
11421137
cnum,
11431138
ExternCrate {
1144-
src: ExternCrateSource::Use { path_len },
1139+
src: ExternCrateSource::Use,
11451140
span,
1141+
path_len,
11461142
direct: true,
11471143
},
11481144
&mut FxHashSet(),

0 commit comments

Comments
 (0)