rustc_attr_parsing/
lib.rs

1//! Centralized logic for parsing and attributes.
2//!
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
8//!
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.
14//!
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)).
19//!
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
36//!
37//! ```text
38//!                      Active                 Inert
39//!              ┌──────────────────────┬──────────────────────┐
40//!              │     (mostly in)      │    these are parsed  │
41//!              │ rustc_builtin_macros │        here!         │
42//!              │                      │                      │
43//!              │    #[derive(...)]    │    #[stable()]       │
44//!     Built-in │    #[cfg()]          │    #[inline()]       │
45//!              │    #[cfg_attr()]     │    #[repr()]         │
46//!              │                      │                      │
47//!              ├──────────────────────┼──────────────────────┤
48//!              │                      │                      │
49//!              │                      │       `b` in         │
50//!              │                      │ #[proc_macro_derive( │
51//! User created │ #[proc_macro_attr()] │    a,                │
52//!              │                      │    attributes(b)     │
53//!              │                      │ ]                    │
54//!              └──────────────────────┴──────────────────────┘
55//! ```
56//!
57//! ## How This Crate Works
58//! In this crate, syntactical attributes (sequences of tokens that look like
59//! `#[something(something else)]`) are parsed into more semantic attributes, markers on items.
60//! Multiple syntactic attributes might influence a single semantic attribute. For example,
61//! `#[stable(...)]` and `#[unstable()]` cannot occur together, and both semantically define
62//! a "stability" of an item. So, the stability attribute has an
63//! [`AttributeParser`](attributes::AttributeParser) that recognizes both the `#[stable()]`
64//! and `#[unstable()]` syntactic attributes, and at the end produce a single
65//! [`AttributeKind::Stability`](rustc_attr_data_structures::AttributeKind::Stability).
66//!
67//! When multiple instances of the same attribute are allowed, they're combined into a single
68//! semantic attribute. For example:
69//!
70//! ```rust
71//! #[repr(C)]
72//! #[repr(packed)]
73//! struct Meow {}
74//! ```
75//!
76//! This is equivalent to `#[repr(C, packed)]` and results in a single `AttributeKind::Repr`
77//! containing both `C` and `packed` annotations.
78
79// tidy-alphabetical-start
80#![allow(internal_features)]
81#![doc(rust_logo)]
82#![feature(rustdoc_internals)]
83#![recursion_limit = "256"]
84// tidy-alphabetical-end
85
86#[macro_use]
87mod attributes;
88pub(crate) mod context;
89mod lints;
90pub mod parser;
91mod session_diagnostics;
92
93pub use attributes::cfg::*;
94pub use attributes::util::{
95    find_crate_name, is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version,
96};
97pub use context::{AttributeParser, Early, Late, OmitDoc};
98pub use lints::emit_attribute_lint;
99
100rustc_fluent_macro::fluent_messages! { "../messages.ftl" }