Skip to content

Commit dab3bd6

Browse files
Create lint store during plugin registration
Remove lint store from Session
1 parent da56d1d commit dab3bd6

File tree

12 files changed

+83
-103
lines changed

12 files changed

+83
-103
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,6 +3483,7 @@ dependencies = [
34833483
"rustc_data_structures",
34843484
"rustc_errors",
34853485
"rustc_interface",
3486+
"rustc_lint",
34863487
"rustc_metadata",
34873488
"rustc_mir",
34883489
"rustc_plugin",

src/librustc/lint/context.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::util::common::time;
3535
use errors::DiagnosticBuilder;
3636
use std::slice;
3737
use std::default::Default as StdDefault;
38-
use rustc_data_structures::sync::{ReadGuard, ParallelIterator, join, par_iter};
38+
use rustc_data_structures::sync::{ParallelIterator, join, par_iter};
3939
use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
4040
use syntax::ast;
4141
use syntax::util::lev_distance::find_best_match_for_name;
@@ -452,7 +452,7 @@ pub struct LateContext<'a, 'tcx> {
452452
pub access_levels: &'a AccessLevels,
453453

454454
/// The store of registered lints and the lint levels.
455-
lint_store: ReadGuard<'a, LintStore>,
455+
lint_store: &'tcx LintStore,
456456

457457
last_node_with_lint_attrs: hir::HirId,
458458

@@ -1320,7 +1320,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
13201320
tables: &ty::TypeckTables::empty(None),
13211321
param_env: ty::ParamEnv::empty(),
13221322
access_levels,
1323-
lint_store: tcx.sess.lint_store.borrow(),
1323+
lint_store: &tcx.lint_store,
13241324
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(),
13251325
generics: None,
13261326
only_module: true,
@@ -1352,7 +1352,7 @@ pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
13521352

13531353
late_lint_mod_pass(tcx, module_def_id, builtin_lints);
13541354

1355-
let mut passes: Vec<_> = tcx.sess.lint_store.borrow().late_module_passes
1355+
let mut passes: Vec<_> = tcx.lint_store.late_module_passes
13561356
.iter().map(|pass| (pass)()).collect();
13571357

13581358
if !passes.is_empty() {
@@ -1370,7 +1370,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
13701370
tables: &ty::TypeckTables::empty(None),
13711371
param_env: ty::ParamEnv::empty(),
13721372
access_levels,
1373-
lint_store: tcx.sess.lint_store.borrow(),
1373+
lint_store: &tcx.lint_store,
13741374
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
13751375
generics: None,
13761376
only_module: false,
@@ -1394,7 +1394,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
13941394
}
13951395

13961396
fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) {
1397-
let mut passes = tcx.sess.lint_store.borrow()
1397+
let mut passes = tcx.lint_store
13981398
.late_passes.iter().map(|p| (p)()).collect::<Vec<_>>();
13991399

14001400
if !tcx.sess.opts.debugging_opts.no_interleave_lints {
@@ -1410,7 +1410,7 @@ fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, b
14101410
});
14111411
}
14121412

1413-
let mut passes: Vec<_> = tcx.sess.lint_store.borrow().late_module_passes
1413+
let mut passes: Vec<_> = tcx.lint_store.late_module_passes
14141414
.iter().map(|pass| (pass)()).collect();
14151415

