Skip to content

Commit a6ca4ff

Browse files
committed
Run early lint checks in the background
1 parent 3e2090c commit a6ca4ff

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/librustc_driver/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub fn run_compiler(
391391
})?;
392392
None
393393
} else {
394-
// Drop AST after creating GlobalCtxt to free memory
394+
// Drop a reference to the AST
395395
let prof = sess.prof.clone();
396396
let ast = queries.expansion()?.take().0;
397397
Some(Future::spawn(move || {
@@ -400,6 +400,11 @@ pub fn run_compiler(
400400
}))
401401
};
402402

403+
queries.global_ctxt()?;
404+
405+
// Drop a reference to the AST by waiting on the lint future.
406+
queries.lower_to_hir()?.take().1.join();
407+
403408
queries.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
404409

405410
// Ensure the AST is dropped by this point.
@@ -410,6 +415,7 @@ pub fn run_compiler(
410415
}
411416

412417
if sess.opts.debugging_opts.save_analysis {
418+
// Drop AST to free memory
413419
mem::drop(queries.expansion()?.take());
414420
}
415421

src/librustc_interface/passes.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,16 @@ fn configure_and_expand_inner<'a>(
435435
}
436436

437437
pub fn lower_to_hir<'res, 'tcx>(
438-
sess: &'tcx Session,
439-
lint_store: &LintStore,
438+
sess: Lrc<Session>,
439+
lint_store: Lrc<LintStore>,
440440
resolver: &'res mut Resolver<'_>,
441441
dep_graph: &'res DepGraph,
442-
krate: &'res ast::Crate,
442+
krate: Lrc<ast::Crate>,
443443
arena: &'tcx Arena<'tcx>,
444-
) -> Result<map::Forest<'tcx>> {
444+
) -> Result<(map::Forest<'tcx>, Future<'static, ()>)> {
445445
// Lower AST to HIR.
446446
let hir_crate = rustc_ast_lowering::lower_crate(
447-
sess,
447+
&sess,
448448
&dep_graph,
449449
&krate,
450450
resolver,
@@ -458,23 +458,27 @@ pub fn lower_to_hir<'res, 'tcx>(
458458

459459
let hir_forest = map::Forest::new(hir_crate, &dep_graph);
460460

461-
sess.time("early_lint_checks", || {
462-
rustc_lint::check_ast_crate(
463-
sess,
464-
lint_store,
465-
&krate,
466-
false,
467-
Some(std::mem::take(resolver.lint_buffer())),
468-
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
469-
)
470-
});
461+
let lint_buffer = std::mem::take(resolver.lint_buffer());
471462

472463
// Discard hygiene data, which isn't required after lowering to HIR.
473464
if !sess.opts.debugging_opts.keep_hygiene_data {
474465
rustc_span::hygiene::clear_syntax_context_map();
475466
}
476467

477-
Ok(hir_forest)
468+
let lints = Future::spawn(move || {
469+
sess.time("early_lint_checks", || {
470+
rustc_lint::check_ast_crate(
471+
&sess,
472+
&lint_store,
473+
&krate,
474+
false,
475+
Some(lint_buffer),
476+
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
477+
)
478+
})
479+
});
480+
481+
Ok((hir_forest, lints))
478482
}
479483

480484
// Returns all the paths that correspond to generated files.

src/librustc_interface/queries.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ pub struct Queries<'tcx> {
7777
Steal<(ast::Crate, Lrc<LintStore>)>,
7878
Steal<Future<'static, Option<DepGraphFuture>>>,
7979
)>,
80-
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
80+
expansion: Query<(Lrc<ast::Crate>, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
8181
dep_graph: Query<DepGraph>,
82-
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>,
82+
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Future<'static, ()>, Steal<ResolverOutputs>)>,
8383
prepare_outputs: Query<OutputFilenames>,
8484
global_ctxt: Query<QueryContext<'tcx>>,
8585
ongoing_codegen: Query<Box<dyn Any>>,
@@ -163,7 +163,7 @@ impl<'tcx> Queries<'tcx> {
163163

164164
pub fn expansion(
165165
&self,
166-
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
166+
) -> Result<&Query<(Lrc<ast::Crate>, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
167167
self.expansion.compute(|| {
168168
let crate_name = self.crate_name()?.peek().clone();
169169
let (krate, lint_store) = self.register_plugins()?.peek().0.steal();
@@ -176,7 +176,7 @@ impl<'tcx> Queries<'tcx> {
176176
&crate_name,
177177
)
178178
.map(|(krate, resolver)| {
179-
(krate, Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
179+
(Lrc::new(krate), Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
180180
})
181181
})
182182
}
@@ -204,25 +204,26 @@ impl<'tcx> Queries<'tcx> {
204204

205205
pub fn lower_to_hir(
206206
&'tcx self,
207-
) -> Result<&Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>> {
207+
) -> Result<&Query<(&'tcx map::Forest<'tcx>, Future<'static, ()>, Steal<ResolverOutputs>)>>
208+
{
208209
self.lower_to_hir.compute(|| {
209210
let expansion_result = self.expansion()?;
210211
let peeked = expansion_result.peek();
211-
let krate = &peeked.0;
212+
let krate = peeked.0.clone();
212213
let resolver = peeked.1.steal();
213-
let lint_store = &peeked.2;
214-
let hir = resolver.borrow_mut().access(|resolver| {
214+
let lint_store = peeked.2.clone();
215+
let (hir, lints) = resolver.borrow_mut().access(|resolver| {
215216
passes::lower_to_hir(
216-
self.session(),
217+
self.session().clone(),
217218
lint_store,
218219
resolver,
219220
&*self.dep_graph()?.peek(),
220-
&krate,
221+
krate,
221222
&self.arena,
222223
)
223224
})?;
224225
let hir = self.arena.alloc(hir);
225-
Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
226+
Ok((hir, lints, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
226227
})
227228
}
228229

@@ -248,7 +249,7 @@ impl<'tcx> Queries<'tcx> {
248249
let outputs = self.prepare_outputs()?.peek().clone();
249250
let lint_store = self.expansion()?.peek().2.clone();
250251
let hir = self.lower_to_hir()?.peek();
251-
let (ref hir_forest, ref resolver_outputs) = &*hir;
252+
let (ref hir_forest, _, ref resolver_outputs) = &*hir;
252253
let _timer = self.session().timer("create_global_ctxt");
253254
Ok(passes::create_global_ctxt(
254255
self.compiler,
@@ -338,6 +339,12 @@ impl Compiler {
338339
});
339340
});
340341

342+
// Join the early lint check future if has started, but haven't been stolen yet.
343+
let _join_lint_future = OnDrop(|| {
344+
let result = queries.lower_to_hir.result.borrow_mut().take();
345+
result.map(|result| result.map(|result| result.1.join()));
346+
});
347+
341348
let ret = f(&queries);
342349

343350
if self.session().opts.debugging_opts.query_stats {
@@ -369,6 +376,7 @@ impl Compiler {
369376
queries.global_ctxt()?;
370377

371378
// Drop AST after creating GlobalCtxt to free memory.
379+
queries.lower_to_hir()?.take().1.join();
372380
mem::drop(queries.expansion()?.take());
373381

374382
queries.ongoing_codegen()?;

0 commit comments

Comments
 (0)