Skip to content

Commit 828cd60

Browse files
committed
Turn back to visitor
1 parent 7606f89 commit 828cd60

File tree

3 files changed

+56
-85
lines changed

3 files changed

+56
-85
lines changed

compiler/rustc_lint/src/levels.rs

+54-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_ast as ast;
1717
use rustc_ast_pretty::pprust;
1818
use rustc_data_structures::{
1919
fx::FxIndexMap,
20-
sync::{join, Lock, Lrc},
20+
sync::Lrc,
2121
};
2222
use rustc_errors::{Diag, DiagMessage, LintDiagnostic, MultiSpan};
2323
use rustc_feature::{Features, GateIssue};
@@ -162,9 +162,9 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
162162
pub fn lints_that_can_emit(tcx: TyCtxt<'_>, (): ()) -> Lrc<(Vec<String>, Vec<String>)> {
163163
let mut visitor = LintLevelMinimum::new(tcx);
164164
visitor.process_opts();
165-
visitor.lint_level_minimums();
165+
tcx.hir().walk_attributes(&mut visitor);
166166

167-
Lrc::new((visitor.lints_to_emit.into_inner(), visitor.lints_allowed.into_inner()))
167+
Lrc::new((visitor.lints_to_emit, visitor.lints_allowed))
168168
}
169169

170170
#[instrument(level = "trace", skip(tcx), ret)]
@@ -474,49 +474,77 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'
474474
struct LintLevelMinimum<'tcx> {
475475
tcx: TyCtxt<'tcx>,
476476
/// The actual list of detected lints.
477-
lints_to_emit: Lock<Vec<String>>,
478-
lints_allowed: Lock<Vec<String>>,
477+
lints_to_emit: Vec<String>,
478+
lints_allowed: Vec<String>,
479479
}
480480

481481
impl<'tcx> LintLevelMinimum<'tcx> {
482482
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
483483
Self {
484484
tcx,
485485
// That magic number is the current number of lints + some more for possible future lints
486-
lints_to_emit: Lock::new(Vec::with_capacity(230)),
487-
lints_allowed: Lock::new(Vec::with_capacity(100)),
486+
lints_to_emit: Vec::with_capacity(230),
487+
lints_allowed: Vec::with_capacity(100),
488488
}
489489
}
490490

491491
fn process_opts(&mut self) {
492492
for (lint, level) in &self.tcx.sess.opts.lint_opts {
493493
if *level == Level::Allow {
494-
self.lints_allowed.with_lock(|lints_allowed| lints_allowed.push(lint.to_string()));
494+
self.lints_allowed.push(lint.clone());
495495
} else {
496-
self.lints_to_emit.with_lock(|lints_to_emit| lints_to_emit.push(lint.to_string()));
496+
self.lints_to_emit.push(lint.to_string());
497497
}
498498
}
499499
}
500+
}
501+
502+
impl<'tcx> Visitor<'tcx> for LintLevelMinimum<'tcx> {
503+
type NestedFilter = nested_filter::All;
504+
505+
fn nested_visit_map(&mut self) -> Self::Map {
506+
self.tcx.hir()
507+
}
500508

