1//! Centralized logic for parsing and attributes.2//!3//! ## Architecture4//! 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 attributes6//! - `rustc_attr_parsing`: This crate, handles the parsing of attributes7//! - [`rustc_passes::check_attr`] handles attribute validation that cannot be done in this crate8//!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 needing13//! the parsing logic, making the codebase more modular and maintainable.14//!15//! ## Background16//! Previously, the compiler had a single attribute definition ([`ast::Attribute`]) with parsing and17//! validation scattered throughout the codebase. This was reorganized for better maintainability18//! (see [#131229](https://github.com/rust-lang/rust/issues/131229)).19//!20//! ## Types of Attributes21//! In Rust, attributes are markers that can be attached to items. They come in two main categories.22//!23//! ### 1. Active Attributes24//! 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 implementations27//! - `#[cfg()]`: Expands based on configuration28//! - `#[cfg_attr()]`: Conditional attribute application29//!30//! ### 2. Inert Attributes31//! 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 items34//! - `#[inline()]`: Suggests function inlining35//! - `#[repr()]`: Controls type representation36//!37//! ```text38//! Active Inert39//! ┌──────────────────────┬──────────────────────┐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 Works58//! In this crate, syntactical attributes (sequences of tokens that look like59//! `#[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 define62//! a "stability" of an item. So, the stability attribute has an63//! [`AttributeParser`](attributes::AttributeParser) that recognizes both the `#[stable()]`64//! and `#[unstable()]` syntactic attributes, and at the end produces a single65//! [`AttributeKind::Stability`](rustc_hir::attrs::AttributeKind::Stability).66//!67//! When multiple instances of the same attribute are allowed, they're combined into a single68//! semantic attribute. For example:69//!70//! ```rust71//! #[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 parsing80//!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::Attribute87//! [`rustc_passes::check_attr`]: ../rustc_passes/check_attr/index.html8889// tidy-alphabetical-start90#![feature(decl_macro)]91#![feature(iter_intersperse)]92#![feature(try_blocks)]93#![recursion_limit = "256"]94// tidy-alphabetical-end9596#[macro_use]97mod attributes;98mod check_cfg;99mod context;100mod diagnostics;101mod early_parsed;102mod interface;103pub mod parser;104mod safety;105mod session_diagnostics;106mod stability;107mod target_checking;108mod template;109pub mod validate_attr;110111pub use attributes::AttributeSafety;112pub use attributes::cfg::{113 CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg, parse_cfg_attr, parse_cfg_entry,114};115pub use attributes::cfg_select::*;116pub use attributes::util::{is_builtin_attr, parse_version};117pub use context::{OmitDoc, ShouldEmit};118pub use interface::{AttributeParser, EmitAttribute};119pub use rustc_parse::parser::Recovery;120pub use session_diagnostics::ParsedDescription;121pub use template::{AttrSuggestionStyle, AttributeTemplate};
Findings
✓ No findings reported for this file.