@@ -36,6 +36,7 @@ use rustc::session::config::nightly_options;
36
36
use rustc:: session:: { early_error, early_warn} ;
37
37
use rustc:: lint:: Lint ;
38
38
use rustc:: lint;
39
+ use rustc:: ty:: TyCtxt ;
39
40
use rustc:: hir:: def_id:: LOCAL_CRATE ;
40
41
use rustc:: util:: common:: { ErrorReported , install_panic_hook, print_time_passes_entry} ;
41
42
use rustc:: util:: common:: { set_time_depth, time} ;
@@ -106,11 +107,19 @@ pub trait Callbacks {
106
107
/// Called before creating the compiler instance
107
108
fn config ( & mut self , _config : & mut interface:: Config ) { }
108
109
/// 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 {
110
115
true
111
116
}
112
117
/// 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 {
114
123
true
115
124
}
116
125
}
@@ -244,7 +253,8 @@ pub fn run_compiler(
244
253
let pretty_info = parse_pretty ( & mut config. opts , & matches) ;
245
254
246
255
interface:: run_compiler ( config, |compiler| {
247
- let sess = compiler. session ( ) ;
256
+ let sess = compiler. session ( ) . clone ( ) ;
257
+ let sess = & * sess;
248
258
let should_stop = RustcDefaultCalls :: print_crate_info (
249
259
& * * compiler. codegen_backend ( ) ,
250
260
sess,
@@ -262,9 +272,9 @@ pub fn run_compiler(
262
272
return sess. compile_status ( ) ;
263
273
}
264
274
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 ) {
268
278
let expansion_result = tcx. expand_macros ( ( ) ) ?;
269
279
pretty:: print_after_hir_lowering (
270
280
tcx,
@@ -274,11 +284,8 @@ pub fn run_compiler(
274
284
opt_uii. clone ( ) ,
275
285
compiler. output_file ( ) . as_ref ( ) . map ( |p| & * * p) ,
276
286
) ;
277
- Ok ( ( ) )
278
- } ) ?;
279
- return sess. compile_status ( ) ;
280
- } else {
281
- compiler. enter ( |tcx| {
287
+ return sess. compile_status ( ) . map ( |_| None ) ;
288
+ } else {
282
289
let krate = tcx. parse ( ( ) ) ?;
283
290
let krate = krate. borrow ( ) ;
284
291
pretty:: print_after_parsing (
@@ -288,57 +295,48 @@ pub fn run_compiler(
288
295
ppm,
289
296
compiler. output_file ( ) . as_ref ( ) . map ( |p| & * * p) ,
290
297
) ;
291
- Ok ( ( ) )
292
- } ) ?;
293
- return sess. compile_status ( ) ;
298
+ return sess. compile_status ( ) . map ( |_| None ) ;
299
+ }
294
300
}
295
- }
296
301
297
- compiler . enter ( |tcx| tcx. parse ( ( ) ) ) ?;
302
+ tcx. parse ( ( ) ) ?;
298
303
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
+ }
302
307
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
+ }
308
313
309
- compiler . enter ( |tcx| tcx. register_plugins ( ( ) ) ) ?;
314
+ tcx. register_plugins ( ( ) ) ?;
310
315
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
+ }
316
321
317
- compiler. enter ( |tcx| {
318
322
tcx. prepare_outputs ( ( ) ) ?;
319
- Ok ( ( ) )
320
- } ) ?;
321
323
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
+ }
327
329
328
- compiler. enter ( |tcx| {
329
330
tcx. lower_ast_to_hir ( ( ) ) ?;
330
- Ok ( ( ) )
331
- } ) ?;
332
331
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
+ }
337
336
338
- if sess. opts . debugging_opts . save_analysis {
339
- compiler. enter ( |tcx| {
337
+ if sess. opts . debugging_opts . save_analysis {
340
338
let expansion_result = tcx. expand_macros ( ( ) ) ?;
341
- let result = tcx. analysis ( LOCAL_CRATE ) ;
339
+ tcx. analysis ( LOCAL_CRATE ) . ok ( ) ;
342
340
let crate_name = & tcx. crate_name ( LOCAL_CRATE ) . as_str ( ) ;
343
341
344
342
time ( sess, "save analysis" , || {
@@ -351,41 +349,36 @@ pub fn run_compiler(
351
349
DumpHandler :: new ( compiler. output_dir ( ) . as_ref ( ) . map ( |p| & * * p) , crate_name)
352
350
)
353
351
} ) ;
354
-
355
- result
356
352
// AST will be dropped *after* the `after_analysis` callback
357
353
// (needed by the RLS)
358
- } ) ?;
359
- } else {
360
- compiler. enter ( |tcx| {
354
+ } else {
361
355
// Drop AST after lowering HIR to free memory
362
356
mem:: drop ( tcx. expand_macros ( ( ) ) . unwrap ( ) . ast_crate . steal ( ) ) ;
363
- } ) ;
364
- }
357
+ }
365
358
366
- compiler . enter ( |tcx| tcx. analysis ( LOCAL_CRATE ) ) ?;
359
+ tcx. analysis ( LOCAL_CRATE ) ?;
367
360
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
+ }
371
364
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
375
367
mem:: drop ( tcx. expand_macros ( ( ) ) . unwrap ( ) . ast_crate . steal ( ) ) ;
376
- } ) ;
377
- }
368
+ }
378
369
379
- compiler. ongoing_codegen ( ) ?;
370
+ compiler. linker ( tcx) . map ( |linker| Some ( linker) )
371
+ } ) ?;
380
372
381
- // Drop GlobalCtxt after starting codegen to free memory
382
- mem:: drop ( compiler. global_ctxt ( ) ?. take ( ) ) ;
383
373
384
374
if sess. opts . debugging_opts . print_type_sizes {
385
375
sess. code_stats . borrow ( ) . print_type_sizes ( ) ;
386
376
}
387
377
388
- compiler. link ( ) ?;
378
+ // Run linker outside `enter` so GlobalCtxt is freed
379
+ if let Some ( linker) = link {
380
+ linker. link ( ) ?;
381
+ }
389
382
390
383
if sess. opts . debugging_opts . perf_stats {
391
384
sess. print_perf_stats ( ) ;
0 commit comments