Skip to content

Commit 20c24c3

Browse files
authored
Unrolled build for #142082
Rollup merge of #142082 - xizheyin:rustc_attr_data_structures, r=jdonszelmann Refactor `rustc_attr_data_structures` documentation I was reading through `AttributeKind` and realized that attributes like `InlineAttr` didn't appear in it, however, I found them in `rustc_codegen_ssa` and understood why (guessing). There's almost no overall documentation for this crate, I've added the organized documentation at the top of `lib.rs`, and I've grouped the Attributes into two categories: `AttributeKind` that run all through the compiler, and the ones that are only used in `codegen_ssa`, such as `InlineAttr`, `OptimizeAttr`, `InstructionSetAttr`. Also, I've added documentation for `AttributeKind` that further explains why attributes like `InlineAttr` don't appear in it, with examples for each variant. r? ```@jdonszelmann```
2 parents d9ca9bd + 1ac89f1 commit 20c24c3

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Data structures for representing parsed attributes in the Rust compiler.
2+
//! For detailed documentation about attribute processing,
3+
//! see [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html).
4+
15
// tidy-alphabetical-start
26
#![allow(internal_features)]
37
#![doc(rust_logo)]

0 commit comments

Comments
 (0)