Skip to content

Commit 761e546

Browse files
authored
Rollup merge of #140539 - nnethercote:simplify-attribute_groups, r=jdonszelmann
Simplify `attribute_groups` It's more complicated than it needs to be. r? ``@jdonszelmann``
2 parents 6341f4e + 354b1cb commit 761e546

File tree

8 files changed

+47
-48
lines changed

8 files changed

+47
-48
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session_diagnostics;
1010

1111
pub(crate) struct AllowInternalUnstableParser;
1212
impl CombineAttributeParser for AllowInternalUnstableParser {
13-
const PATH: &'static [rustc_span::Symbol] = &[sym::allow_internal_unstable];
13+
const PATH: &'static [Symbol] = &[sym::allow_internal_unstable];
1414
type Item = (Symbol, Span);
1515
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowInternalUnstable;
1616

@@ -24,7 +24,7 @@ impl CombineAttributeParser for AllowInternalUnstableParser {
2424

2525
pub(crate) struct AllowConstFnUnstableParser;
2626
impl CombineAttributeParser for AllowConstFnUnstableParser {
27-
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_allow_const_fn_unstable];
27+
const PATH: &'static [Symbol] = &[sym::rustc_allow_const_fn_unstable];
2828
type Item = Symbol;
2929
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowConstFnUnstable;
3030

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ fn get(
4242
}
4343

4444
impl SingleAttributeParser for DeprecationParser {
45-
const PATH: &'static [rustc_span::Symbol] = &[sym::deprecated];
45+
const PATH: &'static [Symbol] = &[sym::deprecated];
4646

47-
fn on_duplicate(cx: &AcceptContext<'_>, first_span: rustc_span::Span) {
47+
fn on_duplicate(cx: &AcceptContext<'_>, first_span: Span) {
4848
// FIXME(jdonszelmann): merge with errors from check_attrs.rs
4949
cx.emit_err(session_diagnostics::UnusedMultiple {
5050
this: cx.attr_span,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use std::marker::PhantomData;
1818

1919
use rustc_attr_data_structures::AttributeKind;
20-
use rustc_span::Span;
20+
use rustc_span::{Span, Symbol};
2121
use thin_vec::ThinVec;
2222

2323
use crate::context::{AcceptContext, FinalizeContext};
@@ -33,7 +33,7 @@ pub(crate) mod transparency;
3333
pub(crate) mod util;
3434

3535
type AcceptFn<T> = fn(&mut T, &AcceptContext<'_>, &ArgParser<'_>);
36-
type AcceptMapping<T> = &'static [(&'static [rustc_span::Symbol], AcceptFn<T>)];
36+
type AcceptMapping<T> = &'static [(&'static [Symbol], AcceptFn<T>)];
3737

3838
/// An [`AttributeParser`] is a type which searches for syntactic attributes.
3939
///
@@ -72,7 +72,7 @@ pub(crate) trait AttributeParser: Default + 'static {
7272
/// [`SingleAttributeParser`] can only convert attributes one-to-one, and cannot combine multiple
7373
/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
7474
pub(crate) trait SingleAttributeParser: 'static {
75-
const PATH: &'static [rustc_span::Symbol];
75+
const PATH: &'static [Symbol];
7676

7777
/// Caled when a duplicate attribute is found.
7878
///
@@ -119,7 +119,7 @@ type ConvertFn<E> = fn(ThinVec<E>) -> AttributeKind;
119119
/// [`CombineAttributeParser`] can only convert a single kind of attribute, and cannot combine multiple
120120
/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
121121
pub(crate) trait CombineAttributeParser: 'static {
122-
const PATH: &'static [rustc_span::Symbol];
122+
const PATH: &'static [Symbol];
123123

124124
type Item;
125125
const CONVERT: ConvertFn<Self::Item>;

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_abi::Align;
22
use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
33
use rustc_attr_data_structures::{AttributeKind, IntType, ReprAttr};
4-
use rustc_span::{Span, Symbol, sym};
4+
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
55

66
use super::{CombineAttributeParser, ConvertFn};
77
use crate::context::AcceptContext;
@@ -21,7 +21,7 @@ pub(crate) struct ReprParser;
2121

2222
impl CombineAttributeParser for ReprParser {
2323
type Item = (ReprAttr, Span);
24-
const PATH: &'static [rustc_span::Symbol] = &[sym::repr];
24+
const PATH: &'static [Symbol] = &[sym::repr];
2525
const CONVERT: ConvertFn<Self::Item> = AttributeKind::Repr;
2626

2727
fn extend<'a>(
@@ -99,7 +99,7 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
9999
let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
100100
(Some(ident.name), ident.span)
101101
} else {
102-
(None, rustc_span::DUMMY_SP)
102+
(None, DUMMY_SP)
103103
};
104104

105105
let args = param.args();

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl AttributeParser for BodyStabilityParser {
117117
pub(crate) struct ConstStabilityIndirectParser;
118118
// FIXME(jdonszelmann): single word attribute group when we have these
119119
impl SingleAttributeParser for ConstStabilityIndirectParser {
120-
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_const_stable_indirect];
120+
const PATH: &'static [Symbol] = &[sym::rustc_const_stable_indirect];
121121

122122
// ignore
123123
fn on_duplicate(_cx: &AcceptContext<'_>, _first_span: Span) {}

compiler/rustc_attr_parsing/src/attributes/transparency.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_attr_data_structures::AttributeKind;
22
use rustc_span::hygiene::Transparency;
3-
use rustc_span::sym;
3+
use rustc_span::{Span, Symbol, sym};
44

55
use super::{AcceptContext, SingleAttributeParser};
66
use crate::parser::ArgParser;
@@ -11,9 +11,9 @@ pub(crate) struct TransparencyParser;
1111
#[allow(rustc::untranslatable_diagnostic)]
1212
#[allow(rustc::diagnostic_outside_of_impl)]
1313
impl SingleAttributeParser for TransparencyParser {
14-
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_macro_transparency];
14+
const PATH: &'static [Symbol] = &[sym::rustc_macro_transparency];
1515

16-
fn on_duplicate(cx: &crate::context::AcceptContext<'_>, first_span: rustc_span::Span) {
16+
fn on_duplicate(cx: &crate::context::AcceptContext<'_>, first_span: Span) {
1717
cx.dcx().span_err(vec![first_span, cx.attr_span], "multiple macro transparency attributes");
1818
}
1919

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,29 @@ macro_rules! attribute_groups {
2626
(
2727
pub(crate) static $name: ident = [$($names: ty),* $(,)?];
2828
) => {
29-
pub(crate) static $name: LazyLock<(
30-
BTreeMap<&'static [Symbol], Vec<Box<dyn Fn(&AcceptContext<'_>, &ArgParser<'_>) + Send + Sync>>>,
31-
Vec<Box<dyn Send + Sync + Fn(&FinalizeContext<'_>) -> Option<AttributeKind>>>
32-
)> = LazyLock::new(|| {
33-
let mut accepts = BTreeMap::<_, Vec<Box<dyn Fn(&AcceptContext<'_>, &ArgParser<'_>) + Send + Sync>>>::new();
34-
let mut finalizes = Vec::<Box<dyn Send + Sync + Fn(&FinalizeContext<'_>) -> Option<AttributeKind>>>::new();
29+
type Accepts = BTreeMap<
30+
&'static [Symbol],
31+
Box<dyn Send + Sync + Fn(&AcceptContext<'_>, &ArgParser<'_>)>
32+
>;
33+
type Finalizes = Vec<
34+
Box<dyn Send + Sync + Fn(&FinalizeContext<'_>) -> Option<AttributeKind>>
35+
>;
36+
pub(crate) static $name: LazyLock<(Accepts, Finalizes)> = LazyLock::new(|| {
37+
let mut accepts = Accepts::new();
38+
let mut finalizes = Finalizes::new();
3539
$(
3640
{
3741
thread_local! {
3842
static STATE_OBJECT: RefCell<$names> = RefCell::new(<$names>::default());
3943
};
4044

4145
for (k, v) in <$names>::ATTRIBUTES {
42-
accepts.entry(*k).or_default().push(Box::new(|cx, args| {
46+
let old = accepts.insert(*k, Box::new(|cx, args| {
4347
STATE_OBJECT.with_borrow_mut(|s| {
4448
v(s, cx, args)
4549
})
4650
}));
51+
assert!(old.is_none());
4752
}
4853

4954
finalizes.push(Box::new(|cx| {
@@ -110,7 +115,8 @@ impl<'a> Deref for AcceptContext<'a> {
110115

111116
/// Context given to every attribute parser during finalization.
112117
///
113-
/// Gives [`AttributeParser`](crate::attributes::AttributeParser)s enough information to create errors, for example.
118+
/// Gives [`AttributeParser`](crate::attributes::AttributeParser)s enough information to create
119+
/// errors, for example.
114120
pub(crate) struct FinalizeContext<'a> {
115121
/// The parse context, gives access to the session and the
116122
/// diagnostics context.
@@ -141,10 +147,9 @@ pub struct AttributeParser<'sess> {
141147
sess: &'sess Session,
142148
features: Option<&'sess Features>,
143149

144-
/// *only* parse attributes with this symbol.
150+
/// *Only* parse attributes with this symbol.
145151
///
146-
/// Used in cases where we want the lowering infrastructure for
147-
/// parse just a single attribute.
152+
/// Used in cases where we want the lowering infrastructure for parse just a single attribute.
148153
parse_only: Option<Symbol>,
149154

150155
/// Can be used to instruct parsers to reduce the number of diagnostics it emits.
@@ -157,9 +162,9 @@ impl<'sess> AttributeParser<'sess> {
157162
/// One example where this is necessary, is to parse `feature` attributes themselves for
158163
/// example.
159164
///
160-
/// Try to use this as little as possible. Attributes *should* be lowered during `rustc_ast_lowering`.
161-
/// Some attributes require access to features to parse, which would crash if you tried to do so
162-
/// through [`parse_limited`](Self::parse_limited).
165+
/// Try to use this as little as possible. Attributes *should* be lowered during
166+
/// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
167+
/// crash if you tried to do so through [`parse_limited`](Self::parse_limited).
163168
///
164169
/// To make sure use is limited, supply a `Symbol` you'd like to parse. Only attributes with
165170
/// that symbol are picked out of the list of instructions and parsed. Those are returned.
@@ -217,19 +222,18 @@ impl<'sess> AttributeParser<'sess> {
217222
let group_cx = FinalizeContext { cx: self, target_span };
218223

219224
for attr in attrs {
220-
// if we're only looking for a single attribute,
221-
// skip all the ones we don't care about
225+
// If we're only looking for a single attribute, skip all the ones we don't care about.
222226
if let Some(expected) = self.parse_only {
223227
if !attr.has_name(expected) {
224228
continue;
225229
}
226230
}
227231

228-
// sometimes, for example for `#![doc = include_str!("readme.md")]`,
232+
// Sometimes, for example for `#![doc = include_str!("readme.md")]`,
229233
// doc still contains a non-literal. You might say, when we're lowering attributes
230234
// that's expanded right? But no, sometimes, when parsing attributes on macros,
231235
// we already use the lowering logic and these are still there. So, when `omit_doc`
232-
// is set we *also* want to ignore these
236+
// is set we *also* want to ignore these.
233237
if omit_doc == OmitDoc::Skip && attr.has_name(sym::doc) {
234238
continue;
235239
}
@@ -263,21 +267,17 @@ impl<'sess> AttributeParser<'sess> {
263267
let (path, args) = parser.deconstruct();
264268
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
265269

266-
if let Some(accepts) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {
267-
for f in accepts {
268-
let cx = AcceptContext {
269-
group_cx: &group_cx,
270-
attr_span: lower_span(attr.span),
271-
};
270+
if let Some(accept) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {
271+
let cx =
272+
AcceptContext { group_cx: &group_cx, attr_span: lower_span(attr.span) };
272273

273-
f(&cx, &args)
274-
}
274+
accept(&cx, &args)
275275
} else {
276-
// if we're here, we must be compiling a tool attribute... Or someone forgot to
277-
// parse their fancy new attribute. Let's warn them in any case. If you are that
278-
// person, and you really your attribute should remain unparsed, carefully read the
279-
// documentation in this module and if you still think so you can add an exception
280-
// to this assertion.
276+
// If we're here, we must be compiling a tool attribute... Or someone
277+
// forgot to parse their fancy new attribute. Let's warn them in any case.
278+
// If you are that person, and you really think your attribute should
279+
// remain unparsed, carefully read the documentation in this module and if
280+
// you still think so you can add an exception to this assertion.
281281

282282
// FIXME(jdonszelmann): convert other attributes, and check with this that
283283
// we caught em all

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use rustc_ast::{AttrArgs, DelimArgs, Expr, ExprKind, LitKind, MetaItemLit, Norma
1212
use rustc_ast_pretty::pprust;
1313
use rustc_errors::DiagCtxtHandle;
1414
use rustc_hir::{self as hir, AttrPath};
15-
use rustc_span::symbol::{Ident, kw, sym};
16-
use rustc_span::{ErrorGuaranteed, Span, Symbol};
15+
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
1716

1817
pub struct SegmentIterator<'a> {
1918
offset: usize,

0 commit comments

Comments
 (0)