@@ -33,6 +33,7 @@ use rustc::ty::TyCtxt;
3333use rustc:: util:: common:: ErrorReported ;
3434use rustc_codegen_utils:: codegen_backend:: CodegenBackend ;
3535use rustc_data_structures:: profiling:: print_time_passes_entry;
36+ use rustc_data_structures:: sync:: future:: Future ;
3637use rustc_data_structures:: sync:: SeqCst ;
3738use rustc_errors:: { registry:: Registry , PResult } ;
3839use rustc_feature:: { find_gated_cfg, UnstableFeatures } ;
@@ -362,7 +363,7 @@ pub fn run_compiler(
362363 return early_exit ( ) ;
363364 }
364365
365- if sess. opts . debugging_opts . save_analysis {
366+ let drop_ast_future = if sess. opts . debugging_opts . save_analysis {
366367 let expanded_crate = & queries. expansion ( ) ?. peek ( ) . 0 ;
367368 let crate_name = queries. crate_name ( ) ?. peek ( ) . clone ( ) ;
368369 queries. global_ctxt ( ) ?. peek_mut ( ) . enter ( |tcx| {
@@ -386,14 +387,22 @@ pub fn run_compiler(
386387 // AST will be dropped *after* the `after_analysis` callback
387388 // (needed by the RLS)
388389 } ) ?;
390+ None
389391 } else {
390392 // Drop AST after creating GlobalCtxt to free memory
391- let _timer = sess. prof . generic_activity ( "drop_ast" ) ;
392- mem:: drop ( queries. expansion ( ) ?. take ( ) ) ;
393- }
393+ let prof = sess. prof . clone ( ) ;
394+ let ast = queries. expansion ( ) ?. take ( ) . 0 ;
395+ Some ( Future :: spawn ( move || {
396+ let _timer = prof. generic_activity ( "drop_ast" ) ;
397+ mem:: drop ( ast) ;
398+ } ) )
399+ } ;
394400
395401 queries. global_ctxt ( ) ?. peek_mut ( ) . enter ( |tcx| tcx. analysis ( LOCAL_CRATE ) ) ?;
396402
403+ // Ensure the AST is dropped by this point.
404+ drop_ast_future. map ( |future| future. join ( ) ) ;
405+
397406 if callbacks. after_analysis ( compiler, queries) == Compilation :: Stop {
398407 return early_exit ( ) ;
399408 }
0 commit comments