Skip to content

Commit c90fc10

Browse files
committed
Querify early_lint_checks.
1 parent b7e2b04 commit c90fc10

File tree

7 files changed

+38
-19
lines changed

7 files changed

+38
-19
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ fn compute_hir_hash(
436436
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
437437
let sess = tcx.sess;
438438
tcx.ensure().output_filenames(());
439+
let _ = tcx.early_lint_checks(()); // Borrows `resolver_for_lowering`.
439440
let (mut resolver, krate) = tcx.resolver_for_lowering(()).steal();
440441

441442
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);

compiler/rustc_driver_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ fn run_compiler(
331331
if let Some(ppm) = &sess.opts.pretty {
332332
if ppm.needs_ast_map() {
333333
queries.global_ctxt()?.enter(|tcx| {
334+
tcx.ensure().early_lint_checks(());
334335
pretty::print_after_hir_lowering(tcx, *ppm);
335336
Ok(())
336337
})?;

compiler/rustc_interface/src/passes.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::parallel;
1111
use rustc_data_structures::steal::Steal;
1212
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1313
use rustc_errors::PResult;
14-
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
14+
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1515
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1616
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
1717
use rustc_metadata::creader::CStore;
@@ -302,6 +302,16 @@ fn configure_and_expand(mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>)
302302

303303
// Done with macro expansion!
304304

305+
resolver.resolve_crate(&krate);
306+
307+
krate
308+
}
309+
310+
fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
311+
let sess = tcx.sess;
312+
let (resolver, krate) = &*tcx.resolver_for_lowering(()).borrow();
313+
let mut lint_buffer = resolver.lint_buffer.steal();
314+
305315
if sess.opts.unstable_opts.input_stats {
306316
eprintln!("Post-expansion node count: {}", count_nodes(&krate));
307317
}
@@ -310,8 +320,6 @@ fn configure_and_expand(mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>)
310320
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS", "ast-stats-2");
311321
}
312322

313-
resolver.resolve_crate(&krate);
314-
315323
// Needs to go *after* expansion to be able to check the results of macro expansion.
316324
sess.time("complete_gated_feature_checking", || {
317325
rustc_ast_passes::feature_gate::check_crate(&krate, sess);
@@ -321,7 +329,7 @@ fn configure_and_expand(mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>)
321329
sess.parse_sess.buffered_lints.with_lock(|buffered_lints| {
322330
info!("{} parse sess buffered_lints", buffered_lints.len());
323331
for early_lint in buffered_lints.drain(..) {
324-
resolver.lint_buffer().add_early_lint(early_lint);
332+
lint_buffer.add_early_lint(early_lint);
325333
}
326334
});
327335

@@ -340,20 +348,16 @@ fn configure_and_expand(mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>)
340348
}
341349
});
342350

343-
sess.time("early_lint_checks", || {
344-
let lint_buffer = Some(std::mem::take(resolver.lint_buffer()));
345-
rustc_lint::check_ast_node(
346-
sess,
347-
false,
348-
lint_store,
349-
resolver.registered_tools(),
350-
lint_buffer,
351-
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
352-
&krate,
353-
)
354-
});
355-
356-
krate
351+
let lint_store = unerased_lint_store(tcx);
352+
rustc_lint::check_ast_node(
353+
sess,
354+
false,
355+
lint_store,
356+
tcx.registered_tools(()),
357+
Some(lint_buffer),
358+
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
359+
&**krate,
360+
)
357361
}
358362

359363
// Returns all the paths that correspond to generated files.
@@ -630,6 +634,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
630634
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
631635
providers.output_filenames = output_filenames;
632636
providers.resolver_for_lowering = resolver_for_lowering;
637+
providers.early_lint_checks = early_lint_checks;
633638
proc_macro_decls::provide(providers);
634639
rustc_const_eval::provide(providers);
635640
rustc_middle::hir::provide(providers);

compiler/rustc_lint_defs/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ pub enum BuiltinLintDiagnostics {
526526

527527
/// Lints that are buffered up early on in the `Session` before the
528528
/// `LintLevels` is calculated.
529+
#[derive(Debug)]
529530
pub struct BufferedEarlyLint {
530531
/// The span of code that we are linting on.
531532
pub span: MultiSpan,
@@ -544,7 +545,7 @@ pub struct BufferedEarlyLint {
544545
pub diagnostic: BuiltinLintDiagnostics,
545546
}
546547

547-
#[derive(Default)]
548+
#[derive(Default, Debug)]
548549
pub struct LintBuffer {
549550
pub map: FxIndexMap<NodeId, Vec<BufferedEarlyLint>>,
550551
}

compiler/rustc_middle/src/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ rustc_queries! {
3131
desc { "compute registered tools for crate" }
3232
}
3333

34+
query early_lint_checks(_: ()) -> () {
35+
desc { "perform lints prior to macro expansion" }
36+
}
37+
3438
query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
3539
feedable
3640
no_hash

compiler/rustc_middle/src/ty/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_attr as attr;
3434
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3535
use rustc_data_structures::intern::Interned;
3636
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
37+
use rustc_data_structures::steal::Steal;
3738
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3839
use rustc_errors::ErrorGuaranteed;
3940
use rustc_hir as hir;
@@ -44,6 +45,7 @@ use rustc_index::vec::IndexVec;
4445
use rustc_macros::HashStable;
4546
use rustc_query_system::ich::StableHashingContext;
4647
use rustc_serialize::{Decodable, Encodable};
48+
use rustc_session::lint::LintBuffer;
4749
pub use rustc_session::lint::RegisteredTools;
4850
use rustc_span::hygiene::MacroKind;
4951
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -211,6 +213,9 @@ pub struct ResolverAstLowering {
211213
pub builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>,
212214
/// List functions and methods for which lifetime elision was successful.
213215
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,
216+
217+
/// Lints that were emitted by the resolver and early lints.
218+
pub lint_buffer: Steal<LintBuffer>,
214219
}
215220

216221
#[derive(Clone, Copy, Debug)]

compiler/rustc_resolve/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
2727
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
2929
use rustc_data_structures::intern::Interned;
30+
use rustc_data_structures::steal::Steal;
3031
use rustc_data_structures::sync::{Lrc, MappedReadGuard};
3132
use rustc_errors::{
3233
Applicability, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, SubdiagnosticMessage,
@@ -1441,6 +1442,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14411442
trait_map: self.trait_map,
14421443
builtin_macro_kinds: self.builtin_macro_kinds,
14431444
lifetime_elision_allowed: self.lifetime_elision_allowed,
1445+
lint_buffer: Steal::new(self.lint_buffer),
14441446
};
14451447
ResolverOutputs { global_ctxt, ast_lowering }
14461448
}

0 commit comments

Comments
 (0)