Skip to content

Commit ab6e478

Browse files
committed
Auto merge of rust-lang#65835 - Mark-Simulacrum:lockless-lintbuffer, r=nikomatsakis
Remove LintBuffer from Session This moves the `LintBuffer` from `Session` into the `Resolver`, where it is used until lowering is done and then consumed by early lint passes. This also happily removes the failure mode of buffering lints too late where it would have previously lead to ICEs; it is statically no longer possible to do so. I suspect that with a bit more work a similar move could be done for the lint buffer inside `ParseSess`, but this PR doesn't touch it (in part to keep itself small). The last commit is the "interesting" commit -- the ones before it don't work (though they compile) as they sort of prepare the various crates for the lint buffer to be passed in rather than accessed through Session.
2 parents cba9368 + c68df7c commit ab6e478

File tree

16 files changed

+173
-153
lines changed

16 files changed

+173
-153
lines changed

src/librustc/hir/lowering.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
4343
use crate::hir::def::{Namespace, Res, DefKind, PartialRes, PerNS};
4444
use crate::hir::{GenericArg, ConstArg};
4545
use crate::hir::ptr::P;
46+
use crate::lint;
4647
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
4748
ELIDED_LIFETIMES_IN_PATHS};
4849
use crate::middle::cstore::CrateStore;
@@ -184,6 +185,8 @@ pub trait Resolver {
184185
) -> (ast::Path, Res<NodeId>);
185186

186187
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
188+
189+
fn lint_buffer(&mut self) -> &mut lint::LintBuffer;
187190
}
188191

