@@ -22,35 +22,42 @@ use std::mem;
22
22
use syntax:: codemap:: { CodeMap , StableFilemapId } ;
23
23
use syntax_pos:: { BytePos , Span , NO_EXPANSION , DUMMY_SP } ;
24
24
25
+ /// `OnDiskCache` provides an interface to incr. comp. data cached from the
26
+ /// previous compilation session. This data will eventually include the results
27
+ /// of a few selected queries (like `typeck_tables_of` and `mir_optimized`) and
28
+ /// any diagnostics that have been emitted during a query.
25
29
pub struct OnDiskCache < ' sess > {
30
+ // The diagnostics emitted during the previous compilation session.
26
31
prev_diagnostics : FxHashMap < SerializedDepNodeIndex , Vec < Diagnostic > > ,
27
32
33
+ // This field collects all Diagnostics emitted during the current
34
+ // compilation session.
35
+ current_diagnostics : RefCell < FxHashMap < DepNodeIndex , Vec < Diagnostic > > > ,
36
+
37
+ // This will eventually be needed for creating Decoders that can rebase
38
+ // spans.
28
39
_prev_filemap_starts : BTreeMap < BytePos , StableFilemapId > ,
29
40
codemap : & ' sess CodeMap ,
30
-
31
- current_diagnostics : RefCell < FxHashMap < DepNodeIndex , Vec < Diagnostic > > > ,
32
41
}
33
42
43
+ // This type is used only for (de-)serialization.
34
44
#[ derive( RustcEncodable , RustcDecodable ) ]
35
45
struct Header {
36
46
prev_filemap_starts : BTreeMap < BytePos , StableFilemapId > ,
37
47
}
38
48
49
+ // This type is used only for (de-)serialization.
39
50
#[ derive( RustcEncodable , RustcDecodable ) ]
40
51
struct Body {
41
52
diagnostics : Vec < ( SerializedDepNodeIndex , Vec < Diagnostic > ) > ,
42
53
}
43
54
44
55
impl < ' sess > OnDiskCache < ' sess > {
45
- pub fn new_empty ( codemap : & ' sess CodeMap ) -> OnDiskCache < ' sess > {
46
- OnDiskCache {
47
- prev_diagnostics : FxHashMap ( ) ,
48
- _prev_filemap_starts : BTreeMap :: new ( ) ,
49
- codemap,
50
- current_diagnostics : RefCell :: new ( FxHashMap ( ) ) ,
51
- }
52
- }
53
-
56
+ /// Create a new OnDiskCache instance from the serialized data in `data`.
57
+ /// Note that the current implementation (which only deals with diagnostics
58
+ /// so far) will eagerly deserialize the complete cache. Once we are
59
+ /// dealing with larger amounts of data (i.e. cached query results),
60
+ /// deserialization will need to happen lazily.
54
61
pub fn new ( sess : & ' sess Session , data : & [ u8 ] ) -> OnDiskCache < ' sess > {
55
62
debug_assert ! ( sess. opts. incremental. is_some( ) ) ;
56
63
@@ -75,6 +82,15 @@ impl<'sess> OnDiskCache<'sess> {
75
82
}
76
83
}
77
84
85
+ pub fn new_empty ( codemap : & ' sess CodeMap ) -> OnDiskCache < ' sess > {
86
+ OnDiskCache {
87
+ prev_diagnostics : FxHashMap ( ) ,
88
+ _prev_filemap_starts : BTreeMap :: new ( ) ,
89
+ codemap,
90
+ current_diagnostics : RefCell :: new ( FxHashMap ( ) ) ,
91
+ }
92
+ }
93
+
78
94
pub fn serialize < ' a , ' tcx , E > ( & self ,
79
95
encoder : & mut E )
80
96
-> Result < ( ) , E :: Error >
@@ -101,12 +117,16 @@ impl<'sess> OnDiskCache<'sess> {
101
117
Ok ( ( ) )
102
118
}
103
119
120
+ /// Load a diagnostic emitted during the previous compilation session.
104
121
pub fn load_diagnostics ( & self ,
105
122
dep_node_index : SerializedDepNodeIndex )
106
123
-> Vec < Diagnostic > {
107
124
self . prev_diagnostics . get ( & dep_node_index) . cloned ( ) . unwrap_or ( vec ! [ ] )
108
125
}
109
126
127
+ /// Store a diagnostic emitted during the current compilation session.
128
+ /// Anything stored like this will be available via `load_diagnostics` in
129
+ /// the next compilation session.
110
130
pub fn store_diagnostics ( & self ,
111
131
dep_node_index : DepNodeIndex ,
112
132
diagnostics : Vec < Diagnostic > ) {
@@ -115,6 +135,10 @@ impl<'sess> OnDiskCache<'sess> {
115
135
debug_assert ! ( prev. is_none( ) ) ;
116
136
}
117
137
138
+ /// Store a diagnostic emitted during computation of an anonymous query.
139
+ /// Since many anonymous queries can share the same `DepNode`, we aggregate
140
+ /// them -- as opposed to regular queries where we assume that there is a
141
+ /// 1:1 relationship between query-key and `DepNode`.
118
142
pub fn store_diagnostics_for_anon_node ( & self ,
119
143
dep_node_index : DepNodeIndex ,
120
144
mut diagnostics : Vec < Diagnostic > ) {
@@ -128,23 +152,9 @@ impl<'sess> OnDiskCache<'sess> {
128
152
}
129
153
}
130
154
131
- impl < ' a > SpecializedDecoder < Span > for CacheDecoder < ' a > {
132
- fn specialized_decode ( & mut self ) -> Result < Span , Self :: Error > {
133
- let lo = BytePos :: decode ( self ) ?;
134
- let hi = BytePos :: decode ( self ) ?;
135
-
136
- if let Some ( ( prev_filemap_start, filemap_id) ) = self . find_filemap_prev_bytepos ( lo) {
137
- if let Some ( current_filemap) = self . codemap . filemap_by_stable_id ( filemap_id) {
138
- let lo = ( lo + current_filemap. start_pos ) - prev_filemap_start;
139
- let hi = ( hi + current_filemap. start_pos ) - prev_filemap_start;
140
- return Ok ( Span :: new ( lo, hi, NO_EXPANSION ) ) ;
141
- }
142
- }
143
-
144
- Ok ( DUMMY_SP )
145
- }
146
- }
147
-
155
+ /// A decoder that can read the incr. comp. cache. It is similar to the one
156
+ /// we use for crate metadata decoding in that it can rebase spans and
157
+ /// eventually will also handle things that contain `Ty` instances.
148
158
struct CacheDecoder < ' a > {
149
159
opaque : opaque:: Decoder < ' a > ,
150
160
codemap : & ' a CodeMap ,
@@ -202,3 +212,20 @@ impl<'sess> Decoder for CacheDecoder<'sess> {
202
212
self . opaque . error ( err)
203
213
}
204
214
}
215
+
216
+ impl < ' a > SpecializedDecoder < Span > for CacheDecoder < ' a > {
217
+ fn specialized_decode ( & mut self ) -> Result < Span , Self :: Error > {
218
+ let lo = BytePos :: decode ( self ) ?;
219
+ let hi = BytePos :: decode ( self ) ?;
220
+
221
+ if let Some ( ( prev_filemap_start, filemap_id) ) = self . find_filemap_prev_bytepos ( lo) {
222
+ if let Some ( current_filemap) = self . codemap . filemap_by_stable_id ( filemap_id) {
223
+ let lo = ( lo + current_filemap. start_pos ) - prev_filemap_start;
224
+ let hi = ( hi + current_filemap. start_pos ) - prev_filemap_start;
225
+ return Ok ( Span :: new ( lo, hi, NO_EXPANSION ) ) ;
226
+ }
227
+ }
228
+
229
+ Ok ( DUMMY_SP )
230
+ }
231
+ }
0 commit comments