1
1
use crate :: errors:: { FailedWritingFile , RustcErrorFatal , RustcErrorUnexpectedAnnotation } ;
2
2
use crate :: interface:: { Compiler , Result } ;
3
- use crate :: passes;
3
+ use crate :: { passes, util } ;
4
4
5
5
use rustc_ast as ast;
6
6
use rustc_codegen_ssa:: traits:: CodegenBackend ;
@@ -9,15 +9,14 @@ use rustc_data_structures::fx::FxIndexMap;
9
9
use rustc_data_structures:: steal:: Steal ;
10
10
use rustc_data_structures:: svh:: Svh ;
11
11
use 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 } ;
13
13
use rustc_hir:: definitions:: Definitions ;
14
14
use rustc_incremental:: DepGraphFuture ;
15
- use rustc_lint:: LintStore ;
16
15
use rustc_metadata:: creader:: CStore ;
17
16
use rustc_middle:: arena:: Arena ;
18
17
use rustc_middle:: dep_graph:: DepGraph ;
19
18
use rustc_middle:: ty:: { GlobalCtxt , TyCtxt } ;
20
- use rustc_session:: config:: { self , OutputFilenames , OutputType } ;
19
+ use rustc_session:: config:: { self , CrateType , OutputFilenames , OutputType } ;
21
20
use rustc_session:: cstore:: Untracked ;
22
21
use rustc_session:: { output:: find_crate_name, Session } ;
23
22
use rustc_span:: symbol:: sym;
@@ -85,12 +84,11 @@ pub struct Queries<'tcx> {
85
84
arena : WorkerLocal < Arena < ' tcx > > ,
86
85
hir_arena : WorkerLocal < rustc_hir:: Arena < ' tcx > > ,
87
86
88
- dep_graph_future : Query < Option < DepGraphFuture > > ,
89
87
parse : Query < ast:: Crate > ,
90
88
pre_configure : Query < ( ast:: Crate , ast:: AttrVec ) > ,
91
89
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 > ,
94
92
// This just points to what's in `gcx_cell`.
95
93
gcx : Query < & ' tcx GlobalCtxt < ' tcx > > ,
96
94
}
@@ -102,12 +100,11 @@ impl<'tcx> Queries<'tcx> {
102
100
gcx_cell : OnceCell :: new ( ) ,
103
101
arena : WorkerLocal :: new ( |_| Arena :: default ( ) ) ,
104
102
hir_arena : WorkerLocal :: new ( |_| rustc_hir:: Arena :: default ( ) ) ,
105
- dep_graph_future : Default :: default ( ) ,
106
103
parse : Default :: default ( ) ,
107
104
pre_configure : Default :: default ( ) ,
108
105
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 ( ) ,
111
108
gcx : Default :: default ( ) ,
112
109
}
113
110
}
@@ -119,13 +116,6 @@ impl<'tcx> Queries<'tcx> {
119
116
self . compiler . codegen_backend ( )
120
117
}
121
118
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
-
129
119
pub fn parse ( & self ) -> Result < QueryResult < ' _ , ast:: Crate > > {
130
120
self . parse
131
121
. compute ( || passes:: parse ( self . session ( ) ) . map_err ( |mut parse_error| parse_error. emit ( ) ) )
@@ -148,84 +138,111 @@ impl<'tcx> Queries<'tcx> {
148
138
} )
149
139
}
150
140
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) )
175
147
} )
176
148
}
177
149
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) )
186
155
} )
187
156
}
188
157
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 ( || {
191
160
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
+ ) )
208
167
} )
209
168
}
210
169
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
+
211
212
pub fn global_ctxt ( & ' tcx self ) -> Result < QueryResult < ' _ , & ' tcx GlobalCtxt < ' tcx > > > {
212
213
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 ( ) ?;
215
217
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 ( ) ;
217
222
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) ) ;
220
232
let source_span = AppendOnlyIndexVec :: new ( ) ;
221
233
let _id = source_span. push ( krate. spans . inner_span ) ;
222
234
debug_assert_eq ! ( _id, CRATE_DEF_ID ) ;
223
235
let untracked = Untracked { cstore, source_span, definitions } ;
224
236
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
+
225
242
let qcx = passes:: create_global_ctxt (
226
243
self . compiler ,
227
244
lint_store,
228
- self . dep_graph ( ) ? . steal ( ) ,
245
+ self . dep_graph ( dep_graph_future ) ,
229
246
untracked,
230
247
& self . gcx_cell ,
231
248
& self . arena ,
0 commit comments