11use crate :: errors:: { FailedWritingFile , RustcErrorFatal , RustcErrorUnexpectedAnnotation } ;
22use crate :: interface:: { Compiler , Result } ;
3- use crate :: passes;
3+ use crate :: { passes, util } ;
44
55use rustc_ast as ast;
66use rustc_codegen_ssa:: traits:: CodegenBackend ;
@@ -9,15 +9,14 @@ use rustc_data_structures::fx::FxIndexMap;
99use rustc_data_structures:: steal:: Steal ;
1010use rustc_data_structures:: svh:: Svh ;
1111use rustc_data_structures:: sync:: { AppendOnlyIndexVec , Lrc , OnceCell , RwLock , WorkerLocal } ;
12- use rustc_hir:: def_id:: { CRATE_DEF_ID , LOCAL_CRATE } ;
12+ use rustc_hir:: def_id:: { StableCrateId , CRATE_DEF_ID , LOCAL_CRATE } ;
1313use rustc_hir:: definitions:: Definitions ;
1414use rustc_incremental:: DepGraphFuture ;
15- use rustc_lint:: LintStore ;
1615use rustc_metadata:: creader:: CStore ;
1716use rustc_middle:: arena:: Arena ;
1817use rustc_middle:: dep_graph:: DepGraph ;
1918use rustc_middle:: ty:: { GlobalCtxt , TyCtxt } ;
20- use rustc_session:: config:: { self , OutputFilenames , OutputType } ;
19+ use rustc_session:: config:: { self , CrateType , OutputFilenames , OutputType } ;
2120use rustc_session:: cstore:: Untracked ;
2221use rustc_session:: { output:: find_crate_name, Session } ;
2322use rustc_span:: symbol:: sym;
@@ -85,12 +84,11 @@ pub struct Queries<'tcx> {
8584 arena : WorkerLocal < Arena < ' tcx > > ,
8685 hir_arena : WorkerLocal < rustc_hir:: Arena < ' tcx > > ,
8786
88- dep_graph_future : Query < Option < DepGraphFuture > > ,
8987 parse : Query < ast:: Crate > ,
9088 pre_configure : Query < ( ast:: Crate , ast:: AttrVec ) > ,
9189 crate_name : Query < Symbol > ,
92- register_plugins : Query < ( ast :: Crate , ast :: AttrVec , Lrc < LintStore > ) > ,
93- dep_graph : Query < DepGraph > ,
90+ crate_types : Query < Vec < CrateType > > ,
91+ stable_crate_id : Query < StableCrateId > ,
9492 // This just points to what's in `gcx_cell`.
9593 gcx : Query < & ' tcx GlobalCtxt < ' tcx > > ,
9694}
@@ -102,12 +100,11 @@ impl<'tcx> Queries<'tcx> {
102100 gcx_cell : OnceCell :: new ( ) ,
103101 arena : WorkerLocal :: new ( |_| Arena :: default ( ) ) ,
104102 hir_arena : WorkerLocal :: new ( |_| rustc_hir:: Arena :: default ( ) ) ,
105- dep_graph_future : Default :: default ( ) ,
106103 parse : Default :: default ( ) ,
107104 pre_configure : Default :: default ( ) ,
108105 crate_name : Default :: default ( ) ,
109- register_plugins : Default :: default ( ) ,
110- dep_graph : Default :: default ( ) ,
106+ crate_types : Default :: default ( ) ,
107+ stable_crate_id : Default :: default ( ) ,
111108 gcx : Default :: default ( ) ,
112109 }
113110 }
@@ -119,13 +116,6 @@ impl<'tcx> Queries<'tcx> {
119116 self . compiler . codegen_backend ( )
120117 }
121118
122- fn dep_graph_future ( & self ) -> Result < QueryResult < ' _ , Option < DepGraphFuture > > > {
123- self . dep_graph_future . compute ( || {
124- let sess = self . session ( ) ;
125- Ok ( sess. opts . build_dep_graph ( ) . then ( || rustc_incremental:: load_dep_graph ( sess) ) )
126- } )
127- }
128-
129119 pub fn parse ( & self ) -> Result < QueryResult < ' _ , ast:: Crate > > {
130120 self . parse
131121 . compute ( || passes:: parse ( self . session ( ) ) . map_err ( |mut parse_error| parse_error. emit ( ) ) )
@@ -148,84 +138,111 @@ impl<'tcx> Queries<'tcx> {
148138 } )
149139 }
150140
151- pub fn register_plugins (
152- & self ,
153- ) -> Result < QueryResult < ' _ , ( ast:: Crate , ast:: AttrVec , Lrc < LintStore > ) > > {
154- self . register_plugins . compute ( || {
155- let crate_name = * self . crate_name ( ) ?. borrow ( ) ;
156- let ( krate, pre_configured_attrs) = self . pre_configure ( ) ?. steal ( ) ;
157-
158- let empty: & ( dyn Fn ( & Session , & mut LintStore ) + Sync + Send ) = & |_, _| { } ;
159- let lint_store = passes:: register_plugins (
160- self . session ( ) ,
161- & * self . codegen_backend ( ) . metadata_loader ( ) ,
162- self . compiler . register_lints . as_deref ( ) . unwrap_or_else ( || empty) ,
163- & pre_configured_attrs,
164- crate_name,
165- ) ?;
166-
167- // Compute the dependency graph (in the background). We want to do
168- // this as early as possible, to give the DepGraph maximum time to
169- // load before dep_graph() is called, but it also can't happen
170- // until after rustc_incremental::prepare_session_directory() is
171- // called, which happens within passes::register_plugins().
172- self . dep_graph_future ( ) . ok ( ) ;
173-
174- Ok ( ( krate, pre_configured_attrs, Lrc :: new ( lint_store) ) )
141+ fn crate_name ( & self ) -> Result < QueryResult < ' _ , Symbol > > {
142+ self . crate_name . compute ( || {
143+ let pre_configure_result = self . pre_configure ( ) ?;
144+ let ( _, pre_configured_attrs) = & * pre_configure_result. borrow ( ) ;
145+ // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
146+ Ok ( find_crate_name ( self . session ( ) , pre_configured_attrs) )
175147 } )
176148 }
177149
178- fn crate_name ( & self ) -> Result < QueryResult < ' _ , Symbol > > {
179- self . crate_name . compute ( || {
180- Ok ( {
181- let pre_configure_result = self . pre_configure ( ) ?;
182- let ( _, pre_configured_attrs) = & * pre_configure_result. borrow ( ) ;
183- // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
184- find_crate_name ( self . session ( ) , pre_configured_attrs)
185- } )
150+ fn crate_types ( & self ) -> Result < QueryResult < ' _ , Vec < CrateType > > > {
151+ self . crate_types . compute ( || {
152+ let pre_configure_result = self . pre_configure ( ) ?;
153+ let ( _, pre_configured_attrs) = & * pre_configure_result. borrow ( ) ;
154+ Ok ( util:: collect_crate_types ( & self . session ( ) , & pre_configured_attrs) )
186155 } )
187156 }
188157
189- fn dep_graph ( & self ) -> Result < QueryResult < ' _ , DepGraph > > {
190- self . dep_graph . compute ( || {
158+ fn stable_crate_id ( & self ) -> Result < QueryResult < ' _ , StableCrateId > > {
159+ self . stable_crate_id . compute ( || {
191160 let sess = self . session ( ) ;
192- let future_opt = self . dep_graph_future ( ) ?. steal ( ) ;
193- let dep_graph = future_opt
194- . and_then ( |future| {
195- let ( prev_graph, mut prev_work_products) =
196- sess. time ( "blocked_on_dep_graph_loading" , || future. open ( ) . open ( sess) ) ;
197- // Convert from UnordMap to FxIndexMap by sorting
198- let prev_work_product_ids =
199- prev_work_products. items ( ) . map ( |x| * x. 0 ) . into_sorted_stable_ord ( ) ;
200- let prev_work_products = prev_work_product_ids
201- . into_iter ( )
202- . map ( |x| ( x, prev_work_products. remove ( & x) . unwrap ( ) ) )
203- . collect :: < FxIndexMap < _ , _ > > ( ) ;
204- rustc_incremental:: build_dep_graph ( sess, prev_graph, prev_work_products)
205- } )
206- . unwrap_or_else ( DepGraph :: new_disabled) ;
207- Ok ( dep_graph)
161+ Ok ( StableCrateId :: new (
162+ * self . crate_name ( ) ?. borrow ( ) ,
163+ self . crate_types ( ) ?. borrow ( ) . contains ( & CrateType :: Executable ) ,
164+ sess. opts . cg . metadata . clone ( ) ,
165+ sess. cfg_version ,
166+ ) )
208167 } )
209168 }
210169
170+ fn dep_graph_future ( & self ) -> Result < Option < DepGraphFuture > > {
171+ let sess = self . session ( ) ;
172+ let crate_name = * self . crate_name ( ) ?. borrow ( ) ;
173+ let stable_crate_id = * self . stable_crate_id ( ) ?. borrow ( ) ;
174+
175+ // `load_dep_graph` can only be called after `prepare_session_directory`.
176+ rustc_incremental:: prepare_session_directory ( sess, crate_name, stable_crate_id) ?;
177+ let res = sess. opts . build_dep_graph ( ) . then ( || rustc_incremental:: load_dep_graph ( sess) ) ;
178+
179+ if sess. opts . incremental . is_some ( ) {
180+ sess. time ( "incr_comp_garbage_collect_session_directories" , || {
181+ if let Err ( e) = rustc_incremental:: garbage_collect_session_directories ( sess) {
182+ warn ! (
183+ "Error while trying to garbage collect incremental \
184+ compilation cache directory: {}",
185+ e
186+ ) ;
187+ }
188+ } ) ;
189+ }
190+
191+ Ok ( res)
192+ }
193+
194+ fn dep_graph ( & self , dep_graph_future : Option < DepGraphFuture > ) -> DepGraph {
195+ dep_graph_future
196+ . and_then ( |future| {
197+ let sess = self . session ( ) ;
198+ let ( prev_graph, mut prev_work_products) =
199+ sess. time ( "blocked_on_dep_graph_loading" , || future. open ( ) . open ( sess) ) ;
200+ // Convert from UnordMap to FxIndexMap by sorting
201+ let prev_work_product_ids =
202+ prev_work_products. items ( ) . map ( |x| * x. 0 ) . into_sorted_stable_ord ( ) ;
203+ let prev_work_products = prev_work_product_ids
204+ . into_iter ( )
205+ . map ( |x| ( x, prev_work_products. remove ( & x) . unwrap ( ) ) )
206+ . collect :: < FxIndexMap < _ , _ > > ( ) ;
207+ rustc_incremental:: build_dep_graph ( sess, prev_graph, prev_work_products)
208+ } )
209+ . unwrap_or_else ( DepGraph :: new_disabled)
210+ }
211+
211212 pub fn global_ctxt ( & ' tcx self ) -> Result < QueryResult < ' _ , & ' tcx GlobalCtxt < ' tcx > > > {
212213 self . gcx . compute ( || {
213- let crate_name = * self . crate_name ( ) ?. borrow ( ) ;
214- let ( krate, pre_configured_attrs, lint_store) = self . register_plugins ( ) ?. steal ( ) ;
214+ // Compute the dependency graph (in the background). We want to do this as early as
215+ // possible, to give the DepGraph maximum time to load before `dep_graph` is called.
216+ let dep_graph_future = self . dep_graph_future ( ) ?;
215217
216- let sess = self . session ( ) ;
218+ let crate_name = self . crate_name ( ) ?. steal ( ) ;
219+ let crate_types = self . crate_types ( ) ?. steal ( ) ;
220+ let stable_crate_id = self . stable_crate_id ( ) ?. steal ( ) ;
221+ let ( krate, pre_configured_attrs) = self . pre_configure ( ) ?. steal ( ) ;
217222
218- let cstore = RwLock :: new ( Box :: new ( CStore :: new ( sess) ) as _ ) ;
219- let definitions = RwLock :: new ( Definitions :: new ( sess. local_stable_crate_id ( ) ) ) ;
223+ let sess = self . session ( ) ;
224+ let lint_store = Lrc :: new ( passes:: create_lint_store (
225+ sess,
226+ & * self . codegen_backend ( ) . metadata_loader ( ) ,
227+ self . compiler . register_lints . as_deref ( ) ,
228+ & pre_configured_attrs,
229+ ) ) ;
230+ let cstore = RwLock :: new ( Box :: new ( CStore :: new ( stable_crate_id) ) as _ ) ;
231+ let definitions = RwLock :: new ( Definitions :: new ( stable_crate_id) ) ;
220232 let source_span = AppendOnlyIndexVec :: new ( ) ;
221233 let _id = source_span. push ( krate. spans . inner_span ) ;
222234 debug_assert_eq ! ( _id, CRATE_DEF_ID ) ;
223235 let untracked = Untracked { cstore, source_span, definitions } ;
224236
237+ // FIXME: Move these fields from session to tcx and make them immutable.
238+ sess. init_crate_types ( crate_types) ;
239+ sess. stable_crate_id . set ( stable_crate_id) . expect ( "not yet initialized" ) ;
240+ sess. init_features ( rustc_expand:: config:: features ( sess, & pre_configured_attrs) ) ;
241+
225242 let qcx = passes:: create_global_ctxt (
226243 self . compiler ,
227244 lint_store,
228- self . dep_graph ( ) ? . steal ( ) ,
245+ self . dep_graph ( dep_graph_future ) ,
229246 untracked,
230247 & self . gcx_cell ,
231248 & self . arena ,
0 commit comments