@@ -30,6 +30,7 @@ use super::preds::*;
3030use super :: fs:: * ;
3131use super :: dirty_clean;
3232use super :: file_format;
33+ use super :: work_product;
3334use calculate_svh:: IchHasher ;
3435
3536pub fn save_dep_graph < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
@@ -84,6 +85,31 @@ pub fn save_work_products(sess: &Session) {
8485 let _ignore = sess. dep_graph . in_ignore ( ) ;
8586 let path = work_products_path ( sess) ;
8687 save_in ( sess, path, |e| encode_work_products ( sess, e) ) ;
88+
89+ // We also need to clean out old work-products, as not all of them are
90+ // deleted during invalidation. Some object files don't change their
91+ // content, they are just not needed anymore.
92+ let new_work_products = sess. dep_graph . work_products ( ) ;
93+ let previous_work_products = sess. dep_graph . previous_work_products ( ) ;
94+
95+ for ( id, wp) in previous_work_products. iter ( ) {
96+ if !new_work_products. contains_key ( id) {
97+ work_product:: delete_workproduct_files ( sess, wp) ;
98+ debug_assert ! ( wp. saved_files. iter( ) . all( |& ( _, ref file_name) | {
99+ !in_incr_comp_dir_sess( sess, file_name) . exists( )
100+ } ) ) ;
101+ }
102+ }
103+
104+ // Check that we did not delete one of the current work-products:
105+ debug_assert ! ( {
106+ new_work_products. iter( )
107+ . flat_map( |( _, wp) | wp. saved_files
108+ . iter( )
109+ . map( |& ( _, ref name) | name) )
110+ . map( |name| in_incr_comp_dir_sess( sess, name) )
111+ . all( |path| path. exists( ) )
112+ } ) ;
87113}
88114
89115fn save_in < F > ( sess : & Session , path_buf : PathBuf , encode : F )
0 commit comments