501-
fn lint_level_minimums(&mut self) {
502-
join(
503-
|| {
504-
self.tcx.sess.psess.lints_that_can_emit.with_lock(|vec| {
505-
for lint_symbol in vec {
506-
self.lints_to_emit
507-
.with_lock(|lints_to_emit| lints_to_emit.push(lint_symbol.to_string()));
509+
fn visit_attribute(&mut self, attribute: &'tcx ast::Attribute) {
510+
if let Some(meta) = attribute.meta() {
511+
if [sym::warn, sym::deny, sym::forbid, sym::expect]
512+
.iter()
513+
.any(|kind| meta.has_name(*kind))
514+
{
515+
// SAFETY: Lint attributes are always a metalist inside a
516+
// metalist (even with just one lint).
517+
for meta_list in meta.meta_item_list().unwrap() {
518+
// If it's a tool lint (e.g. clippy::my_clippy_lint)
519+
if let ast::NestedMetaItem::MetaItem(meta_item) = meta_list {
520+
if meta_item.path.segments.len() == 1 {
521+
self.lints_to_emit.push(
522+
// SAFETY: Lint attributes can only have literals
523+
meta_list.ident().unwrap().name.as_str().to_string(),
524+
);
525+
} else {
526+
self.lints_to_emit
527+
.push(meta_item.path.segments[1].ident.name.as_str().to_string());
528+
}
508529
}
509-
});
510-
},
511-
|| {
512-
self.tcx.sess.psess.lints_allowed.with_lock(|vec| {
513-
for lint_symbol in vec {
514-
self.lints_allowed
515-
.with_lock(|lints_allowed| lints_allowed.push(lint_symbol.to_string()));
530+
}
531+
// We handle #![allow]s differently, as these remove checking rather than adding.
532+
} else if meta.has_name(sym::allow)
533+
&& let ast::AttrStyle::Inner = attribute.style
534+
{
535+
for meta_list in meta.meta_item_list().unwrap() {
536+
// If it's a tool lint (e.g. clippy::my_clippy_lint)
537+
if let ast::NestedMetaItem::MetaItem(meta_item) = meta_list {
538+
if meta_item.path.segments.len() == 1 {
539+
self.lints_allowed.push(meta_list.name_or_empty().as_str().to_string())
540+
} else {
541+
self.lints_allowed
542+
.push(meta_item.path.segments[1].ident.name.as_str().to_string());
543+
}
516544
}
517-
});
518-
},
519-
);
545+
}
546+
}
547+
}
520548
}
521549
}
522550

compiler/rustc_parse/src/parser/attr.rs

+2-50
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a> Parser<'a> {
117117
);
118118
let lo = self.token.span;
119119
// Attributes can't have attributes of their own [Editor's note: not with that attitude]
120-
let attribute_result = self.collect_tokens_no_attrs(|this| {
120+
self.collect_tokens_no_attrs(|this| {
121121
assert!(this.eat(&token::Pound), "parse_attribute called in non-attribute position");
122122

123123
let style =
@@ -133,55 +133,7 @@ impl<'a> Parser<'a> {
133133
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
134134
}
135135
Ok(attr::mk_attr_from_item(&self.psess.attr_id_generator, item, None, style, attr_sp))
136-
});
137-
138-
if let Ok(ref attr) = attribute_result
139-
&& let Some(meta) = attr.meta()
140-
{
141-
if let Some(first) = meta.path.segments.first() {
142-
if [sym::warn, sym::deny, sym::forbid, sym::expect]
143-
.iter()
144-
.any(|symbol| first.ident.name == *symbol)
145-
{
146-
for meta_list in attr.meta_item_list().unwrap() {
147-
// If it's a tool lint (e.g. clippy::my_clippy_lint)
148-
if let ast::NestedMetaItem::MetaItem(ref meta_item) = meta_list {
149-
if meta_item.path.segments.len() == 1 {
150-
self.psess.lints_that_can_emit.with_lock(|lints_that_can_emit| {
151-
lints_that_can_emit
152-
.push(meta_list.ident().unwrap().name.as_str().to_string());
153-
})
154-
} else {
155-
self.psess.lints_that_can_emit.with_lock(|lints_that_can_emit| {
156-
lints_that_can_emit.push(
157-
meta_item.path.segments[1].ident.name.as_str().to_string(),
158-
);
159-
})
160-
}
161-
}
162-
}
163-
} else if first.ident.name == sym::allow && attr.style == ast::AttrStyle::Inner {
164-
for meta_list in attr.meta_item_list().unwrap() {
165-
// If it's a tool lint (e.g. clippy::my_clippy_lint)
166-
if let ast::NestedMetaItem::MetaItem(ref meta_item) = meta_list {
167-
if meta_item.path.segments.len() == 1 {
168-
self.psess.lints_allowed.with_lock(|lints_allowed| {
169-
lints_allowed
170-
.push(meta_list.name_or_empty().as_str().to_string())
171-
})
172-
} else {
173-
self.psess.lints_allowed.with_lock(|lints_allowed| {
174-
lints_allowed.push(
175-
meta_item.path.segments[1].ident.name.as_str().to_string(),
176-
);
177-
})
178-
}
179-
}
180-
}
181-
}
182-
}
183-
}
184-
attribute_result
136+
})
185137
}
186138

187139
fn annotate_following_item_if_applicable(

compiler/rustc_session/src/parse.rs

-9
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,6 @@ pub struct ParseSess {
232232
proc_macro_quoted_spans: AppendOnlyVec<Span>,
233233
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
234234
pub attr_id_generator: AttrIdGenerator,
235-
/// All lints that can emit, even if not emitted (i.e. lints that are mentioned
236-
/// in e.g. #![warn] attributes)
237-
pub lints_that_can_emit: Lock<Vec<String>>,
238-
/// The list of lints that cannot emit, maybe because they are allowed
239-
/// globally, or the default level is Allow and they are not activated
240-
/// manually
241-
pub lints_allowed: Lock<Vec<String>>,
242235
}
243236

244237
impl ParseSess {
@@ -273,8 +266,6 @@ impl ParseSess {
273266
assume_incomplete_release: false,
274267
proc_macro_quoted_spans: Default::default(),
275268
attr_id_generator: AttrIdGenerator::new(),
276-
lints_that_can_emit: Lock::new(Vec::with_capacity(230)),
277-
lints_allowed: Lock::new(Vec::with_capacity(100)),
278269
}
279270
}
280271

0 commit comments

Comments
 (0)