Skip to content

Commit 363a56b

Browse files
committed
Implemented crate tracking mechanism for rust-lang#17.
1 parent 60ac97f commit 363a56b

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/semcheck/mapping.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! map used to temporarily match up unsorted item sequences' elements by name.
55
66
use rustc::hir::def::{Def, Export};
7-
use rustc::hir::def_id::DefId;
7+
use rustc::hir::def_id::{CrateNum, DefId};
88
use rustc::ty::TypeParameterDef;
99

1010
use std::collections::{BTreeSet, HashMap, VecDeque};
@@ -16,8 +16,11 @@ use syntax::ast::Name;
1616
/// Definitions and simple `DefId` mappings are kept separate to record both kinds of
1717
/// correspondence losslessly. The *access* to the stored data happens through the same API,
1818
/// however.
19-
#[derive(Default)]
2019
pub struct IdMapping {
20+
/// The old crate.
21+
old_crate: CrateNum,
22+
/// The new crate.
23+
new_crate: CrateNum,
2124
/// Toplevel items' old `DefId` mapped to old and new `Def`.
2225
toplevel_mapping: HashMap<DefId, (Def, Def)>,
2326
/// Trait items' old `DefId` mapped to old and new `Def`.
@@ -31,6 +34,18 @@ pub struct IdMapping {
3134
}
3235

3336
impl IdMapping {
37+
pub fn new(old_crate: CrateNum, new_crate: CrateNum) -> IdMapping {
38+
IdMapping {
39+
old_crate: old_crate,
40+
new_crate: new_crate,
41+
toplevel_mapping: HashMap::new(),
42+
trait_item_mapping: HashMap::new(),
43+
internal_mapping: HashMap::new(),
44+
child_mapping: HashMap::new(),
45+
type_params: HashMap::new(),
46+
}
47+
}
48+
3449
/// Register two exports representing the same item across versions.
3550
pub fn add_export(&mut self, old: Def, new: Def) -> bool {
3651
if self.toplevel_mapping.contains_key(&old.def_id()) {
@@ -132,6 +147,14 @@ impl IdMapping {
132147
.get(&parent)
133148
.map(|m| m.iter().map(move |old| (*old, self.internal_mapping[old])))
134149
}
150+
151+
pub fn in_old_crate(&self, did: DefId) -> bool {
152+
self.old_crate == did.krate
153+
}
154+
155+
pub fn in_new_crate(&self, did: DefId) -> bool {
156+
self.new_crate == did.krate
157+
}
135158
}
136159

137160
/// A mapping from names to pairs of old and new exports.

src/semcheck/mismatch.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! essentially just renamed instances of the same item (as long as they are both unknown to us
55
//! at the time of analysis). Thus, we may match them up to avoid some false positives.
66
7-
use rustc::hir::def_id::{CrateNum, DefId};
7+
use rustc::hir::def_id::DefId;
88
use rustc::ty;
99
use rustc::ty::{Ty, TyCtxt};
1010
use rustc::ty::Visibility::Public;
@@ -27,20 +27,17 @@ pub struct Mismatch<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
2727
item_queue: VecDeque<(DefId, DefId)>,
2828
/// The id mapping to use.
2929
id_mapping: &'a mut IdMapping,
30-
/// The old crate.
31-
old_crate: CrateNum,
3230
}
3331

3432
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> Mismatch<'a, 'gcx, 'tcx> {
3533
/// Construct a new mismtach type relation.
36-
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, old_crate: CrateNum, id_mapping: &'a mut IdMapping)
34+
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, id_mapping: &'a mut IdMapping)
3735
-> Mismatch<'a, 'gcx, 'tcx>
3836
{
3937
Mismatch {
4038
tcx: tcx,
4139
item_queue: id_mapping.toplevel_queue(),
4240
id_mapping: id_mapping,
43-
old_crate: old_crate,
4441
}
4542
}
4643

@@ -191,8 +188,7 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for Mismatch<'a, 'gcx, 'tcx> {
191188
};
192189

193190
if let Some((old_def_id, new_def_id)) = matching {
194-
// TODO: implement proper crate tracking and fix this then.
195-
if !self.id_mapping.contains_id(old_def_id) && old_def_id.krate == self.old_crate {
191+
if !self.id_mapping.contains_id(old_def_id) && self.id_mapping.in_old_crate(old_def_id) {
196192
self.id_mapping.add_internal_item(old_def_id, new_def_id);
197193
self.item_queue.push_back((old_def_id, new_def_id));
198194
}

src/semcheck/traverse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ pub fn run_analysis<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, old: DefId, new: DefI
3131
-> ChangeSet<'tcx>
3232
{
3333
let mut changes = Default::default();
34-
let mut id_mapping = Default::default();
34+
let mut id_mapping = IdMapping::new(old.krate, new.krate);
3535

3636
// first pass
3737
diff_structure(&mut changes, &mut id_mapping, tcx, old, new);
3838

3939
// second pass
4040
{
41-
let mut mismatch = Mismatch::new(tcx, old.krate, &mut id_mapping);
41+
let mut mismatch = Mismatch::new(tcx, &mut id_mapping);
4242
mismatch.process();
4343
}
4444

0 commit comments

Comments
 (0)