14161416
for pass in &mut passes {
@@ -1571,7 +1571,7 @@ impl Decodable for LintId {
15711571
fn decode<D: Decoder>(d: &mut D) -> Result<LintId, D::Error> {
15721572
let s = d.read_str()?;
15731573
ty::tls::with(|tcx| {
1574-
match tcx.sess.lint_store.borrow().find_lints(&s) {
1574+
match tcx.lint_store.find_lints(&s) {
15751575
Ok(ids) => {
15761576
if ids.len() != 0 {
15771577
panic!("invalid lint-id `{}`", s);

src/librustc/lint/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,11 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
777777

778778
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
779779
assert_eq!(cnum, LOCAL_CRATE);
780-
let store = tcx.sess.lint_store.borrow();
780+
let store = &tcx.lint_store;
781781
let mut builder = LintLevelMapBuilder {
782782
levels: LintLevelSets::builder(tcx.sess, &store),
783783
tcx: tcx,
784-
store: &*store,
784+
store: store,
785785
};
786786
let krate = tcx.hir().krate();
787787

src/librustc/session/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::util::common::{duration_to_secs_str, ErrorReported};
1414

1515
use rustc_data_structures::base_n;
1616
use rustc_data_structures::sync::{
17-
self, Lrc, Lock, OneThread, Once, RwLock, AtomicU64, AtomicUsize, Ordering,
17+
self, Lrc, Lock, OneThread, Once, AtomicU64, AtomicUsize, Ordering,
1818
Ordering::SeqCst,
1919
};
2020

@@ -77,9 +77,11 @@ pub struct Session {
7777
/// if the value stored here has been affected by path remapping.
7878
pub working_dir: (PathBuf, bool),
7979

80-
// FIXME: `lint_store` and `buffered_lints` are not thread-safe,
81-
// but are only used in a single thread.
82-
pub lint_store: RwLock<lint::LintStore>,
80+
/// This is intended to be used from a single thread.
81+
///
82+
/// FIXME: there was a previous comment about this not being thread safe,
83+
/// but it's not clear how or why that's the case. The LintBuffer itself is certainly thread
84+
/// safe at least from a "Rust safety" standpoint.
8385
pub buffered_lints: Lock<Option<lint::LintBuffer>>,
8486

8587
/// Set of `(DiagnosticId, Option<Span>, message)` tuples tracking
@@ -1213,7 +1215,6 @@ fn build_session_(
12131215
sysroot,
12141216
local_crate_source_file,
12151217
working_dir,
1216-
lint_store: RwLock::new(lint::LintStore::new()),
12171218
buffered_lints: Lock::new(Some(Default::default())),
12181219
one_time_diagnostics: Default::default(),
12191220
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),

src/librustc/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,8 @@ pub struct GlobalCtxt<'tcx> {
10351035

10361036
pub sess: &'tcx Session,
10371037

1038+
pub lint_store: Lrc<lint::LintStore>,
1039+
10381040
pub dep_graph: DepGraph,
10391041

10401042
pub prof: SelfProfilerRef,
@@ -1199,6 +1201,7 @@ impl<'tcx> TyCtxt<'tcx> {
11991201
/// reference to the context, to allow formatting values that need it.
12001202
pub fn create_global_ctxt(
12011203
s: &'tcx Session,
1204+
lint_store: Lrc<lint::LintStore>,
12021205
cstore: &'tcx CrateStoreDyn,
12031206
local_providers: ty::query::Providers<'tcx>,
12041207
extern_providers: ty::query::Providers<'tcx>,
@@ -1268,6 +1271,7 @@ impl<'tcx> TyCtxt<'tcx> {
12681271

12691272
GlobalCtxt {
12701273
sess: s,
1274+
lint_store,
12711275
cstore,
12721276
arena: WorkerLocal::new(|_| Arena::default()),
12731277
interners,

src/librustc_driver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ log = "0.4"
1616
env_logger = { version = "0.7", default-features = false }
1717
rustc = { path = "../librustc" }
1818
rustc_target = { path = "../librustc_target" }
19+
rustc_lint = { path = "../librustc_lint" }
1920
rustc_data_structures = { path = "../librustc_data_structures" }
2021
errors = { path = "../librustc_errors", package = "rustc_errors" }
2122
rustc_metadata = { path = "../librustc_metadata" }

src/librustc_driver/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,13 @@ pub fn run_compiler(
202202
interface::run_compiler(config, |compiler| {
203203
let sopts = &compiler.session().opts;
204204
if sopts.describe_lints {
205+
let lint_store = rustc_lint::new_lint_store(
206+
sopts.debugging_opts.no_interleave_lints,
207+
compiler.session().unstable_options(),
208+
);
205209
describe_lints(
206210
compiler.session(),
207-
&*compiler.session().lint_store.borrow(),
211+
&lint_store,
208212
false
209213
);
210214
return;
@@ -321,12 +325,14 @@ pub fn run_compiler(
321325
return sess.compile_status();
322326
}
323327

324-
compiler.register_plugins()?;
328+
{
329+
let (_, _, lint_store) = &*compiler.register_plugins()?.peek();
325330

326-
// Lint plugins are registered; now we can process command line flags.
327-
if sess.opts.describe_lints {
328-
describe_lints(&sess, &sess.lint_store.borrow(), true);
329-
return sess.compile_status();
331+
// Lint plugins are registered; now we can process command line flags.
332+
if sess.opts.describe_lints {
333+
describe_lints(&sess, &lint_store, true);
334+
return sess.compile_status();
335+
}
330336
}
331337

332338
compiler.expansion()?;

src/librustc_interface/passes.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ declare_box_region_type!(
117117
/// Returns `None` if we're aborting after handling -W help.
118118
pub fn configure_and_expand(
119119
sess: Lrc<Session>,
120+
lint_store: Lrc<lint::LintStore>,
120121
cstore: Lrc<CStore>,
121122
krate: ast::Crate,
122123
crate_name: &str,
@@ -134,6 +135,7 @@ pub fn configure_and_expand(
134135
let resolver_arenas = Resolver::arenas();
135136
let res = configure_and_expand_inner(
136137
sess,
138+
&lint_store,
137139
&*cstore,
138140
krate,
139141
&crate_name,
@@ -227,7 +229,7 @@ pub fn register_plugins<'a>(
227229
cstore: &'a CStore,
228230
mut krate: ast::Crate,
229231
crate_name: &str,
230-
) -> Result<(ast::Crate, PluginInfo)> {
232+
) -> Result<(ast::Crate, PluginInfo, Lrc<lint::LintStore>)> {
231233
krate = time(sess, "attributes injection", || {
232234
syntax_ext::cmdline_attrs::inject(
233235
krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr
@@ -278,7 +280,12 @@ pub fn register_plugins<'a>(
278280
)
279281
});
280282

281-
let mut registry = Registry::new(sess, krate.span);
283+
let mut lint_store = rustc_lint::new_lint_store(
284+
sess.opts.debugging_opts.no_interleave_lints,
285+
sess.unstable_options(),
286+
);
287+
288+
let mut registry = Registry::new(sess, &mut lint_store, krate.span);
282289

283290
time(sess, "plugin registration", || {
284291
for registrar in registrars {
@@ -289,36 +296,20 @@ pub fn register_plugins<'a>(
289296

290297
let Registry {
291298
syntax_exts,
292-
early_lint_passes,
293-
late_lint_passes,
294-
lints,
295-
lint_groups,
296299
llvm_passes,
297300
attributes,
298301
..
299302
} = registry;
300303

301-
let mut ls = sess.lint_store.borrow_mut();
302-
ls.register_lints(&lints);
303-
for pass in early_lint_passes {
304-
ls.register_early_pass(pass);
305-
}
306-
for pass in late_lint_passes {
307-
ls.register_late_pass(pass);
308-
}
309-
310-
for (name, (to, deprecated_name)) in lint_groups {
311-
ls.register_group(true, name, deprecated_name, to);
312-
}
313-
314304
*sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
315305
*sess.plugin_attributes.borrow_mut() = attributes;
316306

317-
Ok((krate, PluginInfo { syntax_exts }))
307+
Ok((krate, PluginInfo { syntax_exts }, Lrc::new(lint_store)))
318308
}
319309

320310
fn configure_and_expand_inner<'a>(
321311
sess: &'a Session,
312+
lint_store: &'a lint::LintStore,
322313
cstore: &'a CStore,
323314
mut krate: ast::Crate,
324315
crate_name: &str,
@@ -329,7 +320,7 @@ fn configure_and_expand_inner<'a>(
329320
time(sess, "pre-AST-expansion lint checks", || {
330321
lint::check_ast_crate(
331322
sess,
332-
&*sess.lint_store.borrow(),
323+
lint_store,
333324
&krate,
334325
true,
335326
rustc_lint::BuiltinCombinedPreExpansionLintPass::new());
@@ -539,6 +530,7 @@ fn configure_and_expand_inner<'a>(
539530

540531
pub fn lower_to_hir(
541532
sess: &Session,
533+
lint_store: &lint::LintStore,
542534
cstore: &CStore,
543535
resolver: &mut Resolver<'_>,
544536
dep_graph: &DepGraph,
@@ -559,7 +551,7 @@ pub fn lower_to_hir(
559551
time(sess, "early lint checks", || {
560552
lint::check_ast_crate(
561553
sess,
562-
&*sess.lint_store.borrow(),
554+
lint_store,
563555
&krate,
564556
false,
565557
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
@@ -826,6 +818,7 @@ impl BoxedGlobalCtxt {
826818

827819
pub fn create_global_ctxt(
828820
compiler: &Compiler,
821+
lint_store: Lrc<lint::LintStore>,
829822
mut hir_forest: hir::map::Forest,
830823
defs: hir::map::Definitions,
831824
resolutions: Resolutions,
@@ -863,6 +856,7 @@ pub fn create_global_ctxt(
863856

864857
let gcx = TyCtxt::create_global_ctxt(
865858
sess,
859+
lint_store,
866860
cstore,
867861
local_providers,
868862
extern_providers,

src/librustc_interface/queries.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use crate::interface::{Compiler, Result};
22
use crate::passes::{self, BoxedResolver, ExpansionResult, BoxedGlobalCtxt, PluginInfo};
33

44
use rustc_incremental::DepGraphFuture;
5+
use rustc_data_structures::sync::Lrc;
56
use rustc::session::config::{OutputFilenames, OutputType};
67
use rustc::util::common::{time, ErrorReported};
78
use rustc::hir;
9+
use rustc::lint::LintStore;
810
use rustc::hir::def_id::LOCAL_CRATE;
911
use rustc::ty::steal::Steal;
1012
use rustc::dep_graph::DepGraph;
@@ -74,8 +76,8 @@ pub(crate) struct Queries {
7476
dep_graph_future: Query<Option<DepGraphFuture>>,
7577
parse: Query<ast::Crate>,
7678
crate_name: Query<String>,
77-
register_plugins: Query<(ast::Crate, PluginInfo)>,
78-
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>)>,
79+
register_plugins: Query<(ast::Crate, PluginInfo, Lrc<LintStore>)>,
80+
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
7981
dep_graph: Query<DepGraph>,
8082
lower_to_hir: Query<(Steal<hir::map::Forest>, ExpansionResult)>,
8183
prepare_outputs: Query<OutputFilenames>,
@@ -106,7 +108,7 @@ impl Compiler {
106108
})
107109
}
108110

109-
pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, PluginInfo)>> {
111+
pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, PluginInfo, Lrc<LintStore>)>> {
110112
self.queries.register_plugins.compute(|| {
111113
let crate_name = self.crate_name()?.peek().clone();
112114
let krate = self.parse()?.take();
@@ -148,17 +150,20 @@ impl Compiler {
148150

149151
pub fn expansion(
150152
&self
151-
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>)>> {
153+
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
152154
self.queries.expansion.compute(|| {
153155
let crate_name = self.crate_name()?.peek().clone();
154-
let (krate, plugin_info) = self.register_plugins()?.take();
156+
let (krate, plugin_info, lint_store) = self.register_plugins()?.take();
155157
passes::configure_and_expand(
156158
self.sess.clone(),
159+
lint_store.clone(),
157160
self.cstore().clone(),
158161
krate,
159162
&crate_name,
160163
plugin_info,
161-
).map(|(krate, resolver)| (krate, Steal::new(Rc::new(RefCell::new(resolver)))))
164+
).map(|(krate, resolver)| {
165+
(krate, Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
166+
})
162167
})
163168
}
164169

@@ -185,9 +190,11 @@ impl Compiler {
185190
let peeked = expansion_result.peek();
186191
let krate = &peeked.0;
187192
let resolver = peeked.1.steal();
193+
let lint_store = &peeked.2;
188194
let hir = Steal::new(resolver.borrow_mut().access(|resolver| {
189195
passes::lower_to_hir(
190196
self.session(),
197+
lint_store,
191198
self.cstore(),
192199
resolver,
193200
&*self.dep_graph()?.peek(),
@@ -212,11 +219,13 @@ impl Compiler {
212219
self.queries.global_ctxt.compute(|| {
213220
let crate_name = self.crate_name()?.peek().clone();
214221
let outputs = self.prepare_outputs()?.peek().clone();
222+
let lint_store = self.expansion()?.peek().2.clone();
215223
let hir = self.lower_to_hir()?;
216224
let hir = hir.peek();
217225
let (ref hir_forest, ref expansion) = *hir;
218226
Ok(passes::create_global_ctxt(
219227
self,
228+
lint_store,
220229
hir_forest.steal(),
221230
expansion.defs.steal(),
222231
expansion.resolutions.steal(),

0 commit comments

Comments
 (0)