@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashMap;
22use rustc_data_structures:: sync:: join;
33use rustc_middle:: dep_graph:: { DepGraph , DepKind , WorkProduct , WorkProductId } ;
44use rustc_middle:: ty:: TyCtxt ;
5- use rustc_serialize:: opaque:: Encoder ;
5+ use rustc_serialize:: opaque:: { FileEncodeResult , FileEncoder } ;
66use rustc_serialize:: Encodable as RustcEncodable ;
77use rustc_session:: Session ;
88use std:: fs;
@@ -33,12 +33,12 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
3333 join (
3434 move || {
3535 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) ) ;
3737 } ) ;
3838 } ,
3939 || {
4040 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| {
4242 sess. time ( "incr_comp_encode_dep_graph" , || encode_dep_graph ( tcx, e) )
4343 } ) ;
4444 } ) ;
@@ -65,7 +65,7 @@ pub fn save_work_product_index(
6565 debug ! ( "save_work_product_index()" ) ;
6666 dep_graph. assert_ignored ( ) ;
6767 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) ) ;
6969
7070 // We also need to clean out old work-products, as not all of them are
7171 // deleted during invalidation. Some object files don't change their
@@ -92,13 +92,13 @@ pub fn save_work_product_index(
9292 } ) ;
9393}
9494
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 )
9696where
97- F : FnOnce ( & mut Encoder ) ,
97+ F : FnOnce ( & mut FileEncoder ) -> FileEncodeResult ,
9898{
9999 debug ! ( "save: storing data in {}" , path_buf. display( ) ) ;
100100
101- // delete the old dep-graph , if any
101+ // Delete the old file , if any.
102102 // Note: It's important that we actually delete the old file and not just
103103 // truncate and overwrite it, since it might be a shared hard-link, the
104104 // underlying data of which we don't want to modify
@@ -109,34 +109,44 @@ where
109109 Err ( err) if err. kind ( ) == io:: ErrorKind :: NotFound => ( ) ,
110110 Err ( err) => {
111111 sess. err ( & format ! (
112- "unable to delete old dep-graph at `{}`: {}" ,
112+ "unable to delete old {} at `{}`: {}" ,
113+ name,
113114 path_buf. display( ) ,
114115 err
115116 ) ) ;
116117 return ;
117118 }
118119 }
119120
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,
131123 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 ;
133126 }
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 ;
134137 }
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" ) ;
135145}
136146
137- fn encode_dep_graph ( tcx : TyCtxt < ' _ > , encoder : & mut Encoder ) {
147+ fn encode_dep_graph ( tcx : TyCtxt < ' _ > , encoder : & mut FileEncoder ) -> FileEncodeResult {
138148 // 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) ? ;
140150
141151 // Encode the graph data.
142152 let serialized_graph =
@@ -214,15 +224,13 @@ fn encode_dep_graph(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
214224 println ! ( "[incremental]" ) ;
215225 }
216226
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) )
220228}
221229
222230fn encode_work_product_index (
223231 work_products : & FxHashMap < WorkProductId , WorkProduct > ,
224- encoder : & mut Encoder ,
225- ) {
232+ encoder : & mut FileEncoder ,
233+ ) -> FileEncodeResult {
226234 let serialized_products: Vec < _ > = work_products
227235 . iter ( )
228236 . map ( |( id, work_product) | SerializedWorkProduct {
@@ -231,11 +239,9 @@ fn encode_work_product_index(
231239 } )
232240 . collect ( ) ;
233241
234- serialized_products. encode ( encoder) . unwrap ( ) ;
242+ serialized_products. encode ( encoder)
235243}
236244
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) )
241247}
0 commit comments