Skip to content

Commit 52fdd7d

Browse files
authored
Unrolled build for #142058
Rollup merge of #142058 - xizheyin:rustc-attr-parsing, r=jdonszelmann Clean `rustc_attr_parsing/src/lib.rs` documentation Improves the documentation clarity in `rustc_attr_parsing` by restructuring content with clearer section headers, simplifying explanations of attribute types, making technical descriptions more precise. r? ``@oli-obk``
2 parents d00435f + 1e49ad3 commit 52fdd7d

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) trait AttributeParser: Default + 'static {
7474
pub(crate) trait SingleAttributeParser: 'static {
7575
const PATH: &'static [Symbol];
7676

77-
/// Caled when a duplicate attribute is found.
77+
/// Called when a duplicate attribute is found.
7878
///
7979
/// `first_span` is the span of the first occurrence of this attribute.
8080
// FIXME(jdonszelmann): default error

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
//! Centralized logic for parsing and attributes.
22
//!
3-
//! Part of a series of crates:
4-
//! - rustc_attr_data_structures: contains types that the parsers parse into
5-
//! - rustc_attr_parsing: this crate
6-
//! - (in the future): rustc_attr_validation
3+
//! ## Architecture
4+
//! This crate is part of a series of crates that handle attribute processing.
5+
//! - [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
6+
//! - [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html): This crate, handles the parsing of attributes
7+
//! - (planned) rustc_attr_validation: Will handle attribute validation
78
//!
8-
//! History: Check out [#131229](https://github.com/rust-lang/rust/issues/131229).
9-
//! There used to be only one definition of attributes in the compiler: `ast::Attribute`.
10-
//! These were then parsed or validated or both in places distributed all over the compiler.
11-
//! This was a mess...
9+
//! The separation between data structures and parsing follows the principle of separation of concerns.
10+
//! Data structures (`rustc_attr_data_structures`) define what attributes look like after parsing.
11+
//! This crate (`rustc_attr_parsing`) handles how to convert raw tokens into those structures.
12+
//! This split allows other parts of the compiler to use the data structures without needing
13+
//! the parsing logic, making the codebase more modular and maintainable.
1214
//!
13-
//! Attributes are markers on items.
14-
//! Many of them are actually attribute-like proc-macros, and are expanded to some other rust syntax.
15-
//! This could either be a user provided proc macro, or something compiler provided.
16-
//! `derive` is an example of one that the compiler provides.
17-
//! These are built-in, but they have a valid expansion to Rust tokens and are thus called "active".
18-
//! I personally like calling these *active* compiler-provided attributes, built-in *macros*,
19-
//! because they still expand, and this helps to differentiate them from built-in *attributes*.
20-
//! However, I'll be the first to admit that the naming here can be confusing.
15+
//! ## Background
16+
//! Previously, the compiler had a single attribute definition (`ast::Attribute`) with parsing and
17+
//! validation scattered throughout the codebase. This was reorganized for better maintainability
18+
//! (see [#131229](https://github.com/rust-lang/rust/issues/131229)).
2119
//!
22-
//! The alternative to active attributes, are inert attributes.
23-
//! These can occur in user code (proc-macro helper attributes).
24-
//! But what's important is, many built-in attributes are inert like this.
25-
//! There is nothing they expand to during the macro expansion process,
26-
//! sometimes because they literally cannot expand to something that is valid Rust.
27-
//! They are really just markers to guide the compilation process.
28-
//! An example is `#[inline(...)]` which changes how code for functions is generated.
20+
//! ## Types of Attributes
21+
//! In Rust, attributes are markers that can be attached to items. They come in two main categories.
22+
//!
23+
//! ### 1. Active Attributes
24+
//! These are attribute-like proc-macros that expand into other Rust code.
25+
//! They can be either user-defined or compiler-provided. Examples of compiler-provided active attributes:
26+
//! - `#[derive(...)]`: Expands into trait implementations
27+
//! - `#[cfg()]`: Expands based on configuration
28+
//! - `#[cfg_attr()]`: Conditional attribute application
29+
//!
30+
//! ### 2. Inert Attributes
31+
//! These are pure markers that don't expand into other code. They guide the compilation process.
32+
//! They can be user-defined (in proc-macro helpers) or built-in. Examples of built-in inert attributes:
33+
//! - `#[stable()]`: Marks stable API items
34+
//! - `#[inline()]`: Suggests function inlining
35+
//! - `#[repr()]`: Controls type representation
2936
//!
3037
//! ```text
3138
//! Active Inert
3239
//! ┌──────────────────────┬──────────────────────┐
3340
//! │ (mostly in) │ these are parsed │
3441
//! │ rustc_builtin_macros │ here! │
3542
//! │ │ │
36-
//! │ │ │
3743
//! │ #[derive(...)] │ #[stable()] │
3844
//! Built-in │ #[cfg()] │ #[inline()] │
3945
//! │ #[cfg_attr()] │ #[repr()] │
4046
//! │ │ │
41-
//! │ │ │
42-
//! │ │ │
4347
//! ├──────────────────────┼──────────────────────┤
4448
//! │ │ │
45-
//! │ │ │
4649
//! │ │ `b` in │
4750
//! │ │ #[proc_macro_derive( │
4851
//! User created │ #[proc_macro_attr()] │ a, │
4952
//! │ │ attributes(b) │
5053
//! │ │ ] │
51-
//! │ │ │
52-
//! │ │ │
53-
//! │ │ │
5454
//! └──────────────────────┴──────────────────────┘
5555
//! ```
5656
//!
57+
//! ## How This Crate Works
5758
//! In this crate, syntactical attributes (sequences of tokens that look like
5859
//! `#[something(something else)]`) are parsed into more semantic attributes, markers on items.
5960
//! Multiple syntactic attributes might influence a single semantic attribute. For example,
@@ -63,18 +64,17 @@
6364
//! and `#[unstable()]` syntactic attributes, and at the end produce a single
6465
//! [`AttributeKind::Stability`](rustc_attr_data_structures::AttributeKind::Stability).
6566
//!
66-
//! As a rule of thumb, when a syntactical attribute can be applied more than once, they should be
67-
//! combined into a single semantic attribute. For example:
67+
//! When multiple instances of the same attribute are allowed, they're combined into a single
68+
//! semantic attribute. For example:
6869
//!
69-
//! ```
70+
//! ```rust
7071
//! #[repr(C)]
7172
//! #[repr(packed)]
7273
//! struct Meow {}
7374
//! ```
7475
//!
75-
//! should result in a single `AttributeKind::Repr` containing a list of repr annotations, in this
76-
//! case `C` and `packed`. This is equivalent to writing `#[repr(C, packed)]` in a single
77-
//! syntactical annotation.
76+
//! This is equivalent to `#[repr(C, packed)]` and results in a single `AttributeKind::Repr`
77+
//! containing both `C` and `packed` annotations.
7878
7979
// tidy-alphabetical-start
8080
#![allow(internal_features)]

0 commit comments

Comments
 (0)