Skip to content

Commit 4ef6670

Browse files
committed
Refactor rustc_attr_data_structures documentation
Signed-off-by: xizheyin <[email protected]>
1 parent ed44c0e commit 4ef6670

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,54 @@ impl Deprecation {
130130
}
131131
}
132132

133-
/// Represent parsed, *built in*, inert attributes.
133+
/// Represents parsed *built-in* inert attributes.
134134
///
135-
/// That means attributes that are not actually ever expanded.
136-
/// For more information on this, see the module docs on the [`rustc_attr_parsing`] crate.
137-
/// They're instead used as markers, to guide the compilation process in various way in most every stage of the compiler.
138-
/// These are kept around after the AST, into the HIR and further on.
135+
/// ## Overview
136+
/// These attributes are markers that guide the compilation process and are never expanded into other code.
137+
/// They persist throughout the compilation phases, from AST to HIR and beyond.
139138
///
140-
/// The word "parsed" could be a little misleading here, because the parser already parses
141-
/// attributes early on. However, the result, an [`ast::Attribute`]
142-
/// is only parsed at a high level, still containing a token stream in many cases. That is
143-
/// because the structure of the contents varies from attribute to attribute.
144-
/// With a parsed attribute I mean that each attribute is processed individually into a
145-
/// final structure, which on-site (the place where the attribute is useful for, think the
146-
/// the place where `must_use` is checked) little to no extra parsing or validating needs to
147-
/// happen.
139+
/// ## Attribute Processing
140+
/// While attributes are initially parsed by [`rustc_parse`] into [`ast::Attribute`], they still contain raw token streams
141+
/// because different attributes have different internal structures. This enum represents the final,
142+
/// fully parsed form of these attributes, where each variant contains contains all the information and
143+
/// structure relevant for the specific attribute.
148144
///
149-
/// For more docs, look in [`rustc_attr_parsing`].
145+
/// Some attributes can be applied multiple times to the same item, and they are "collapsed" into a single
146+
/// semantic attribute. For example:
147+
/// ```rust
148+
/// #[repr(C)]
149+
/// #[repr(packed)]
150+
/// struct S { }
151+
/// ```
152+
/// This is equivalent to `#[repr(C, packed)]` and results in a single [`AttributeKind::Repr`] containing
153+
/// both `C` and `packed` annotations. This collapsing happens during parsing and is reflected in the
154+
/// data structures defined in this enum.
150155
///
156+
/// ## Usage
157+
/// These parsed attributes are used throughout the compiler to:
158+
/// - Control code generation (e.g., `#[repr]`)
159+
/// - Mark API stability (`#[stable]`, `#[unstable]`)
160+
/// - Provide documentation (`#[doc]`)
161+
/// - Guide compiler behavior (e.g., `#[allow_internal_unstable]`)
162+
///
163+
/// ## Note on Attribute Organization
164+
/// Some attributes like `InlineAttr`, `OptimizeAttr`, and `InstructionSetAttr` are defined separately
165+
/// from this enum because they are used in specific compiler phases (like code generation) and don't
166+
/// need to persist throughout the entire compilation process. They are typically processed and
167+
/// converted into their final form earlier in the compilation pipeline.
168+
///
169+
/// For example:
170+
/// - `InlineAttr` is used during code generation to control function inlining
171+
/// - `OptimizeAttr` is used to control optimization levels
172+
/// - `InstructionSetAttr` is used for target-specific code generation
173+
///
174+
/// These attributes are handled by their respective compiler passes in the [`rustc_codegen_ssa`] crate
175+
/// and don't need to be preserved in the same way as the attributes in this enum.
176+
///
177+
/// For more details on attribute parsing, see the [`rustc_attr_parsing`] crate.
178+
///
179+
/// [`rustc_parse`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/index.html
180+
/// [`rustc_codegen_ssa`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/index.html
151181
/// [`rustc_attr_parsing`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html
152182
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
153183
pub enum AttributeKind {

compiler/rustc_attr_data_structures/src/lib.rs

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

0 commit comments

Comments
 (0)