@@ -36,6 +36,7 @@ use rustc::session::config::nightly_options;
3636use rustc:: session:: { early_error, early_warn} ;
3737use rustc:: lint:: Lint ;
3838use rustc:: lint;
39+ use rustc:: ty:: TyCtxt ;
3940use rustc:: hir:: def_id:: LOCAL_CRATE ;
4041use rustc:: util:: common:: { ErrorReported , install_panic_hook, print_time_passes_entry} ;
4142use rustc:: util:: common:: { set_time_depth, time} ;
@@ -106,11 +107,19 @@ pub trait Callbacks {
106107 /// Called before creating the compiler instance
107108 fn config ( & mut self , _config : & mut interface:: Config ) { }
108109 /// Called after parsing and returns true to continue execution
109- fn after_parsing ( & mut self , _compiler : & interface:: Compiler ) -> bool {
110+ fn after_parsing (
111+ & mut self ,
112+ _compiler : & interface:: Compiler ,
113+ _tcx : TyCtxt < ' _ > ,
114+ ) -> bool {
110115 true
111116 }
112117 /// Called after analysis and returns true to continue execution
113- fn after_analysis ( & mut self , _compiler : & interface:: Compiler ) -> bool {
118+ fn after_analysis (
119+ & mut self ,
120+ _compiler : & interface:: Compiler ,
121+ _tcx : TyCtxt < ' _ > ,
122+ ) -> bool {
114123 true
115124 }
116125}
@@ -244,7 +253,8 @@ pub fn run_compiler(
244253 let pretty_info = parse_pretty ( & mut config. opts , & matches) ;
245254
246255 interface:: run_compiler ( config, |compiler| {
247- let sess = compiler. session ( ) ;
256+ let sess = compiler. session ( ) . clone ( ) ;
257+ let sess = & * sess;
248258 let should_stop = RustcDefaultCalls :: print_crate_info (
249259 & * * compiler. codegen_backend ( ) ,
250260 sess,
@@ -262,9 +272,9 @@ pub fn run_compiler(
262272 return sess. compile_status ( ) ;
263273 }
264274
265- if let Some ( ( ppm , opt_uii ) ) = pretty_info {
266- if ppm . needs_ast_map ( & opt_uii) {
267- compiler . enter ( |tcx| {
275+ let link = compiler . enter ( |compiler , tcx| {
276+ if let Some ( ( ppm , opt_uii) ) = pretty_info {
277+ if ppm . needs_ast_map ( & opt_uii ) {
268278 let expansion_result = tcx. expand_macros ( ( ) ) ?;
269279 pretty:: print_after_hir_lowering (
270280 tcx,
@@ -274,11 +284,8 @@ pub fn run_compiler(
274284 opt_uii. clone ( ) ,
275285 compiler. output_file ( ) . as_ref ( ) . map ( |p| & * * p) ,
276286 ) ;
277- Ok ( ( ) )
278- } ) ?;
279- return sess. compile_status ( ) ;
280- } else {
281- compiler. enter ( |tcx| {
287+ return sess. compile_status ( ) . map ( |_| None ) ;
288+ } else {
282289 let krate = tcx. parse ( ( ) ) ?;
283290 let krate = krate. borrow ( ) ;
284291 pretty:: print_after_parsing (
@@ -288,57 +295,48 @@ pub fn run_compiler(
288295 ppm,
289296 compiler. output_file ( ) . as_ref ( ) . map ( |p| & * * p) ,
290297 ) ;
291- Ok ( ( ) )
292- } ) ?;
293- return sess. compile_status ( ) ;
298+ return sess. compile_status ( ) . map ( |_| None ) ;
299+ }
294300 }
295- }
296301
297- compiler . enter ( |tcx| tcx. parse ( ( ) ) ) ?;
302+ tcx. parse ( ( ) ) ?;
298303
299- if !callbacks. after_parsing ( compiler) {
300- return sess. compile_status ( ) ;
301- }
304+ if !callbacks. after_parsing ( compiler, tcx ) {
305+ return sess. compile_status ( ) . map ( |_| None ) ;
306+ }
302307
303- if sess. opts . debugging_opts . parse_only ||
304- sess. opts . debugging_opts . show_span . is_some ( ) ||
305- sess. opts . debugging_opts . ast_json_noexpand {
306- return sess. compile_status ( ) ;
307- }
308+ if sess. opts . debugging_opts . parse_only ||
309+ sess. opts . debugging_opts . show_span . is_some ( ) ||
310+ sess. opts . debugging_opts . ast_json_noexpand {
311+ return sess. compile_status ( ) . map ( |_| None ) ;
312+ }
308313
309- compiler . enter ( |tcx| tcx. register_plugins ( ( ) ) ) ?;
314+ tcx. register_plugins ( ( ) ) ?;
310315
311- // Lint plugins are registered; now we can process command line flags.
312- if sess. opts . describe_lints {
313- describe_lints ( & sess, & sess. lint_store . borrow ( ) , true ) ;
314- return sess. compile_status ( ) ;
315- }
316+ // Lint plugins are registered; now we can process command line flags.
317+ if sess. opts . describe_lints {
318+ describe_lints ( & sess, & sess. lint_store . borrow ( ) , true ) ;
319+ return sess. compile_status ( ) . map ( |_| None ) ;
320+ }
316321
317- compiler. enter ( |tcx| {
318322 tcx. prepare_outputs ( ( ) ) ?;
319- Ok ( ( ) )
320- } ) ?;
321323
322- if sess. opts . output_types . contains_key ( & OutputType :: DepInfo )
323- && sess. opts . output_types . len ( ) == 1
324- {
325- return sess. compile_status ( ) ;
326- }
324+ if sess. opts . output_types . contains_key ( & OutputType :: DepInfo )
325+ && sess. opts . output_types . len ( ) == 1
326+ {
327+ return sess. compile_status ( ) . map ( |_| None ) ;
328+ }
327329
328- compiler. enter ( |tcx| {
329330 tcx. lower_ast_to_hir ( ( ) ) ?;
330- Ok ( ( ) )
331- } ) ?;
332331
333- if sess. opts . debugging_opts . no_analysis ||
334- sess. opts . debugging_opts . ast_json {
335- return sess. compile_status ( ) ;
336- }
332+ if sess. opts . debugging_opts . no_analysis ||
333+ sess. opts . debugging_opts . ast_json {
334+ return sess. compile_status ( ) . map ( |_| None ) ;
335+ }
337336
338- if sess. opts . debugging_opts . save_analysis {
339- compiler. enter ( |tcx| {
337+ if sess. opts . debugging_opts . save_analysis {
340338 let expansion_result = tcx. expand_macros ( ( ) ) ?;
341- let result = tcx. analysis ( LOCAL_CRATE ) ;
339+ tcx. analysis ( LOCAL_CRATE ) . ok ( ) ;
342340 let crate_name = & tcx. crate_name ( LOCAL_CRATE ) . as_str ( ) ;
343341
344342 time ( sess, "save analysis" , || {
@@ -351,41 +349,36 @@ pub fn run_compiler(
351349 DumpHandler :: new ( compiler. output_dir ( ) . as_ref ( ) . map ( |p| & * * p) , crate_name)
352350 )
353351 } ) ;
354-
355- result
356352 // AST will be dropped *after* the `after_analysis` callback
357353 // (needed by the RLS)
358- } ) ?;
359- } else {
360- compiler. enter ( |tcx| {
354+ } else {
361355 // Drop AST after lowering HIR to free memory
362356 mem:: drop ( tcx. expand_macros ( ( ) ) . unwrap ( ) . ast_crate . steal ( ) ) ;
363- } ) ;
364- }
357+ }
365358
366- compiler . enter ( |tcx| tcx. analysis ( LOCAL_CRATE ) ) ?;
359+ tcx. analysis ( LOCAL_CRATE ) ?;
367360
368- if !callbacks. after_analysis ( compiler) {
369- return sess. compile_status ( ) ;
370- }
361+ if !callbacks. after_analysis ( compiler, tcx ) {
362+ return sess. compile_status ( ) . map ( |_| None ) ;
363+ }
371364
372- if sess. opts . debugging_opts . save_analysis {
373- compiler. enter ( |tcx| {
374- // Drop AST after lowering HIR to free memory
365+ if sess. opts . debugging_opts . save_analysis {
366+ // Drop AST after running `after_analysis` callback to free memory
375367 mem:: drop ( tcx. expand_macros ( ( ) ) . unwrap ( ) . ast_crate . steal ( ) ) ;
376- } ) ;
377- }
368+ }
378369
379- compiler. ongoing_codegen ( ) ?;
370+ compiler. linker ( tcx) . map ( |linker| Some ( linker) )
371+ } ) ?;
380372
381- // Drop GlobalCtxt after starting codegen to free memory
382- mem:: drop ( compiler. global_ctxt ( ) ?. take ( ) ) ;
383373
384374 if sess. opts . debugging_opts . print_type_sizes {
385375 sess. code_stats . borrow ( ) . print_type_sizes ( ) ;
386376 }
387377
388- compiler. link ( ) ?;
378+ // Run linker outside `enter` so GlobalCtxt is freed
379+ if let Some ( linker) = link {
380+ linker. link ( ) ?;
381+ }
389382
390383 if sess. opts . debugging_opts . perf_stats {
391384 sess. print_perf_stats ( ) ;
0 commit comments