@@ -7,7 +7,6 @@ use rustc_ast::{self as ast, visit};
7
7
use rustc_codegen_ssa:: back:: link:: emit_metadata;
8
8
use rustc_codegen_ssa:: traits:: CodegenBackend ;
9
9
use rustc_data_structures:: parallel;
10
- use rustc_data_structures:: steal:: Steal ;
11
10
use rustc_data_structures:: sync:: { par_iter, Lrc , OnceCell , ParallelIterator , WorkerLocal } ;
12
11
use rustc_data_structures:: temp_dir:: MaybeTempDir ;
13
12
use rustc_errors:: { ErrorReported , PResult } ;
@@ -101,7 +100,7 @@ mod boxed_resolver {
101
100
}
102
101
103
102
// Note: Drop order is important to prevent dangling references. Resolver must be dropped first,
104
- // then resolver_arenas and finally session.
103
+ // then resolver_arenas and session.
105
104
impl Drop for BoxedResolverInner {
106
105
fn drop ( & mut self ) {
107
106
self . resolver . take ( ) ;
@@ -110,13 +109,10 @@ mod boxed_resolver {
110
109
}
111
110
112
111
impl BoxedResolver {
113
- pub ( super ) fn new < F > ( session : Lrc < Session > , make_resolver : F ) -> Result < ( ast:: Crate , Self ) >
114
- where
115
- F : for < ' a > FnOnce (
116
- & ' a Session ,
117
- & ' a ResolverArenas < ' a > ,
118
- ) -> Result < ( ast:: Crate , Resolver < ' a > ) > ,
119
- {
112
+ pub ( super ) fn new (
113
+ session : Lrc < Session > ,
114
+ make_resolver : impl for < ' a > FnOnce ( & ' a Session , & ' a ResolverArenas < ' a > ) -> Resolver < ' a > ,
115
+ ) -> BoxedResolver {
120
116
let mut boxed_resolver = Box :: new ( BoxedResolverInner {
121
117
session,
122
118
resolver_arenas : Some ( Resolver :: arenas ( ) ) ,
@@ -127,14 +123,14 @@ mod boxed_resolver {
127
123
// returns a resolver with the same lifetime as the arena. We ensure that the arena
128
124
// outlives the resolver in the drop impl and elsewhere so these transmutes are sound.
129
125
unsafe {
130
- let ( crate_ , resolver) = make_resolver (
126
+ let resolver = make_resolver (
131
127
std:: mem:: transmute :: < & Session , & Session > ( & boxed_resolver. session ) ,
132
128
std:: mem:: transmute :: < & ResolverArenas < ' _ > , & ResolverArenas < ' _ > > (
133
129
boxed_resolver. resolver_arenas . as_ref ( ) . unwrap ( ) ,
134
130
) ,
135
- ) ? ;
131
+ ) ;
136
132
boxed_resolver. resolver = Some ( resolver) ;
137
- Ok ( ( crate_ , BoxedResolver ( Pin :: new_unchecked ( boxed_resolver) ) ) )
133
+ BoxedResolver ( Pin :: new_unchecked ( boxed_resolver) )
138
134
}
139
135
}
140
136
@@ -165,35 +161,15 @@ mod boxed_resolver {
165
161
}
166
162
}
167
163
168
- /// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
169
- /// syntax expansion, secondary `cfg` expansion, synthesis of a test
170
- /// harness if one is to be provided, injection of a dependency on the
171
- /// standard library and prelude, and name resolution.
172
- ///
173
- /// Returns [`None`] if we're aborting after handling -W help.
174
- pub fn configure_and_expand (
164
+ pub fn create_resolver (
175
165
sess : Lrc < Session > ,
176
- lint_store : Lrc < LintStore > ,
177
166
metadata_loader : Box < MetadataLoaderDyn > ,
178
- krate : ast:: Crate ,
167
+ krate : & ast:: Crate ,
179
168
crate_name : & str ,
180
- ) -> Result < ( ast:: Crate , BoxedResolver ) > {
181
- tracing:: trace!( "configure_and_expand" ) ;
182
- // Currently, we ignore the name resolution data structures for the purposes of dependency
183
- // tracking. Instead we will run name resolution and include its output in the hash of each
184
- // item, much like we do for macro expansion. In other words, the hash reflects not just
185
- // its contents but the results of name resolution on those contents. Hopefully we'll push
186
- // this back at some point.
187
- let crate_name = crate_name. to_string ( ) ;
169
+ ) -> BoxedResolver {
170
+ tracing:: trace!( "create_resolver" ) ;
188
171
BoxedResolver :: new ( sess, move |sess, resolver_arenas| {
189
- configure_and_expand_inner (
190
- sess,
191
- & lint_store,
192
- krate,
193
- & crate_name,
194
- & resolver_arenas,
195
- metadata_loader,
196
- )
172
+ Resolver :: new ( sess, & krate, & crate_name, metadata_loader, & resolver_arenas)
197
173
} )
198
174
}
199
175
@@ -278,28 +254,24 @@ fn pre_expansion_lint(
278
254
} ) ;
279
255
}
280
256
281
- fn configure_and_expand_inner < ' a > (
282
- sess : & ' a Session ,
257
+ /// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
258
+ /// syntax expansion, secondary `cfg` expansion, synthesis of a test
259
+ /// harness if one is to be provided, injection of a dependency on the
260
+ /// standard library and prelude, and name resolution.
261
+ pub fn configure_and_expand (
262
+ sess : & Session ,
283
263
lint_store : & LintStore ,
284
264
mut krate : ast:: Crate ,
285
265
crate_name : & str ,
286
- resolver_arenas : & ' a ResolverArenas < ' a > ,
287
- metadata_loader : Box < MetadataLoaderDyn > ,
288
- ) -> Result < ( ast:: Crate , Resolver < ' a > ) > {
289
- tracing:: trace!( "configure_and_expand_inner" ) ;
266
+ resolver : & mut Resolver < ' _ > ,
267
+ ) -> Result < ast:: Crate > {
268
+ tracing:: trace!( "configure_and_expand" ) ;
290
269
pre_expansion_lint ( sess, lint_store, & krate, crate_name) ;
291
-
292
- let mut resolver = Resolver :: new ( sess, & krate, crate_name, metadata_loader, & resolver_arenas) ;
293
- rustc_builtin_macros:: register_builtin_macros ( & mut resolver) ;
270
+ rustc_builtin_macros:: register_builtin_macros ( resolver) ;
294
271
295
272
krate = sess. time ( "crate_injection" , || {
296
273
let alt_std_name = sess. opts . alt_std_name . as_ref ( ) . map ( |s| Symbol :: intern ( s) ) ;
297
- rustc_builtin_macros:: standard_library_imports:: inject (
298
- krate,
299
- & mut resolver,
300
- & sess,
301
- alt_std_name,
302
- )
274
+ rustc_builtin_macros:: standard_library_imports:: inject ( krate, resolver, & sess, alt_std_name)
303
275
} ) ;
304
276
305
277
util:: check_attr_crate_type ( & sess, & krate. attrs , & mut resolver. lint_buffer ( ) ) ;
@@ -354,7 +326,7 @@ fn configure_and_expand_inner<'a>(
354
326
pre_expansion_lint ( sess, lint_store, & krate, & ident. name . as_str ( ) ) ;
355
327
( krate. attrs , krate. items )
356
328
} ;
357
- let mut ecx = ExtCtxt :: new ( & sess, cfg, & mut resolver, Some ( & extern_mod_loaded) ) ;
329
+ let mut ecx = ExtCtxt :: new ( & sess, cfg, resolver, Some ( & extern_mod_loaded) ) ;
358
330
359
331
// Expand macros now!
360
332
let krate = sess. time ( "expand_crate" , || ecx. monotonic_expander ( ) . expand_crate ( krate) ) ;
@@ -396,16 +368,16 @@ fn configure_and_expand_inner<'a>(
396
368
} ) ?;
397
369
398
370
sess. time ( "maybe_building_test_harness" , || {
399
- rustc_builtin_macros:: test_harness:: inject ( & sess, & mut resolver, & mut krate)
371
+ rustc_builtin_macros:: test_harness:: inject ( & sess, resolver, & mut krate)
400
372
} ) ;
401
373
402
374
if let Some ( PpMode :: Source ( PpSourceMode :: EveryBodyLoops ) ) = sess. opts . pretty {
403
375
tracing:: debug!( "replacing bodies with loop {{}}" ) ;
404
- util:: ReplaceBodyWithLoop :: new ( & mut resolver) . visit_crate ( & mut krate) ;
376
+ util:: ReplaceBodyWithLoop :: new ( resolver) . visit_crate ( & mut krate) ;
405
377
}
406
378
407
379
let has_proc_macro_decls = sess. time ( "AST_validation" , || {
408
- rustc_ast_passes:: ast_validation:: check_crate ( sess, & krate, & mut resolver. lint_buffer ( ) )
380
+ rustc_ast_passes:: ast_validation:: check_crate ( sess, & krate, resolver. lint_buffer ( ) )
409
381
} ) ;
410
382
411
383
let crate_types = sess. crate_types ( ) ;
@@ -431,7 +403,7 @@ fn configure_and_expand_inner<'a>(
431
403
let is_test_crate = sess. opts . test ;
432
404
rustc_builtin_macros:: proc_macro_harness:: inject (
433
405
& sess,
434
- & mut resolver,
406
+ resolver,
435
407
krate,
436
408
is_proc_macro_crate,
437
409
has_proc_macro_decls,
@@ -471,26 +443,20 @@ fn configure_and_expand_inner<'a>(
471
443
}
472
444
} ) ;
473
445
474
- Ok ( ( krate, resolver ) )
446
+ Ok ( krate)
475
447
}
476
448
477
449
pub fn lower_to_hir < ' res , ' tcx > (
478
450
sess : & ' tcx Session ,
479
451
lint_store : & LintStore ,
480
452
resolver : & ' res mut Resolver < ' _ > ,
481
- dep_graph : & ' res DepGraph ,
482
- krate : & ' res ast:: Crate ,
453
+ krate : Rc < ast:: Crate > ,
483
454
arena : & ' tcx rustc_ast_lowering:: Arena < ' tcx > ,
484
- ) -> Crate < ' tcx > {
485
- // We're constructing the HIR here; we don't care what we will
486
- // read, since we haven't even constructed the *input* to
487
- // incr. comp. yet.
488
- dep_graph. assert_ignored ( ) ;
489
-
455
+ ) -> & ' tcx Crate < ' tcx > {
490
456
// Lower AST to HIR.
491
457
let hir_crate = rustc_ast_lowering:: lower_crate (
492
458
sess,
493
- & krate,
459
+ & * krate,
494
460
resolver,
495
461
rustc_parse:: nt_to_tokenstream,
496
462
arena,
@@ -511,6 +477,9 @@ pub fn lower_to_hir<'res, 'tcx>(
511
477
)
512
478
} ) ;
513
479
480
+ // Drop AST to free memory
481
+ sess. time ( "drop_ast" , || std:: mem:: drop ( krate) ) ;
482
+
514
483
// Discard hygiene data, which isn't required after lowering to HIR.
515
484
if !sess. opts . debugging_opts . keep_hygiene_data {
516
485
rustc_span:: hygiene:: clear_syntax_context_map ( ) ;
@@ -603,7 +572,7 @@ fn escape_dep_env(symbol: Symbol) -> String {
603
572
604
573
fn write_out_deps (
605
574
sess : & Session ,
606
- boxed_resolver : & Steal < Rc < RefCell < BoxedResolver > > > ,
575
+ boxed_resolver : & RefCell < BoxedResolver > ,
607
576
outputs : & OutputFilenames ,
608
577
out_filenames : & [ PathBuf ] ,
609
578
) {
@@ -630,7 +599,7 @@ fn write_out_deps(
630
599
}
631
600
632
601
if sess. binary_dep_depinfo ( ) {
633
- boxed_resolver. borrow ( ) . borrow_mut ( ) . access ( |resolver| {
602
+ boxed_resolver. borrow_mut ( ) . access ( |resolver| {
634
603
for cnum in resolver. cstore ( ) . crates_untracked ( ) {
635
604
let source = resolver. cstore ( ) . crate_source_untracked ( cnum) ;
636
605
if let Some ( ( path, _) ) = source. dylib {
@@ -699,7 +668,7 @@ pub fn prepare_outputs(
699
668
sess : & Session ,
700
669
compiler : & Compiler ,
701
670
krate : & ast:: Crate ,
702
- boxed_resolver : & Steal < Rc < RefCell < BoxedResolver > > > ,
671
+ boxed_resolver : & RefCell < BoxedResolver > ,
703
672
crate_name : & str ,
704
673
) -> Result < OutputFilenames > {
705
674
let _timer = sess. timer ( "prepare_outputs" ) ;
@@ -803,16 +772,26 @@ impl<'tcx> QueryContext<'tcx> {
803
772
pub fn create_global_ctxt < ' tcx > (
804
773
compiler : & ' tcx Compiler ,
805
774
lint_store : Lrc < LintStore > ,
806
- krate : & ' tcx Crate < ' tcx > ,
775
+ krate : Rc < ast :: Crate > ,
807
776
dep_graph : DepGraph ,
808
- resolver_outputs : ResolverOutputs ,
777
+ resolver : Rc < RefCell < BoxedResolver > > ,
809
778
outputs : OutputFilenames ,
810
779
crate_name : & str ,
811
780
queries : & ' tcx OnceCell < TcxQueries < ' tcx > > ,
812
781
global_ctxt : & ' tcx OnceCell < GlobalCtxt < ' tcx > > ,
813
782
arena : & ' tcx WorkerLocal < Arena < ' tcx > > ,
783
+ hir_arena : & ' tcx WorkerLocal < rustc_ast_lowering:: Arena < ' tcx > > ,
814
784
) -> QueryContext < ' tcx > {
785
+ // We're constructing the HIR here; we don't care what we will
786
+ // read, since we haven't even constructed the *input* to
787
+ // incr. comp. yet.
788
+ dep_graph. assert_ignored ( ) ;
789
+
815
790
let sess = & compiler. session ( ) ;
791
+ let krate = resolver
792
+ . borrow_mut ( )
793
+ . access ( |resolver| lower_to_hir ( sess, & lint_store, resolver, krate, hir_arena) ) ;
794
+ let resolver_outputs = BoxedResolver :: to_resolver_outputs ( resolver) ;
816
795
817
796
let query_result_on_disk_cache = rustc_incremental:: load_query_result_cache ( sess) ;
818
797
@@ -831,7 +810,7 @@ pub fn create_global_ctxt<'tcx>(
831
810
let queries = queries. get_or_init ( || TcxQueries :: new ( local_providers, extern_providers) ) ;
832
811
833
812
let gcx = sess. time ( "setup_global_ctxt" , || {
834
- global_ctxt. get_or_init ( || {
813
+ global_ctxt. get_or_init ( move || {
835
814
TyCtxt :: create_global_ctxt (
836
815
sess,
837
816
lint_store,
0 commit comments