189192
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
@@ -1857,7 +1860,7 @@ impl<'a> LoweringContext<'a> {
18571860
GenericArgs::Parenthesized(ref data) => match parenthesized_generic_args {
18581861
ParenthesizedGenericArgs::Ok => self.lower_parenthesized_parameter_data(data),
18591862
ParenthesizedGenericArgs::Warn => {
1860-
self.sess.buffer_lint(
1863+
self.resolver.lint_buffer().buffer_lint(
18611864
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
18621865
CRATE_NODE_ID,
18631866
data.span,
@@ -1953,7 +1956,7 @@ impl<'a> LoweringContext<'a> {
19531956
}
19541957
AnonymousLifetimeMode::PassThrough |
19551958
AnonymousLifetimeMode::ReportError => {
1956-
self.sess.buffer_lint_with_diagnostic(
1959+
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
19571960
ELIDED_LIFETIMES_IN_PATHS,
19581961
CRATE_NODE_ID,
19591962
path_span,
@@ -3346,15 +3349,15 @@ impl<'a> LoweringContext<'a> {
33463349
}
33473350
}
33483351

3349-
fn maybe_lint_bare_trait(&self, span: Span, id: NodeId, is_global: bool) {
3352+
fn maybe_lint_bare_trait(&mut self, span: Span, id: NodeId, is_global: bool) {
33503353
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
33513354
// call site which do not have a macro backtrace. See #61963.
33523355
let is_macro_callsite = self.sess.source_map()
33533356
.span_to_snippet(span)
33543357
.map(|snippet| snippet.starts_with("#["))
33553358
.unwrap_or(true);
33563359
if !is_macro_callsite {
3357-
self.sess.buffer_lint_with_diagnostic(
3360+
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
33583361
builtin::BARE_TRAIT_OBJECTS,
33593362
id,
33603363
span,

src/librustc/lint/context.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::util::common::time;
3434

3535
use errors::DiagnosticBuilder;
3636
use std::slice;
37-
use std::default::Default as StdDefault;
3837
use rustc_data_structures::sync::{self, ParallelIterator, join, par_iter};
3938
use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
4039
use syntax::ast;
@@ -584,12 +583,13 @@ impl<'a> EarlyContext<'a> {
584583
lint_store: &'a LintStore,
585584
krate: &'a ast::Crate,
586585
buffered: LintBuffer,
586+
warn_about_weird_lints: bool,
587587
) -> EarlyContext<'a> {
588588
EarlyContext {
589589
sess,
590590
krate,
591591
lint_store,
592-
builder: LintLevelSets::builder(sess, lint_store),
592+
builder: LintLevelSets::builder(sess, warn_about_weird_lints, lint_store),
593593
buffered,
594594
}
595595
}
@@ -1490,9 +1490,10 @@ fn early_lint_crate<T: EarlyLintPass>(
14901490
krate: &ast::Crate,
14911491
pass: T,
14921492
buffered: LintBuffer,
1493+
warn_about_weird_lints: bool,
14931494
) -> LintBuffer {
14941495
let mut cx = EarlyContextAndPass {
1495-
context: EarlyContext::new(sess, lint_store, krate, buffered),
1496+
context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints),
14961497
pass,
14971498
};
14981499

@@ -1514,22 +1515,19 @@ pub fn check_ast_crate<T: EarlyLintPass>(
15141515
lint_store: &LintStore,
15151516
krate: &ast::Crate,
15161517
pre_expansion: bool,
1518+
lint_buffer: Option<LintBuffer>,
15171519
builtin_lints: T,
15181520
) {
1519-
let (mut passes, mut buffered): (Vec<_>, _) = if pre_expansion {
1520-
(
1521-
lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect(),
1522-
LintBuffer::default(),
1523-
)
1521+
let mut passes: Vec<_> = if pre_expansion {
1522+
lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect()
15241523
} else {
1525-
(
1526-
lint_store.early_passes.iter().map(|p| (p)()).collect(),
1527-
sess.buffered_lints.borrow_mut().take().unwrap(),
1528-
)
1524+
lint_store.early_passes.iter().map(|p| (p)()).collect()
15291525
};
1526+
let mut buffered = lint_buffer.unwrap_or_default();
15301527

15311528
if !sess.opts.debugging_opts.no_interleave_lints {
1532-
buffered = early_lint_crate(sess, lint_store, krate, builtin_lints, buffered);
1529+
buffered = early_lint_crate(sess, lint_store, krate, builtin_lints, buffered,
1530+
pre_expansion);
15331531

15341532
if !passes.is_empty() {
15351533
buffered = early_lint_crate(
@@ -1538,6 +1536,7 @@ pub fn check_ast_crate<T: EarlyLintPass>(
15381536
krate,
15391537
EarlyLintPassObjects { lints: &mut passes[..] },
15401538
buffered,
1539+
pre_expansion,
15411540
);
15421541
}
15431542
} else {
@@ -1549,6 +1548,7 @@ pub fn check_ast_crate<T: EarlyLintPass>(
15491548
krate,
15501549
EarlyLintPassObjects { lints: slice::from_mut(pass) },
15511550
buffered,
1551+
pre_expansion,
15521552
)
15531553
});
15541554
}

src/librustc/lint/levels.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ impl LintLevelSets {
4444
return me
4545
}
4646

47-
pub fn builder<'a>(sess: &'a Session, store: &LintStore) -> LintLevelsBuilder<'a> {
48-
LintLevelsBuilder::new(sess, LintLevelSets::new(sess, store))
47+
pub fn builder<'a>(
48+
sess: &'a Session,
49+
warn_about_weird_lints: bool,
50+
store: &LintStore,
51+
) -> LintLevelsBuilder<'a> {
52+
LintLevelsBuilder::new(sess, warn_about_weird_lints, LintLevelSets::new(sess, store))
4953
}
5054

5155
fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
@@ -160,14 +164,18 @@ pub struct BuilderPush {
160164
}
161165

162166
impl<'a> LintLevelsBuilder<'a> {
163-
pub fn new(sess: &'a Session, sets: LintLevelSets) -> LintLevelsBuilder<'a> {
167+
pub fn new(
168+
sess: &'a Session,
169+
warn_about_weird_lints: bool,
170+
sets: LintLevelSets,
171+
) -> LintLevelsBuilder<'a> {
164172
assert_eq!(sets.list.len(), 1);
165173
LintLevelsBuilder {
166174
sess,
167175
sets,
168176
cur: 0,
169177
id_to_set: Default::default(),
170-
warn_about_weird_lints: sess.buffered_lints.borrow().is_some(),
178+
warn_about_weird_lints,
171179
}
172180
}
173181

src/librustc/lint/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,29 @@ impl LintBuffer {
643643
}
644644
}
645645

646-
pub fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
646+
fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
647647
self.map.remove(&id).unwrap_or_default()
648648
}
649649

650-
pub fn get_any(&self) -> Option<&[BufferedEarlyLint]> {
651-
let key = self.map.keys().next().map(|k| *k);
652-
key.map(|k| &self.map[&k][..])
650+
pub fn buffer_lint<S: Into<MultiSpan>>(
651+
&mut self,
652+
lint: &'static Lint,
653+
id: ast::NodeId,
654+
sp: S,
655+
msg: &str,
656+
) {
657+
self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
658+
}
659+
660+
pub fn buffer_lint_with_diagnostic<S: Into<MultiSpan>>(
661+
&mut self,
662+
lint: &'static Lint,
663+
id: ast::NodeId,
664+
sp: S,
665+
msg: &str,
666+
diagnostic: BuiltinLintDiagnostics,
667+
) {
668+
self.add_lint(lint, id, sp.into(), msg, diagnostic)
653669
}
654670
}
655671

@@ -779,7 +795,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
779795
assert_eq!(cnum, LOCAL_CRATE);
780796
let store = &tcx.lint_store;
781797
let mut builder = LintLevelMapBuilder {
782-
levels: LintLevelSets::builder(tcx.sess, &store),
798+
levels: LintLevelSets::builder(tcx.sess, false, &store),
783799
tcx: tcx,
784800
store: store,
785801
};

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ pub fn rustc_deprecation_message(depr: &RustcDeprecation, path: &str) -> (String
586586
}
587587

588588
pub fn early_report_deprecation(
589-
sess: &Session,
589+
lint_buffer: &'a mut lint::LintBuffer,
590590
message: &str,
591591
suggestion: Option<Symbol>,
592592
lint: &'static Lint,
@@ -597,7 +597,7 @@ pub fn early_report_deprecation(
597597
}
598598

599599
let diag = BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span);
600-
sess.buffer_lint_with_diagnostic(lint, CRATE_NODE_ID, span, message, diag);
600+
lint_buffer.buffer_lint_with_diagnostic(lint, CRATE_NODE_ID, span, message, diag);
601601
}
602602

603603
fn late_report_deprecation(

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14691469
(such as entering an empty infinite loop) by inserting llvm.sideeffect"),
14701470
}
14711471

1472-
pub fn default_lib_output() -> CrateType {
1472+
pub const fn default_lib_output() -> CrateType {
14731473
CrateType::Rlib
14741474
}
14751475

src/librustc/session/mod.rs

-38
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::hir::def_id::CrateNum;
66
use rustc_data_structures::fingerprint::Fingerprint;
77

88
use crate::lint;
9-
use crate::lint::builtin::BuiltinLintDiagnostics;
109
use crate::session::config::{OutputType, PrintRequest, Sanitizer, SwitchWithOptPath};
1110
use crate::session::search_paths::{PathKind, SearchPath};
1211
use crate::util::nodemap::{FxHashMap, FxHashSet};
@@ -77,13 +76,6 @@ pub struct Session {
7776
/// if the value stored here has been affected by path remapping.
7877
pub working_dir: (PathBuf, bool),
7978

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.
85-
pub buffered_lints: Lock<Option<lint::LintBuffer>>,
86-
8779
/// Set of `(DiagnosticId, Option<Span>, message)` tuples tracking
8880
/// (sub)diagnostics that have been set once, but should not be set again,
8981
/// in order to avoid redundantly verbose output (Issue #24690, #44953).
@@ -366,35 +358,6 @@ impl Session {
366358
self.diagnostic().span_note_without_error(sp, msg)
367359
}
368360

369-
pub fn buffer_lint<S: Into<MultiSpan>>(
370-
&self,
371-
lint: &'static lint::Lint,
372-
id: ast::NodeId,
373-
sp: S,
374-
msg: &str,
375-
) {
376-
match *self.buffered_lints.borrow_mut() {
377-
Some(ref mut buffer) => {
378-
buffer.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
379-
}
380-
None => bug!("can't buffer lints after HIR lowering"),
381-
}
382-
}
383-
384-
pub fn buffer_lint_with_diagnostic<S: Into<MultiSpan>>(
385-
&self,
386-
lint: &'static lint::Lint,
387-
id: ast::NodeId,
388-
sp: S,
389-
msg: &str,
390-
diagnostic: BuiltinLintDiagnostics,
391-
) {
392-
match *self.buffered_lints.borrow_mut() {
393-
Some(ref mut buffer) => buffer.add_lint(lint, id, sp.into(), msg, diagnostic),
394-
None => bug!("can't buffer lints after HIR lowering"),
395-
}
396-
}
397-
398361
pub fn reserve_node_ids(&self, count: usize) -> ast::NodeId {
399362
let id = self.next_node_id.get();
400363

@@ -1218,7 +1181,6 @@ fn build_session_(
12181181
sysroot,
12191182
local_crate_source_file,
12201183
working_dir,
1221-
buffered_lints: Lock::new(Some(Default::default())),
12221184
one_time_diagnostics: Default::default(),
12231185
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),
12241186
plugin_attributes: Lock::new(Vec::new()),

src/librustc_interface/passes.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ fn configure_and_expand_inner<'a>(
267267
lint_store,
268268
&krate,
269269
true,
270+
None,
270271
rustc_lint::BuiltinCombinedPreExpansionLintPass::new());
271272
});
272273

@@ -293,6 +294,8 @@ fn configure_and_expand_inner<'a>(
293294
krate
294295
});
295296

297+
util::check_attr_crate_type(&krate.attrs, &mut resolver.lint_buffer());
298+
296299
syntax_ext::plugin_macro_defs::inject(
297300
&mut krate, &mut resolver, plugin_info.syntax_exts, sess.edition()
298301
);
@@ -366,7 +369,7 @@ fn configure_and_expand_inner<'a>(
366369
for span in missing_fragment_specifiers {
367370
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
368371
let msg = "missing fragment specifier";
369-
sess.buffer_lint(lint, ast::CRATE_NODE_ID, span, msg);
372+
resolver.lint_buffer().buffer_lint(lint, ast::CRATE_NODE_ID, span, msg);
370373
}
371374
if cfg!(windows) {
372375
env::set_var("PATH", &old_path);
@@ -395,7 +398,7 @@ fn configure_and_expand_inner<'a>(
395398
}
396399

397400
let has_proc_macro_decls = time(sess, "AST validation", || {
398-
ast_validation::check_crate(sess, &krate)
401+
ast_validation::check_crate(sess, &krate, &mut resolver.lint_buffer())
399402
});
400403

401404

@@ -464,7 +467,7 @@ fn configure_and_expand_inner<'a>(
464467
info!("{} parse sess buffered_lints", buffered_lints.len());
465468
for BufferedEarlyLint{id, span, msg, lint_id} in buffered_lints.drain(..) {
466469
let lint = lint::Lint::from_parser_lint_id(lint_id);
467-
sess.buffer_lint(lint, id, span, &msg);
470+
resolver.lint_buffer().buffer_lint(lint, id, span, &msg);
468471
}
469472
});
470473

@@ -496,6 +499,7 @@ pub fn lower_to_hir(
496499
lint_store,
497500
&krate,
498501
false,
502+
Some(std::mem::take(resolver.lint_buffer())),
499503
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
500504
)
501505
});

0 commit comments

Comments
 (0)