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 and modules that handle attribute processing.
5//! - [`rustc_hir::attrs`]: Defines the data structures that store parsed attributes
6//! - `rustc_attr_parsing`: This crate, handles the parsing of attributes
7//! - [`rustc_passes::check_attr`] handles attribute validation that cannot be done in this crate
8//!
9//! The separation between data structures and parsing follows the principle of separation of concerns.
10//! Data structures (`rustc_hir::attrs`) 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 produces a single
65//! [`AttributeKind::Stability`](rustc_hir::attrs::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//! ## Early attribute parsing
80//!
81//! While lowering to the HIR, all attributes are parsed using the attribute parsers.
82//! However, sometimes an attributes' parsed form is needed before the HIR is constructed.
83//! This is referred to as "early" attribute parsing,
84//! and is performed using the `parse_limited_*` family of functions on `AttributeParser`.
85//!
86//! [`ast::Attribute`]: rustc_ast::ast::Attribute
87//! [`rustc_passes::check_attr`]: ../rustc_passes/check_attr/index.html
88
89// tidy-alphabetical-start
90#![feature(decl_macro)]
91#![feature(iter_intersperse)]
92#![feature(try_blocks)]
93#![recursion_limit = "256"]
94// tidy-alphabetical-end
95
96#[macro_use]
97mod attributes;
98mod check_cfg;
99mod context;
100mod early_parsed;
101mod errors;
102mod interface;
103pub mod parser;
104mod safety;
105mod session_diagnostics;
106mod stability;
107mod target_checking;
108pub mod validate_attr;
109
110pub use attributes::AttributeSafety;
111pub use attributes::cfg::{
112 CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg, parse_cfg_attr, parse_cfg_entry,
113};
114pub use attributes::cfg_select::*;
115pub use attributes::util::{is_builtin_attr, parse_version};
116pub use context::{OmitDoc, ShouldEmit};
117pub use interface::{AttributeParser, EmitAttribute};
118pub use rustc_parse::parser::Recovery;
119pub use session_diagnostics::ParsedDescription;