@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashMap;
2
2
use rustc_data_structures:: sync:: join;
3
3
use rustc_middle:: dep_graph:: { DepGraph , DepKind , WorkProduct , WorkProductId } ;
4
4
use rustc_middle:: ty:: TyCtxt ;
5
- use rustc_serialize:: opaque:: Encoder ;
5
+ use rustc_serialize:: opaque:: { FileEncodeResult , FileEncoder } ;
6
6
use rustc_serialize:: Encodable as RustcEncodable ;
7
7
use rustc_session:: Session ;
8
8
use std:: fs;
@@ -33,12 +33,12 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
33
33
join (
34
34
move || {
35
35
sess. time ( "incr_comp_persist_result_cache" , || {
36
- save_in ( sess, query_cache_path, |e| encode_query_cache ( tcx, e) ) ;
36
+ save_in ( sess, query_cache_path, "query cache" , |e| encode_query_cache ( tcx, e) ) ;
37
37
} ) ;
38
38
} ,
39
39
|| {
40
40
sess. time ( "incr_comp_persist_dep_graph" , || {
41
- save_in ( sess, dep_graph_path, |e| {
41
+ save_in ( sess, dep_graph_path, "dependency graph" , |e| {
42
42
sess. time ( "incr_comp_encode_dep_graph" , || encode_dep_graph ( tcx, e) )
43
43
} ) ;
44
44
} ) ;
@@ -65,7 +65,7 @@ pub fn save_work_product_index(
65
65
debug ! ( "save_work_product_index()" ) ;
66
66
dep_graph. assert_ignored ( ) ;
67
67
let path = work_products_path ( sess) ;
68
- save_in ( sess, path, |e| encode_work_product_index ( & new_work_products, e) ) ;
68
+ save_in ( sess, path, "work product index" , |e| encode_work_product_index ( & new_work_products, e) ) ;
69
69
70
70
// We also need to clean out old work-products, as not all of them are
71
71
// deleted during invalidation. Some object files don't change their
@@ -92,13 +92,13 @@ pub fn save_work_product_index(
92
92
} ) ;
93
93
}
94
94
95
- fn save_in < F > ( sess : & Session , path_buf : PathBuf , encode : F )
95
+ fn save_in < F > ( sess : & Session , path_buf : PathBuf , name : & str , encode : F )
96
96
where
97
- F : FnOnce ( & mut Encoder ) ,
97
+ F : FnOnce ( & mut FileEncoder ) -> FileEncodeResult ,
98
98
{
99
99
debug ! ( "save: storing data in {}" , path_buf. display( ) ) ;
100
100
101
- // delete the old dep-graph , if any
101
+ // Delete the old file , if any.
102
102
// Note: It's important that we actually delete the old file and not just
103
103
// truncate and overwrite it, since it might be a shared hard-link, the
104
104
// underlying data of which we don't want to modify
@@ -109,34 +109,44 @@ where
109
109
Err ( err) if err. kind ( ) == io:: ErrorKind :: NotFound => ( ) ,
110
110
Err ( err) => {
111
111
sess. err ( & format ! (
112
- "unable to delete old dep-graph at `{}`: {}" ,
112
+ "unable to delete old {} at `{}`: {}" ,
113
+ name,
113
114
path_buf. display( ) ,
114
115
err
115
116
) ) ;
116
117
return ;
117
118
}
118
119
}
119
120
120
- // generate the data in a memory buffer
121
- let mut encoder = Encoder :: new ( Vec :: new ( ) ) ;
122
- file_format:: write_file_header ( & mut encoder, sess. is_nightly_build ( ) ) ;
123
- encode ( & mut encoder) ;
124
-
125
- // write the data out
126
- let data = encoder. into_inner ( ) ;
127
- match fs:: write ( & path_buf, data) {
128
- Ok ( _) => {
129
- debug ! ( "save: data written to disk successfully" ) ;
130
- }
121
+ let mut encoder = match FileEncoder :: new ( & path_buf) {
122
+ Ok ( encoder) => encoder,
131
123
Err ( err) => {
132
- sess. err ( & format ! ( "failed to write dep-graph to `{}`: {}" , path_buf. display( ) , err) ) ;
124
+ sess. err ( & format ! ( "failed to create {} at `{}`: {}" , name, path_buf. display( ) , err) ) ;
125
+ return ;
133
126
}
127
+ } ;
128
+
129
+ if let Err ( err) = file_format:: write_file_header ( & mut encoder, sess. is_nightly_build ( ) ) {
130
+ sess. err ( & format ! ( "failed to write {} header to `{}`: {}" , name, path_buf. display( ) , err) ) ;
131
+ return ;
132
+ }
133
+
134
+ if let Err ( err) = encode ( & mut encoder) {
135
+ sess. err ( & format ! ( "failed to write {} to `{}`: {}" , name, path_buf. display( ) , err) ) ;
136
+ return ;
134
137
}
138
+
139
+ if let Err ( err) = encoder. flush ( ) {
140
+ sess. err ( & format ! ( "failed to flush {} to `{}`: {}" , name, path_buf. display( ) , err) ) ;
141
+ return ;
142
+ }
143
+
144
+ debug ! ( "save: data written to disk successfully" ) ;
135
145
}
136
146
137
- fn encode_dep_graph ( tcx : TyCtxt < ' _ > , encoder : & mut Encoder ) {
147
+ fn encode_dep_graph ( tcx : TyCtxt < ' _ > , encoder : & mut FileEncoder ) -> FileEncodeResult {
138
148
// First encode the commandline arguments hash
139
- tcx. sess . opts . dep_tracking_hash ( ) . encode ( encoder) . unwrap ( ) ;
149
+ tcx. sess . opts . dep_tracking_hash ( ) . encode ( encoder) ? ;
140
150
141
151
// Encode the graph data.
142
152
let serialized_graph =
@@ -214,15 +224,13 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
214
224
println ! ( "[incremental]" ) ;
215
225
}
216
226
217
- tcx. sess . time ( "incr_comp_encode_serialized_dep_graph" , || {
218
- serialized_graph. encode ( encoder) . unwrap ( ) ;
219
- } ) ;
227
+ tcx. sess . time ( "incr_comp_encode_serialized_dep_graph" , || serialized_graph. encode ( encoder) )
220
228
}
221
229
222
230
fn encode_work_product_index (
223
231
work_products : & FxHashMap < WorkProductId , WorkProduct > ,
224
- encoder : & mut Encoder ,
225
- ) {
232
+ encoder : & mut FileEncoder ,
233
+ ) -> FileEncodeResult {
226
234
let serialized_products: Vec < _ > = work_products
227
235
. iter ( )
228
236
. map ( |( id, work_product) | SerializedWorkProduct {
@@ -231,11 +239,9 @@ fn encode_work_product_index(
231
239
} )
232
240
. collect ( ) ;
233
241
234
- serialized_products. encode ( encoder) . unwrap ( ) ;
242
+ serialized_products. encode ( encoder)
235
243
}
236
244
237
- fn encode_query_cache ( tcx : TyCtxt < ' _ > , encoder : & mut Encoder ) {
238
- tcx. sess . time ( "incr_comp_serialize_result_cache" , || {
239
- tcx. serialize_query_result_cache ( encoder) . unwrap ( ) ;
240
- } )
245
+ fn encode_query_cache ( tcx : TyCtxt < ' _ > , encoder : & mut FileEncoder ) -> FileEncodeResult {
246
+ tcx. sess . time ( "incr_comp_serialize_result_cache" , || tcx. serialize_query_result_cache ( encoder) )
241
247
}
0 commit comments