@@ -33,6 +33,7 @@ use rustc::ty::TyCtxt;
33
33
use rustc:: util:: common:: ErrorReported ;
34
34
use rustc_codegen_utils:: codegen_backend:: CodegenBackend ;
35
35
use rustc_data_structures:: profiling:: print_time_passes_entry;
36
+ use rustc_data_structures:: sync:: future:: Future ;
36
37
use rustc_data_structures:: sync:: SeqCst ;
37
38
use rustc_errors:: { registry:: Registry , PResult } ;
38
39
use rustc_feature:: { find_gated_cfg, UnstableFeatures } ;
@@ -362,7 +363,7 @@ pub fn run_compiler(
362
363
return early_exit ( ) ;
363
364
}
364
365
365
- if sess. opts . debugging_opts . save_analysis {
366
+ let drop_ast_future = if sess. opts . debugging_opts . save_analysis {
366
367
let expanded_crate = & queries. expansion ( ) ?. peek ( ) . 0 ;
367
368
let crate_name = queries. crate_name ( ) ?. peek ( ) . clone ( ) ;
368
369
queries. global_ctxt ( ) ?. peek_mut ( ) . enter ( |tcx| {
@@ -386,14 +387,22 @@ pub fn run_compiler(
386
387
// AST will be dropped *after* the `after_analysis` callback
387
388
// (needed by the RLS)
388
389
} ) ?;
390
+ None
389
391
} else {
390
392
// 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
+ } ;
394
400
395
401
queries. global_ctxt ( ) ?. peek_mut ( ) . enter ( |tcx| tcx. analysis ( LOCAL_CRATE ) ) ?;
396
402
403
+ // Ensure the AST is dropped by this point.
404
+ drop_ast_future. map ( |future| future. join ( ) ) ;
405
+
397
406
if callbacks. after_analysis ( compiler, queries) == Compilation :: Stop {
398
407
return early_exit ( ) ;
399
408
}
0 commit comments