Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit 05f2336

Browse files
authored
Merge pull request #103 from Xanewok/further-tweaks
Further simplify and explain crate id mapping
2 parents 8a8c998 + 6c28fe8 commit 05f2336

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

src/analysis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Analysis {
187187
if def.is_none() {
188188
info!("def not found for {}", id);
189189
}
190-
def.map(|def| f(*id, &def))
190+
def.map(|def| f(*id, def))
191191
})
192192
.collect(),
193193
);
@@ -215,7 +215,7 @@ impl Analysis {
215215
self.for_all_crates(|c| {
216216
c.def_names.get(name).map(|ids| {
217217
ids.into_iter()
218-
.flat_map(|id| c.defs.get(id).map(|def| def.clone()))
218+
.flat_map(|id| c.defs.get(id).cloned())
219219
.collect()
220220
})
221221
})

src/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub struct SymbolResult {
6262
impl SymbolResult {
6363
fn new(id: Id, def: &Def) -> SymbolResult {
6464
SymbolResult {
65-
id: id,
65+
id,
6666
name: def.name.clone(),
6767
span: def.span.clone(),
6868
kind: def.kind,
@@ -97,18 +97,18 @@ impl AnalysisHost<CargoAnalysisLoader> {
9797
master_crate_map: Mutex::new(HashMap::new()),
9898
loader: CargoAnalysisLoader {
9999
path_prefix: Mutex::new(None),
100-
target: target,
100+
target,
101101
},
102102
}
103103
}
104104
}
105105

106106
impl<L: AnalysisLoader> AnalysisHost<L> {
107-
pub fn new_with_loader(l: L) -> AnalysisHost<L> {
108-
AnalysisHost {
107+
pub fn new_with_loader(loader: L) -> Self {
108+
Self {
109109
analysis: Mutex::new(None),
110110
master_crate_map: Mutex::new(HashMap::new()),
111-
loader: l,
111+
loader,
112112
}
113113
}
114114

@@ -167,12 +167,11 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
167167

168168
let raw_analysis = read_analysis_incremental(&self.loader, timestamps, blacklist);
169169

170-
let result = lowering::lower(raw_analysis, base_dir, self, |host, per_crate, path| {
170+
lowering::lower(raw_analysis, base_dir, self, |host, per_crate, path| {
171171
let mut a = host.analysis.lock()?;
172172
a.as_mut().unwrap().update(per_crate, path);
173173
Ok(())
174-
});
175-
result
174+
})
176175
}
177176

178177
// Reloads the entire project's analysis data.
@@ -226,8 +225,8 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
226225
Ok(())
227226
}
228227

229-
/// Note that self.has_def == true =/> self.goto_def.is_some(), since if the
230-
/// def is in an api crate, there is no reasonable span to jump to.
228+
/// Note that `self.has_def()` =/> `self.goto_def().is_ok()`, since if the
229+
/// Def is in an api crate, there is no reasonable Span to jump to.
231230
pub fn has_def(&self, id: Id) -> bool {
232231
match self.analysis.lock() {
233232
Ok(a) => a.as_ref().unwrap().has_def(id),
@@ -416,7 +415,7 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
416415

417416
pub fn find_impls(&self, id: Id) -> AResult<Vec<Span>> {
418417
self.with_analysis(|a| {
419-
Some(a.for_all_crates(|c| c.impls.get(&id).map(|v| v.clone())))
418+
Some(a.for_all_crates(|c| c.impls.get(&id).cloned()))
420419
})
421420
}
422421

@@ -490,7 +489,7 @@ impl<L: AnalysisLoader> AnalysisHost<L> {
490489
analysis.with_defs(p, |parent| match def.kind {
491490
DefKind::Field | DefKind::Method | DefKind::Tuple => {
492491
let ns = name_space_for_def_kind(def.kind);
493-
let mut res = AnalysisHost::<L>::mk_doc_url(&parent, analysis)
492+
let mut res = AnalysisHost::<L>::mk_doc_url(parent, analysis)
494493
.unwrap_or_else(|| "".into());
495494
res.push_str(&format!("#{}.{}", def.name, ns));
496495
res

src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn extract_target_triple(sys_root_path: &Path) -> String {
9898
.expect("extracting toolchain failed");
9999
// Extracts x86_64-pc-windows-msvc from nightly-x86_64-pc-windows-pc
100100
let triple = toolchain
101-
.splitn(2, "-")
101+
.splitn(2, '-')
102102
.last()
103103
.map(String::from)
104104
.expect("extracting triple failed");

src/lowering.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ fn lower_span(raw_span: &raw::SpanData, base_dir: &Path) -> Span {
8585
}
8686

8787
struct CrateReader {
88+
/// This is effectively a map from local crate id -> global crate id, where
89+
/// local crate id are indices 0...external_crate_count.
8890
crate_map: Vec<u32>,
8991
base_dir: PathBuf,
9092
crate_name: String,
@@ -96,23 +98,27 @@ impl CrateReader {
9698
master_crate_map: &mut HashMap<String, u32>,
9799
base_dir: &Path,
98100
) -> CrateReader {
99-
// println!("building crate map for {}", crate_name);
100-
let next = master_crate_map.len() as u32;
101-
let mut crate_map = vec![
102-
*master_crate_map
103-
.entry(prelude.crate_name.clone())
104-
.or_insert_with(|| next),
105-
];
106-
// println!(" {} -> {}", crate_name, master_crate_map[&crate_name]);
101+
fn fetch_crate_id(map: &mut HashMap<String, u32>, crate_name: &String) -> u32 {
102+
let next = map.len() as u32;
103+
*map.entry(crate_name.clone()).or_insert(next)
104+
}
105+
// When reading a local crate and its external crates, we need to:
106+
// 1. Update a global crate id map if we encounter any new crate
107+
// 2. Prepare a local crate id -> global crate id map, so we can easily
108+
// map those when lowering symbols with local crate ids into global registry
109+
// It's worth noting, that we assume that local crate id is 0, whereas
110+
// the external crates will have num in 1..count contiguous range.
111+
trace!("building crate map for {}", prelude.crate_name);
112+
let id = fetch_crate_id(master_crate_map, &prelude.crate_name);
113+
let mut crate_map = vec![id];
114+
trace!(" {} -> {}", prelude.crate_name, master_crate_map[&prelude.crate_name]);
107115

108116
prelude.external_crates.sort_by(|a, b| a.num.cmp(&b.num));
109117
for c in prelude.external_crates {
110118
assert!(c.num == crate_map.len() as u32);
111-
let next = master_crate_map.len() as u32;
112-
crate_map.push(*master_crate_map
113-
.entry(c.name.clone())
114-
.or_insert_with(|| next));
115-
// println!(" {} -> {}", c.name, master_crate_map[&c.name]);
119+
let id = fetch_crate_id(master_crate_map, &c.name);
120+
crate_map.push(id);
121+
trace!(" {} -> {}", c.name, master_crate_map[&c.name]);
116122
}
117123

118124
CrateReader {
@@ -221,7 +227,7 @@ impl CrateReader {
221227
docs: d.docs,
222228
// sig: d.sig.map(|ref s| self.lower_sig(s, &self.base_dir)),
223229
};
224-
info!(
230+
trace!(
225231
"record def: {:?}/{:?} ({}): {:?}",
226232
id,
227233
d.id,
@@ -334,15 +340,15 @@ impl CrateReader {
334340
// }
335341
// }
336342

343+
/// Recreates resulting crate-local (u32, u32) id from compiler to a global
344+
/// `u64` `Id`, mapping from a local to global crate id.
337345
fn id_from_compiler_id(&self, id: &data::Id) -> Id {
338346
if id.krate == u32::MAX || id.index == u32::MAX {
339347
return NULL;
340348
}
341-
// We build an id by looking up the local crate number into a global crate number and using
342-
// that for the high order bits, then use the index for the least significant bits.
343349

344350
let krate = self.crate_map[id.krate as usize] as u64;
345-
351+
// Use global crate number for high order bits, then index for least significant bits.
346352
Id((krate << 32) | (id.index as u64))
347353
}
348354
}

0 commit comments

Comments
 (0)