Skip to content

Commit 2b2dd25

Browse files
authored
Rollup merge of #117649 - nnethercote:mv-lint_store, r=cjgillot
Move `lint_store` Some nice cleanups enabled by the removal of compiler plugins. r? `@cjgillot`
2 parents f2f526c + dededd2 commit 2b2dd25

File tree

11 files changed

+61
-106
lines changed

11 files changed

+61
-106
lines changed

Diff for: compiler/rustc_driver_impl/src/lib.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_feature::find_gated_cfg;
3333
use rustc_fluent_macro::fluent_messages;
3434
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
3535
use rustc_interface::{interface, Queries};
36-
use rustc_lint::{unerased_lint_store, LintStore};
36+
use rustc_lint::unerased_lint_store;
3737
use rustc_metadata::locator;
3838
use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
3939
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType, TrimmedDefPaths};
@@ -356,16 +356,7 @@ fn run_compiler(
356356
let handler = EarlyErrorHandler::new(sopts.error_format);
357357

358358
if sopts.describe_lints {
359-
let mut lint_store =
360-
rustc_lint::new_lint_store(compiler.session().enable_internal_lints());
361-
let registered_lints =
362-
if let Some(register_lints) = compiler.register_lints() {
363-
register_lints(compiler.session(), &mut lint_store);
364-
true
365-
} else {
366-
false
367-
};
368-
describe_lints(compiler.session(), &lint_store, registered_lints);
359+
describe_lints(compiler.session());
369360
return;
370361
}
371362
let should_stop = print_crate_info(
@@ -442,9 +433,7 @@ fn run_compiler(
442433
}
443434

444435
if sess.opts.describe_lints {
445-
queries
446-
.global_ctxt()?
447-
.enter(|tcx| describe_lints(sess, unerased_lint_store(tcx), true));
436+
describe_lints(sess);
448437
return early_exit();
449438
}
450439

@@ -991,7 +980,7 @@ the command line flag directly.
991980
}
992981

993982
/// Write to stdout lint command options, together with a list of all available lints
994-
pub fn describe_lints(sess: &Session, lint_store: &LintStore, loaded_lints: bool) {
983+
pub fn describe_lints(sess: &Session) {
995984
safe_println!(
996985
"
997986
Available lint options:
@@ -1017,6 +1006,7 @@ Available lint options:
10171006
lints
10181007
}
10191008

1009+
let lint_store = unerased_lint_store(sess);
10201010
let (loaded, builtin): (Vec<_>, _) =
10211011
lint_store.get_lints().iter().cloned().partition(|&lint| lint.is_loaded);
10221012
let loaded = sort_lints(sess, loaded);
@@ -1094,7 +1084,7 @@ Available lint options:
10941084

10951085
print_lint_groups(builtin_groups, true);
10961086

1097-
match (loaded_lints, loaded.len(), loaded_groups.len()) {
1087+
match (sess.registered_lints, loaded.len(), loaded_groups.len()) {
10981088
(false, 0, _) | (false, _, 0) => {
10991089
safe_println!("Lint tools like Clippy can load additional lints and lint groups.");
11001090
}

Diff for: compiler/rustc_interface/src/interface.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
4040
pub struct Compiler {
4141
pub(crate) sess: Lrc<Session>,
4242
codegen_backend: Lrc<dyn CodegenBackend>,
43-
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
4443
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
4544
}
4645

@@ -51,9 +50,6 @@ impl Compiler {
5150
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
5251
&self.codegen_backend
5352
}
54-
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
55-
&self.register_lints
56-
}
5753
pub fn build_output_filenames(
5854
&self,
5955
sess: &Session,
@@ -485,10 +481,19 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
485481
sess.opts.untracked_state_hash = hasher.finish()
486482
}
487483

484+
// Even though the session holds the lint store, we can't build the
485+
// lint store until after the session exists. And we wait until now
486+
// so that `register_lints` sees the fully initialized session.
487+
let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
488+
if let Some(register_lints) = config.register_lints.as_deref() {
489+
register_lints(&sess, &mut lint_store);
490+
sess.registered_lints = true;
491+
}
492+
sess.lint_store = Some(Lrc::new(lint_store));
493+
488494
let compiler = Compiler {
489495
sess: Lrc::new(sess),
490496
codegen_backend: Lrc::from(codegen_backend),
491-
register_lints: config.register_lints,
492497
override_queries: config.override_queries,
493498
};
494499

Diff for: compiler/rustc_interface/src/passes.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ fn count_nodes(krate: &ast::Crate) -> usize {
7272
counter.count
7373
}
7474

75-
pub(crate) fn create_lint_store(
76-
sess: &Session,
77-
register_lints: Option<impl Fn(&Session, &mut LintStore)>,
78-
) -> LintStore {
79-
let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
80-
if let Some(register_lints) = register_lints {
81-
register_lints(sess, &mut lint_store);
82-
}
83-
lint_store
84-
}
85-
8675
fn pre_expansion_lint<'a>(
8776
sess: &Session,
8877
features: &Features,
@@ -138,7 +127,7 @@ fn configure_and_expand(
138127
let tcx = resolver.tcx();
139128
let sess = tcx.sess;
140129
let features = tcx.features();
141-
let lint_store = unerased_lint_store(tcx);
130+
let lint_store = unerased_lint_store(&tcx.sess);
142131
let crate_name = tcx.crate_name(LOCAL_CRATE);
143132
let lint_check_node = (&krate, pre_configured_attrs);
144133
pre_expansion_lint(
@@ -330,7 +319,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
330319
}
331320
});
332321

333-
let lint_store = unerased_lint_store(tcx);
322+
let lint_store = unerased_lint_store(&tcx.sess);
334323
rustc_lint::check_ast_node(
335324
sess,
336325
tcx.features(),
@@ -645,7 +634,6 @@ pub fn create_global_ctxt<'tcx>(
645634
compiler: &'tcx Compiler,
646635
crate_types: Vec<CrateType>,
647636
stable_crate_id: StableCrateId,
648-
lint_store: Lrc<LintStore>,
649637
dep_graph: DepGraph,
650638
untracked: Untracked,
651639
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
@@ -676,7 +664,6 @@ pub fn create_global_ctxt<'tcx>(
676664
sess,
677665
crate_types,
678666
stable_crate_id,
679-
lint_store,
680667
arena,
681668
hir_arena,
682669
untracked,

Diff for: compiler/rustc_interface/src/queries.rs

-3
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ impl<'tcx> Queries<'tcx> {
148148
);
149149
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id)?;
150150

151-
let lint_store =
152-
Lrc::new(passes::create_lint_store(sess, self.compiler.register_lints.as_deref()));
153151
let cstore = FreezeLock::new(Box::new(CStore::new(
154152
self.codegen_backend().metadata_loader(),
155153
stable_crate_id,
@@ -164,7 +162,6 @@ impl<'tcx> Queries<'tcx> {
164162
self.compiler,
165163
crate_types,
166164
stable_crate_id,
167-
lint_store,
168165
dep_graph,
169166
untracked,
170167
&self.gcx_cell,

Diff for: compiler/rustc_lint/src/context.rs

+5-27
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,6 @@ pub struct LateContext<'tcx> {
497497
/// Items accessible from the crate being checked.
498498
pub effective_visibilities: &'tcx EffectiveVisibilities,
499499

500-
/// The store of registered lints and the lint levels.
501-
pub lint_store: &'tcx LintStore,
502-
503500
pub last_node_with_lint_attrs: hir::HirId,
504501

505502
/// Generic type parameters in scope for the item we are in.
@@ -515,21 +512,14 @@ pub struct EarlyContext<'a> {
515512
pub buffered: LintBuffer,
516513
}
517514

518-
pub trait LintPassObject: Sized {}
519-
520-
impl LintPassObject for EarlyLintPassObject {}
521-
522-
impl LintPassObject for LateLintPassObject<'_> {}
523-
524-
pub trait LintContext: Sized {
525-
type PassObject: LintPassObject;
526-
515+
pub trait LintContext {
527516
fn sess(&self) -> &Session;
528-
fn lints(&self) -> &LintStore;
529517

530-
/// Emit a lint at the appropriate level, with an optional associated span and an existing diagnostic.
518+
/// Emit a lint at the appropriate level, with an optional associated span and an existing
519+
/// diagnostic.
531520
///
532-
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed explanation.
521+
/// Return value of the `decorate` closure is ignored, see [`struct_lint_level`] for a detailed
522+
/// explanation.
533523
///
534524
/// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature
535525
#[rustc_lint_diagnostics]
@@ -1059,17 +1049,11 @@ impl<'a> EarlyContext<'a> {
10591049
}
10601050

10611051
impl<'tcx> LintContext for LateContext<'tcx> {
1062-
type PassObject = LateLintPassObject<'tcx>;
1063-
10641052
/// Gets the overall compiler `Session` object.
10651053
fn sess(&self) -> &Session {
10661054
&self.tcx.sess
10671055
}
10681056

1069-
fn lints(&self) -> &LintStore {
1070-
&*self.lint_store
1071-
}
1072-
10731057
#[rustc_lint_diagnostics]
10741058
fn lookup<S: Into<MultiSpan>>(
10751059
&self,
@@ -1094,17 +1078,11 @@ impl<'tcx> LintContext for LateContext<'tcx> {
10941078
}
10951079

10961080
impl LintContext for EarlyContext<'_> {
1097-
type PassObject = EarlyLintPassObject;
1098-
10991081
/// Gets the overall compiler `Session` object.
11001082
fn sess(&self) -> &Session {
11011083
&self.builder.sess()
11021084
}
11031085

1104-
fn lints(&self) -> &LintStore {
1105-
self.builder.lint_store()
1106-
}
1107-
11081086
#[rustc_lint_diagnostics]
11091087
fn lookup<S: Into<MultiSpan>>(
11101088
&self,

Diff for: compiler/rustc_lint/src/late.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@
1717
use crate::{passes::LateLintPassObject, LateContext, LateLintPass, LintStore};
1818
use rustc_ast as ast;
1919
use rustc_data_structures::stack::ensure_sufficient_stack;
20-
use rustc_data_structures::sync::join;
20+
use rustc_data_structures::sync::{join, Lrc};
2121
use rustc_hir as hir;
2222
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
2323
use rustc_hir::intravisit as hir_visit;
2424
use rustc_middle::hir::nested_filter;
2525
use rustc_middle::ty::{self, TyCtxt};
2626
use rustc_session::lint::LintPass;
27+
use rustc_session::Session;
2728
use rustc_span::Span;
2829

2930
use std::any::Any;
3031
use std::cell::Cell;
3132

3233
/// Extract the `LintStore` from the query context.
33-
/// This function exists because we've erased `LintStore` as `dyn Any` in the context.
34-
pub fn unerased_lint_store(tcx: TyCtxt<'_>) -> &LintStore {
35-
let store: &dyn Any = &*tcx.lint_store;
34+
/// This function exists because we've erased `LintStore` as `dyn Any` in the session.
35+
pub fn unerased_lint_store(sess: &Session) -> &LintStore {
36+
assert!(sess.lint_store.is_some());
37+
let store: &Lrc<_> = sess.lint_store.as_ref().unwrap();
38+
let store: &dyn Any = &**store;
3639
store.downcast_ref().unwrap()
3740
}
3841

@@ -353,7 +356,6 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
353356
cached_typeck_results: Cell::new(None),
354357
param_env: ty::ParamEnv::empty(),
355358
effective_visibilities: &tcx.effective_visibilities(()),
356-
lint_store: unerased_lint_store(tcx),
357359
last_node_with_lint_attrs: tcx.hir().local_def_id_to_hir_id(module_def_id),
358360
generics: None,
359361
only_module: true,
@@ -362,8 +364,11 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
362364
// Note: `passes` is often empty. In that case, it's faster to run
363365
// `builtin_lints` directly rather than bundling it up into the
364366
// `RuntimeCombinedLateLintPass`.
365-
let mut passes: Vec<_> =
366-
unerased_lint_store(tcx).late_module_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
367+
let mut passes: Vec<_> = unerased_lint_store(&tcx.sess)
368+
.late_module_passes
369+
.iter()
370+
.map(|mk_pass| (mk_pass)(tcx))
371+
.collect();
367372
if passes.is_empty() {
368373
late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
369374
} else {
@@ -400,7 +405,7 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
400405
fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
401406
// Note: `passes` is often empty.
402407
let mut passes: Vec<_> =
403-
unerased_lint_store(tcx).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
408+
unerased_lint_store(&tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
404409

405410
if passes.is_empty() {
406411
return;
@@ -412,7 +417,6 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
412417
cached_typeck_results: Cell::new(None),
413418
param_env: ty::ParamEnv::empty(),
414419
effective_visibilities: &tcx.effective_visibilities(()),
415-
lint_store: unerased_lint_store(tcx),
416420
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
417421
generics: None,
418422
only_module: false,

Diff for: compiler/rustc_lint/src/levels.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl LintLevelSets {
123123
}
124124

125125
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
126-
let store = unerased_lint_store(tcx);
126+
let store = unerased_lint_store(&tcx.sess);
127127

128128
let mut builder = LintLevelsBuilder {
129129
sess: tcx.sess,
@@ -152,7 +152,7 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
152152

153153
#[instrument(level = "trace", skip(tcx), ret)]
154154
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLevelMap {
155-
let store = unerased_lint_store(tcx);
155+
let store = unerased_lint_store(&tcx.sess);
156156
let attrs = tcx.hir_attrs(owner);
157157

158158
let mut levels = LintLevelsBuilder {
@@ -548,10 +548,6 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
548548
self.features
549549
}
550550

551-
pub(crate) fn lint_store(&self) -> &LintStore {
552-
self.store
553-
}
554-
555551
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
556552
self.provider.current_specs()
557553
}

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
3939
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
4040
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4141
use rustc_data_structures::steal::Steal;
42-
use rustc_data_structures::sync::{self, FreezeReadGuard, Lock, Lrc, WorkerLocal};
42+
use rustc_data_structures::sync::{FreezeReadGuard, Lock, WorkerLocal};
4343
use rustc_data_structures::unord::UnordSet;
4444
use rustc_errors::{
4545
DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
@@ -69,7 +69,6 @@ use rustc_type_ir::TyKind::*;
6969
use rustc_type_ir::WithCachedTypeInfo;
7070
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags};
7171

72-
use std::any::Any;
7372
use std::borrow::Borrow;
7473
use std::cmp::Ordering;
7574
use std::fmt;
@@ -544,12 +543,6 @@ pub struct GlobalCtxt<'tcx> {
544543
/// `rustc_symbol_mangling` crate for more information.
545544
stable_crate_id: StableCrateId,
546545

547-
/// This only ever stores a `LintStore` but we don't want a dependency on that type here.
548-
///
549-
/// FIXME(Centril): consider `dyn LintStoreMarker` once
550-
/// we can upcast to `Any` for some additional type safety.
551-
pub lint_store: Lrc<dyn Any + sync::DynSync + sync::DynSend>,
552-
553546
pub dep_graph: DepGraph,
554547

555548
pub prof: SelfProfilerRef,
@@ -709,7 +702,6 @@ impl<'tcx> TyCtxt<'tcx> {
709702
s: &'tcx Session,
710703
crate_types: Vec<CrateType>,
711704
stable_crate_id: StableCrateId,
712-
lint_store: Lrc<dyn Any + sync::DynSend + sync::DynSync>,
713705
arena: &'tcx WorkerLocal<Arena<'tcx>>,
714706
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
715707
untracked: Untracked,
@@ -730,7 +722,6 @@ impl<'tcx> TyCtxt<'tcx> {
730722
sess: s,
731723
crate_types,
732724
stable_crate_id,
733-
lint_store,
734725
arena,
735726
hir_arena,
736727
interners,

0 commit comments

Comments
 (0)