Skip to content

Commit ff9276d

Browse files
committed
Refactor rustc_attr_data_structures documentation
Signed-off-by: xizheyin <[email protected]>
1 parent 91fad92 commit ff9276d

File tree

2 files changed

+170
-24
lines changed

2 files changed

+170
-24
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 136 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,59 +138,171 @@ impl Deprecation {
138138
}
139139
}
140140

141-
/// Represent parsed, *built in*, inert attributes.
141+
/// Represents parsed built-in inert attributes in the Rust compiler.
142142
///
143-
/// That means attributes that are not actually ever expanded.
144-
/// For more information on this, see the module docs on the [`rustc_attr_parsing`] crate.
145-
/// They're instead used as markers, to guide the compilation process in various way in most every stage of the compiler.
146-
/// These are kept around after the AST, into the HIR and further on.
143+
/// ## Overview
144+
/// These attributes are markers that guide the compilation process and are never expanded into other code.
145+
/// They persist throughout the compilation phases, from AST to HIR and beyond.
147146
///
148-
/// The word "parsed" could be a little misleading here, because the parser already parses
149-
/// attributes early on. However, the result, an [`ast::Attribute`]
150-
/// is only parsed at a high level, still containing a token stream in many cases. That is
151-
/// because the structure of the contents varies from attribute to attribute.
152-
/// With a parsed attribute I mean that each attribute is processed individually into a
153-
/// final structure, which on-site (the place where the attribute is useful for, think the
154-
/// the place where `must_use` is checked) little to no extra parsing or validating needs to
155-
/// happen.
147+
/// ## Attribute Processing
148+
/// While attributes are initially parsed into [`ast::Attribute`], they still contain raw token streams
149+
/// because different attributes have different internal structures. This enum represents the final,
150+
/// fully parsed form of these attributes, where each variant contains all necessary information
151+
/// in a structured format.
156152
///
157-
/// For more docs, look in [`rustc_attr_parsing`].
153+
/// ## Usage
154+
/// These parsed attributes are used throughout the compiler to:
155+
/// - Control code generation (e.g., `#[repr]`)
156+
/// - Mark API stability (`#[stable]`, `#[unstable]`)
157+
/// - Provide documentation (`#[doc]`)
158+
/// - Guide compiler behavior (e.g., `#[allow_internal_unstable]`)
158159
///
160+
/// ## Note on Attribute Organization
161+
/// Some attributes like `InlineAttr`, `OptimizeAttr`, and `InstructionSetAttr` are defined separately
162+
/// from this enum because they are used in specific compiler phases (like code generation) and don't
163+
/// need to persist throughout the entire compilation process. They are typically processed and
164+
/// converted into their final form earlier in the compilation pipeline.
165+
///
166+
/// For example:
167+
/// - `InlineAttr` is used during code generation to control function inlining
168+
/// - `OptimizeAttr` is used to control optimization levels
169+
/// - `InstructionSetAttr` is used for target-specific code generation
170+
///
171+
/// These attributes are handled by their respective compiler passes in the [`rustc_codegen_ssa`] crate
172+
/// and don't need to be preserved in the same way as the attributes in this enum.
173+
///
174+
/// For more details on attribute parsing, see the [`rustc_attr_parsing`] crate.
175+
///
176+
/// [`rustc_codegen_ssa`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/index.html
159177
/// [`rustc_attr_parsing`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html
160178
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
161179
pub enum AttributeKind {
162180
// tidy-alphabetical-start
181+
/// Allows unstable features in const functions.
182+
/// Contains a list of allowed feature names.
183+
///
184+
/// # Example
185+
/// ```rust
186+
/// #[allow_internal_unstable(const_fn)]
187+
/// ```
163188
AllowConstFnUnstable(ThinVec<Symbol>),
189+
190+
/// Allows internal unstable features.
191+
/// Contains pairs of feature names and their spans.
192+
///
193+
/// # Example
194+
/// ```rust
195+
/// #[allow_internal_unstable(feature1, feature2)]
196+
/// ```
164197
AllowInternalUnstable(ThinVec<(Symbol, Span)>),
198+
199+
/// Marks the stability of a default body implementation.
200+
/// Contains stability information and the attribute's span.
201+
///
202+
/// # Example
203+
/// ```rust
204+
/// #[rustc_default_body_unstable(feature = "default_foo", issue = "12345")]
205+
/// ```
165206
BodyStability {
166207
stability: DefaultBodyStability,
167208
/// Span of the `#[rustc_default_body_unstable(...)]` attribute
168209
span: Span,
169210
},
211+
212+
/// Marks confusable symbols.
213+
/// Contains the symbols and the span of the first occurrence.
214+
///
215+
/// # Example
216+
/// ```rust
217+
/// #[rustc_confusables("l", "I", "1")]
218+
/// ```
170219
Confusables {
171220
symbols: ThinVec<Symbol>,
172221
// FIXME(jdonszelmann): remove when target validation code is moved
173222
first_span: Span,
174223
},
224+
225+
/// Marks the stability of const functions.
226+
/// Contains stability information and the attribute's span.
227+
///
228+
/// # Example
229+
/// ```rust
230+
/// #[rustc_const_stable(feature = "const_foo", since = "1.50.0")]
231+
/// #[rustc_const_unstable(feature = "const_bar", issue = "12345")]
232+
/// ```
175233
ConstStability {
176234
stability: PartialConstStability,
177235
/// Span of the `#[rustc_const_stable(...)]` or `#[rustc_const_unstable(...)]` attribute
178236
span: Span,
179237
},
238+
239+
/// Indicates indirect const stability.
240+
///
241+
/// # Example
242+
/// ```rust
243+
/// #[rustc_const_stable(feature = "const_foo", since = "1.50.0")]
244+
/// #[rustc_const_stable(feature = "const_bar", since = "1.50.0")]
245+
/// ```
180246
ConstStabilityIndirect,
181-
Deprecation {
182-
deprecation: Deprecation,
183-
span: Span,
184-
},
247+
248+
/// Marks deprecated items.
249+
/// Contains deprecation information and the attribute's span.
250+
///
251+
/// # Example
252+
/// ```rust
253+
/// #[deprecated(since = "1.0.0", note = "Use new_function instead")]
254+
/// ```
255+
Deprecation { deprecation: Deprecation, span: Span },
256+
257+
/// Controls diagnostic behavior.
258+
///
259+
/// # Example
260+
/// ```rust
261+
/// #[rustc_on_unimplemented(
262+
/// message = "the type `{Self}` cannot be indexed by `{Idx}`",
263+
/// label = "the type `{Self}` cannot be indexed by `{Idx}`"
264+
/// )]
265+
/// ```
185266
Diagnostic(DiagnosticAttribute),
186-
DocComment {
187-
style: AttrStyle,
188-
kind: CommentKind,
189-
span: Span,
190-
comment: Symbol,
191-
},
267+
268+
/// Represents documentation comments.
269+
/// Contains the comment style, kind, span, and content.
270+
///
271+
/// # Example
272+
/// ```rust
273+
/// /// This is a documentation comment
274+
/// /// It can span multiple lines
275+
/// #[doc = "This is also a doc comment"]
276+
/// ```
277+
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
278+
279+
/// Controls macro transparency levels.
280+
///
281+
/// # Example
282+
/// ```rust
283+
/// #[rustc_macro_transparency = "semitransparent"]
284+
/// ```
192285
MacroTransparency(Transparency),
286+
287+
/// Controls type representation.
288+
/// Contains a list of repr attributes and their spans.
289+
///
290+
/// # Example
291+
/// ```rust
292+
/// #[repr(C)]
293+
/// #[repr(packed)]
294+
/// #[repr(align(8))]
295+
/// ```
193296
Repr(ThinVec<(ReprAttr, Span)>),
297+
298+
/// Marks API stability.
299+
/// Contains stability information and the attribute's span.
300+
///
301+
/// # Example
302+
/// ```rust
303+
/// #[stable(feature = "rust1", since = "1.0.0")]
304+
/// #[unstable(feature = "foo", issue = "12345")]
305+
/// ```
194306
Stability {
195307
stability: Stability,
196308
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute

compiler/rustc_attr_data_structures/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
//! Data structures for representing parsed attributes in the Rust compiler.
2+
//!
3+
//! This crate is part of a series of crates that handle attribute processing:
4+
//! - [rustc_attr_data_structures](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_data_structures/index.html): Defines the data structures that store parsed attributes
5+
//! - [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html): Handles the parsing of attributes
6+
//! - (planned) rustc_attr_validation: Will handle attribute validation
7+
//!
8+
//! ## Overview
9+
//! This crate defines the data structures that represent attributes after they have been parsed.
10+
//! These structures are used throughout the compiler to store and process attribute information.
11+
//!
12+
//! ## Key Components
13+
//! - [`AttributeKind`]: The main enum that represents different types of built-in inert attributes
14+
//! - [`InlineAttr`]: Controls function inlining behavior
15+
//! - [`OptimizeAttr`]: Controls optimization levels
16+
//! - [`InstructionSetAttr`]: Controls target-specific code generation
17+
//!
18+
//! ## Attribute Organization
19+
//! Attributes in this crate are organized based on their usage in the compiler:
20+
//! - Attributes in [`AttributeKind`] are used throughout the compilation process
21+
//! - Other attributes (like [`InlineAttr`]) are used in specific compiler phases
22+
//! and are handled by their respective passes in the [`rustc_codegen_ssa`] crate
23+
//!
24+
//! ## Related Crates
25+
//! - [`rustc_attr_parsing`]: Handles the parsing of attributes into these data structures
26+
//! - [`rustc_codegen_ssa`]: Uses some of these structures for code generation
27+
//!
28+
//! [`AttributeKind`]: attributes::AttributeKind
29+
//! [`InlineAttr`]: attributes::InlineAttr
30+
//! [`OptimizeAttr`]: attributes::OptimizeAttr
31+
//! [`InstructionSetAttr`]: attributes::InstructionSetAttr
32+
//! [`rustc_attr_parsing`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html
33+
//! [`rustc_codegen_ssa`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/index.html
34+
135
// tidy-alphabetical-start
236
#![allow(internal_features)]
337
#![doc(rust_logo)]

0 commit comments

Comments
 (0)