Skip to content

Commit 9bd6dd4

Browse files
authored
Unrolled build for #142369
Rollup merge of #142369 - jdonszelmann:attr-docs, r=fmease Improve some attribute docs and rename groups r? `@nnethercote` Some naming here got changed at some point, and this feels more consistent. The docs changes were a direct response to `@mejrs` trying to implement a new parsers and running into this.
2 parents e703dff + ce04386 commit 9bd6dd4

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! - [`CombineAttributeParser`]: makes it easy to implement an attribute which should combine the
1313
//! contents of attributes, if an attribute appear multiple times in a list
1414
//!
15-
//! Attributes should be added to [`ATTRIBUTE_MAPPING`](crate::context::ATTRIBUTE_MAPPING) to be parsed.
15+
//! Attributes should be added to [`ATTRIBUTE_PARSERS`](crate::context::ATTRIBUTE_PARSERS) to be parsed.
1616
1717
use std::marker::PhantomData;
1818

@@ -51,6 +51,9 @@ type AcceptMapping<T> = &'static [(&'static [Symbol], AcceptFn<T>)];
5151
/// whether it has seen the attribute it has been looking for.
5252
///
5353
/// The state machine is automatically reset to parse attributes on the next item.
54+
///
55+
/// For a simpler attribute parsing interface, consider using [`SingleAttributeParser`]
56+
/// or [`CombineAttributeParser`] instead.
5457
pub(crate) trait AttributeParser: Default + 'static {
5558
/// The symbols for the attributes that this parser is interested in.
5659
///
@@ -59,6 +62,12 @@ pub(crate) trait AttributeParser: Default + 'static {
5962

6063
/// The parser has gotten a chance to accept the attributes on an item,
6164
/// here it can produce an attribute.
65+
///
66+
/// All finalize methods of all parsers are unconditionally called.
67+
/// This means you can't unconditionally return `Some` here,
68+
/// that'd be equivalent to unconditionally applying an attribute to
69+
/// every single syntax item that could have attributes applied to it.
70+
/// Your accept mappings should determine whether this returns something.
6271
fn finalize(self, cx: &FinalizeContext<'_>) -> Option<AttributeKind>;
6372
}
6473

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::attributes::transparency::TransparencyParser;
2222
use crate::attributes::{AttributeParser as _, Combine, Single};
2323
use crate::parser::{ArgParser, MetaItemParser};
2424

25-
macro_rules! attribute_groups {
25+
macro_rules! attribute_parsers {
2626
(
2727
pub(crate) static $name: ident = [$($names: ty),* $(,)?];
2828
) => {
@@ -63,8 +63,8 @@ macro_rules! attribute_groups {
6363
};
6464
}
6565

66-
attribute_groups!(
67-
pub(crate) static ATTRIBUTE_MAPPING = [
66+
attribute_parsers!(
67+
pub(crate) static ATTRIBUTE_PARSERS = [
6868
// tidy-alphabetical-start
6969
BodyStabilityParser,
7070
ConfusablesParser,
@@ -90,7 +90,7 @@ attribute_groups!(
9090
///
9191
/// Gives [`AttributeParser`]s enough information to create errors, for example.
9292
pub(crate) struct AcceptContext<'a> {
93-
pub(crate) group_cx: &'a FinalizeContext<'a>,
93+
pub(crate) finalize_cx: &'a FinalizeContext<'a>,
9494
/// The span of the attribute currently being parsed
9595
pub(crate) attr_span: Span,
9696
}
@@ -109,7 +109,7 @@ impl<'a> Deref for AcceptContext<'a> {
109109
type Target = FinalizeContext<'a>;
110110

111111
fn deref(&self) -> &Self::Target {
112-
&self.group_cx
112+
&self.finalize_cx
113113
}
114114
}
115115

@@ -219,7 +219,7 @@ impl<'sess> AttributeParser<'sess> {
219219
) -> Vec<Attribute> {
220220
let mut attributes = Vec::new();
221221

222-
let group_cx = FinalizeContext { cx: self, target_span };
222+
let finalize_cx = FinalizeContext { cx: self, target_span };
223223

224224
for attr in attrs {
225225
// If we're only looking for a single attribute, skip all the ones we don't care about.
@@ -268,9 +268,11 @@ impl<'sess> AttributeParser<'sess> {
268268
let args = parser.args();
269269
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
270270

271-
if let Some(accept) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {
272-
let cx =
273-
AcceptContext { group_cx: &group_cx, attr_span: lower_span(attr.span) };
271+
if let Some(accept) = ATTRIBUTE_PARSERS.0.get(parts.as_slice()) {
272+
let cx = AcceptContext {
273+
finalize_cx: &finalize_cx,
274+
attr_span: lower_span(attr.span),
275+
};
274276

275277
accept(&cx, &args)
276278
} else {
@@ -302,8 +304,8 @@ impl<'sess> AttributeParser<'sess> {
302304
}
303305

304306
let mut parsed_attributes = Vec::new();
305-
for f in &ATTRIBUTE_MAPPING.1 {
306-
if let Some(attr) = f(&group_cx) {
307+
for f in &ATTRIBUTE_PARSERS.1 {
308+
if let Some(attr) = f(&finalize_cx) {
307309
parsed_attributes.push(Attribute::Parsed(attr));
308310
}
309311
}

0 commit comments

Comments
 (0)