@@ -7,7 +7,7 @@ use rustc::hir::def::{Def, Export};
7
7
use rustc:: hir:: def_id:: { CrateNum , DefId } ;
8
8
use rustc:: ty:: TypeParameterDef ;
9
9
10
- use std:: collections:: { BTreeSet , HashMap , VecDeque } ;
10
+ use std:: collections:: { BTreeSet , HashMap , HashSet , VecDeque } ;
11
11
12
12
use syntax:: ast:: Name ;
13
13
@@ -23,6 +23,8 @@ pub struct IdMapping {
23
23
new_crate : CrateNum ,
24
24
/// Toplevel items' old `DefId` mapped to old and new `Def`.
25
25
toplevel_mapping : HashMap < DefId , ( Def , Def ) > ,
26
+ /// The set of toplevel items that have been removed.
27
+ removed_items : HashSet < DefId > ,
26
28
/// Trait items' old `DefId` mapped to old and new `Def`.
27
29
trait_item_mapping : HashMap < DefId , ( Def , Def , DefId ) > ,
28
30
/// Other items' old `DefId` mapped to new `DefId`.
@@ -39,6 +41,7 @@ impl IdMapping {
39
41
old_crate : old_crate,
40
42
new_crate : new_crate,
41
43
toplevel_mapping : HashMap :: new ( ) ,
44
+ removed_items : HashSet :: new ( ) ,
42
45
trait_item_mapping : HashMap :: new ( ) ,
43
46
internal_mapping : HashMap :: new ( ) ,
44
47
child_mapping : HashMap :: new ( ) ,
@@ -48,19 +51,30 @@ impl IdMapping {
48
51
49
52
/// Register two exports representing the same item across versions.
50
53
pub fn add_export ( & mut self , old : Def , new : Def ) -> bool {
51
- if self . toplevel_mapping . contains_key ( & old. def_id ( ) ) {
54
+ let old_def_id = old. def_id ( ) ;
55
+
56
+ if !self . in_old_crate ( old_def_id) || self . toplevel_mapping . contains_key ( & old_def_id) {
52
57
return false ;
53
58
}
54
59
55
60
self . toplevel_mapping
56
- . insert ( old . def_id ( ) , ( old, new) ) ;
61
+ . insert ( old_def_id , ( old, new) ) ;
57
62
58
63
true
59
64
}
60
65
66
+ /// Register that an old item has no corresponding new item.
67
+ pub fn add_removal ( & mut self , old : DefId ) {
68
+ self . removed_items . insert ( old) ;
69
+ }
70
+
61
71
/// Add any trait item's old and new `DefId`s.
62
72
pub fn add_trait_item ( & mut self , old : Def , new : Def , trait_def_id : DefId ) {
63
- self . trait_item_mapping . insert ( old. def_id ( ) , ( old, new, trait_def_id) ) ;
73
+ let old_def_id = old. def_id ( ) ;
74
+
75
+ assert ! ( self . in_old_crate( old_def_id) ) ;
76
+
77
+ self . trait_item_mapping . insert ( old_def_id, ( old, new, trait_def_id) ) ;
64
78
}
65
79
66
80
/// Add any other item's old and new `DefId`s.
@@ -70,12 +84,14 @@ impl IdMapping {
70
84
old,
71
85
self . internal_mapping[ & old] ,
72
86
new) ;
87
+ assert ! ( self . in_old_crate( old) ) ;
73
88
74
89
self . internal_mapping . insert ( old, new) ;
75
90
}
76
91
77
92
/// Add any other item's old and new `DefId`s, together with a parent entry.
78
93
pub fn add_subitem ( & mut self , parent : DefId , old : DefId , new : DefId ) {
94
+ // NB: we rely on the assers in `add_internal_item` here.
79
95
self . add_internal_item ( old, new) ;
80
96
self . child_mapping
81
97
. entry ( parent)
@@ -85,29 +101,49 @@ impl IdMapping {
85
101
86
102
/// Record that a `DefId` represents a new type parameter.
87
103
pub fn add_type_param ( & mut self , new : TypeParameterDef ) {
104
+ assert ! ( self . in_new_crate( new. def_id) ) ;
88
105
self . type_params . insert ( new. def_id , new) ;
89
106
}
90
107
91
108
/// Get the type parameter represented by a given `DefId`.
92
- pub fn get_type_param ( & self , def_id : & DefId ) -> TypeParameterDef {
93
- self . type_params [ def_id ]
109
+ pub fn get_type_param ( & self , did : & DefId ) -> TypeParameterDef {
110
+ self . type_params [ did ]
94
111
}
95
112
96
113
/// Check whether a `DefId` represents a newly added defaulted type parameter.
97
114
pub fn is_defaulted_type_param ( & self , new : & DefId ) -> bool {
115
+ // TODO?
98
116
self . type_params
99
117
. get ( new)
100
118
. map_or ( false , |def| def. has_default )
101
119
}
102
120
103
121
/// Get the new `DefId` associated with the given old one.
104
122
pub fn get_new_id ( & self , old : DefId ) -> DefId {
105
- if let Some ( new) = self . toplevel_mapping . get ( & old) {
106
- new. 1 . def_id ( )
107
- } else if let Some ( new) = self . trait_item_mapping . get ( & old) {
108
- new. 1 . def_id ( )
123
+ assert ! ( !self . in_new_crate( old) ) ;
124
+
125
+ if self . in_old_crate ( old) && !self . removed_items . contains ( & old) {
126
+ if let Some ( new) = self . toplevel_mapping . get ( & old) {
127
+ new. 1 . def_id ( )
128
+ } else if let Some ( new) = self . trait_item_mapping . get ( & old) {
129
+ new. 1 . def_id ( )
130
+ } else {
131
+ self . internal_mapping [ & old]
132
+ }
133
+ } else {
134
+ old
135
+ }
136
+ }
137
+
138
+ /// Get the new `DefId` associated with the given old one, respecting possibly removed
139
+ /// traits that are a parent of the given `DefId`.
140
+ pub fn get_new_trait_item_id ( & self , old : DefId , trait_id : DefId ) -> DefId {
141
+ assert ! ( !self . in_new_crate( trait_id) ) ;
142
+
143
+ if !self . removed_items . contains ( & trait_id) {
144
+ self . get_new_id ( old)
109
145
} else {
110
- self . internal_mapping [ & old]
146
+ old
111
147
}
112
148
}
113
149
@@ -148,10 +184,12 @@ impl IdMapping {
148
184
. map ( |m| m. iter ( ) . map ( move |old| ( * old, self . internal_mapping [ old] ) ) )
149
185
}
150
186
187
+ /// Check whether a `DefId` belongs to an item in the old crate.
151
188
pub fn in_old_crate ( & self , did : DefId ) -> bool {
152
189
self . old_crate == did. krate
153
190
}
154
191
192
+ /// Check whether a `DefId` belongs to an item in the new crate.
155
193
pub fn in_new_crate ( & self , did : DefId ) -> bool {
156
194
self . new_crate == did. krate
157
195
}
0 commit comments