Skip to content

Commit bead569

Browse files
Rollup merge of rust-lang#156091 - Bryntet:move-tools, r=JonathanBrouwer
change field `tools` on `AttributeParser` to hold `&'tcx RegisteredTools` Makes tools actually stored, and not just tool names this was originally part of rust-lang#155691 but was split out to make that PR smaller. r? @petrochenkov cc @JonathanBrouwer
2 parents 00d9219 + 76fd1b3 commit bead569

7 files changed

Lines changed: 29 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,7 @@ dependencies = [
35863586
"rustc_feature",
35873587
"rustc_hir",
35883588
"rustc_lexer",
3589+
"rustc_lint_defs",
35893590
"rustc_macros",
35903591
"rustc_parse",
35913592
"rustc_parse_format",

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ struct LoweringContext<'a, 'hir> {
164164

165165
impl<'a, 'hir> LoweringContext<'a, 'hir> {
166166
fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>) -> Self {
167-
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
168167
Self {
169168
tcx,
170169
resolver,
@@ -220,7 +219,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
220219
attribute_parser: AttributeParser::new(
221220
tcx.sess,
222221
tcx.features(),
223-
registered_tools,
222+
tcx.registered_tools(()),
224223
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
225224
),
226225
delayed_lints: Vec::new(),

compiler/rustc_attr_parsing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rustc_errors = { path = "../rustc_errors" }
1313
rustc_feature = { path = "../rustc_feature" }
1414
rustc_hir = { path = "../rustc_hir" }
1515
rustc_lexer = { path = "../rustc_lexer" }
16+
rustc_lint_defs = { path = "../rustc_lint_defs" }
1617
rustc_macros = { path = "../rustc_macros" }
1718
rustc_parse = { path = "../rustc_parse" }
1819
rustc_parse_format = { path = "../rustc_parse_format" }

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,18 @@ impl AttributeParser for NakedParser {
264264

265265
let span = self.span?;
266266

267+
let Some(tools) = cx.tools else {
268+
unreachable!("tools required while parsing attributes");
269+
};
270+
267271
// only if we found a naked attribute do we do the somewhat expensive check
268272
'outer: for other_attr in cx.all_attrs {
269273
for allowed_attr in ALLOW_LIST {
270-
if other_attr.segments().next().is_some_and(|i| cx.tools.contains(&i.name)) {
274+
if other_attr
275+
.segments()
276+
.next()
277+
.is_some_and(|i| tools.iter().any(|tool| tool.name == i.name))
278+
{
271279
// effectively skips the error message being emitted below
272280
// if it's a tool attribute
273281
continue 'outer;

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
88
use rustc_feature::{AttributeTemplate, Features};
99
use rustc_hir::attrs::AttributeKind;
1010
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
11+
use rustc_lint_defs::RegisteredTools;
1112
use rustc_session::Session;
1213
use rustc_session::lint::LintId;
1314
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
@@ -33,7 +34,7 @@ pub struct EmitAttribute(
3334
/// Context created once, for example as part of the ast lowering
3435
/// context, through which all attributes can be lowered.
3536
pub struct AttributeParser<'sess> {
36-
pub(crate) tools: Vec<Symbol>,
37+
pub(crate) tools: Option<&'sess RegisteredTools>,
3738
pub(crate) features: Option<&'sess Features>,
3839
pub(crate) sess: &'sess Session,
3940
pub(crate) should_emit: ShouldEmit,
@@ -59,6 +60,8 @@ impl<'sess> AttributeParser<'sess> {
5960
/// No diagnostics will be emitted when parsing limited. Lints are not emitted at all, while
6061
/// errors will be emitted as a delayed bugs. in other words, we *expect* attributes parsed
6162
/// with `parse_limited` to be reparsed later during ast lowering where we *do* emit the errors
63+
///
64+
/// Due to this function not taking in RegisteredTools, *do not* use this for parsing any lint attributes
6265
pub fn parse_limited(
6366
sess: &'sess Session,
6467
attrs: &[ast::Attribute],
@@ -79,6 +82,8 @@ impl<'sess> AttributeParser<'sess> {
7982

8083
/// This does the same as `parse_limited`, except it has a `should_emit` parameter which allows it to emit errors.
8184
/// Usually you want `parse_limited`, which emits no errors.
85+
///
86+
/// Due to this function not taking in RegisteredTools, *do not* use this for parsing any lint attributes
8287
pub fn parse_limited_should_emit(
8388
sess: &'sess Session,
8489
attrs: &[ast::Attribute],
@@ -98,6 +103,7 @@ impl<'sess> AttributeParser<'sess> {
98103
target_node_id,
99104
features,
100105
should_emit,
106+
None,
101107
);
102108
assert!(parsed.len() <= 1);
103109
parsed.pop()
@@ -119,8 +125,9 @@ impl<'sess> AttributeParser<'sess> {
119125
target_node_id: NodeId,
120126
features: Option<&'sess Features>,
121127
should_emit: ShouldEmit,
128+
tools: Option<&'sess RegisteredTools>,
122129
) -> Vec<Attribute> {
123-
let mut p = Self { features, tools: Vec::new(), parse_only, sess, should_emit };
130+
let mut p = Self { features, tools, parse_only, sess, should_emit };
124131
p.parse_attribute_list(
125132
attrs,
126133
target_span,
@@ -202,7 +209,7 @@ impl<'sess> AttributeParser<'sess> {
202209
parse_fn: fn(cx: &mut AcceptContext<'_, '_>, item: &I) -> T,
203210
template: &AttributeTemplate,
204211
) -> T {
205-
let mut parser = Self { features, tools: Vec::new(), parse_only: None, sess, should_emit };
212+
let mut parser = Self { features, tools: None, parse_only: None, sess, should_emit };
206213
let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| {
207214
sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
208215
};
@@ -237,10 +244,10 @@ impl<'sess> AttributeParser<'sess> {
237244
pub fn new(
238245
sess: &'sess Session,
239246
features: &'sess Features,
240-
tools: Vec<Symbol>,
247+
tools: &'sess RegisteredTools,
241248
should_emit: ShouldEmit,
242249
) -> Self {
243-
Self { features: Some(features), tools, parse_only: None, sess, should_emit }
250+
Self { features: Some(features), tools: Some(tools), parse_only: None, sess, should_emit }
244251
}
245252

246253
pub(crate) fn sess(&self) -> &'sess Session {
@@ -432,12 +439,13 @@ impl<'sess> AttributeParser<'sess> {
432439

433440
let attr = Attribute::Unparsed(Box::new(attr));
434441

435-
if self.tools.contains(&parts[0])
442+
if self.tools.is_some_and(|tools| {
443+
tools.iter().any(|tool| tool.name == parts[0])
436444
// FIXME: this can be removed once #152369 has been merged.
437445
// https://github.com/rust-lang/rust/pull/152369
438446
|| [sym::allow, sym::deny, sym::expect, sym::forbid, sym::warn]
439447
.contains(&parts[0])
440-
{
448+
}) {
441449
attributes.push(attr);
442450
} else {
443451
dropped_attributes.push(attr);

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22502250
self.cx.current_expansion.lint_node_id,
22512251
Some(self.cx.ecfg.features),
22522252
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
2253+
Some(self.cx.resolver.registered_tools()),
22532254
);
22542255

22552256
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
149149
let mut parser = AttributeParser::new(
150150
&self.r.tcx.sess,
151151
self.r.tcx.features(),
152-
Vec::new(),
152+
self.r.tcx().registered_tools(()),
153153
ShouldEmit::Nothing,
154154
);
155155
let attrs = parser.parse_attribute_list(

0 commit comments

Comments
 (0)