2025-02-09 21:49:28 +00:00
//! Centralized logic for parsing and attributes.
2024-12-13 13:47:11 +00:00
//!
2025-06-05 05:20:02 +00:00
//! ## Architecture
//! This crate is part of a series of crates that handle attribute processing.
//! - [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
//! - [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html): This crate, handles the parsing of attributes
//! - (planned) rustc_attr_validation: Will handle attribute validation
2025-02-09 21:49:28 +00:00
//!
2025-06-05 05:20:02 +00:00
//! The separation between data structures and parsing follows the principle of separation of concerns.
//! Data structures (`rustc_attr_data_structures`) define what attributes look like after parsing.
//! This crate (`rustc_attr_parsing`) handles how to convert raw tokens into those structures.
//! This split allows other parts of the compiler to use the data structures without needing
//! the parsing logic, making the codebase more modular and maintainable.
2025-02-09 21:49:28 +00:00
//!
2025-06-05 05:20:02 +00:00
//! ## Background
//! Previously, the compiler had a single attribute definition (`ast::Attribute`) with parsing and
//! validation scattered throughout the codebase. This was reorganized for better maintainability
//! (see [#131229](https://github.com/rust-lang/rust/issues/131229)).
2025-02-09 21:49:31 +00:00
//!
2025-06-05 05:20:02 +00:00
//! ## Types of Attributes
//! In Rust, attributes are markers that can be attached to items. They come in two main categories.
//!
//! ### 1. Active Attributes
//! These are attribute-like proc-macros that expand into other Rust code.
//! They can be either user-defined or compiler-provided. Examples of compiler-provided active attributes:
//! - `#[derive(...)]`: Expands into trait implementations
//! - `#[cfg()]`: Expands based on configuration
//! - `#[cfg_attr()]`: Conditional attribute application
//!
//! ### 2. Inert Attributes
//! These are pure markers that don't expand into other code. They guide the compilation process.
//! They can be user-defined (in proc-macro helpers) or built-in. Examples of built-in inert attributes:
//! - `#[stable()]`: Marks stable API items
//! - `#[inline()]`: Suggests function inlining
//! - `#[repr()]`: Controls type representation
2025-02-09 21:49:31 +00:00
//!
//! ```text
//! Active Inert
//! ┌──────────────────────┬──────────────────────┐
//! │ (mostly in) │ these are parsed │
//! │ rustc_builtin_macros │ here! │
//! │ │ │
//! │ #[derive(...)] │ #[stable()] │
//! Built-in │ #[cfg()] │ #[inline()] │
//! │ #[cfg_attr()] │ #[repr()] │
//! │ │ │
//! ├──────────────────────┼──────────────────────┤
//! │ │ │
//! │ │ `b` in │
//! │ │ #[proc_macro_derive( │
//! User created │ #[proc_macro_attr()] │ a, │
//! │ │ attributes(b) │
//! │ │ ] │
//! └──────────────────────┴──────────────────────┘
//! ```
2025-02-09 21:49:28 +00:00
//!
2025-06-05 05:20:02 +00:00
//! ## How This Crate Works
2025-02-09 21:49:28 +00:00
//! In this crate, syntactical attributes (sequences of tokens that look like
//! `#[something(something else)]`) are parsed into more semantic attributes, markers on items.
//! Multiple syntactic attributes might influence a single semantic attribute. For example,
//! `#[stable(...)]` and `#[unstable()]` cannot occur together, and both semantically define
//! a "stability" of an item. So, the stability attribute has an
//! [`AttributeParser`](attributes::AttributeParser) that recognizes both the `#[stable()]`
2025-05-18 16:14:43 +00:00
//! and `#[unstable()]` syntactic attributes, and at the end produce a single
2025-02-12 12:59:08 +00:00
//! [`AttributeKind::Stability`].
2025-02-09 21:49:28 +00:00
//!
2025-06-05 05:20:02 +00:00
//! When multiple instances of the same attribute are allowed, they're combined into a single
//! semantic attribute. For example:
2025-02-09 21:49:28 +00:00
//!
2025-06-05 05:20:02 +00:00
//! ```rust
2025-02-09 21:49:28 +00:00
//! #[repr(C)]
//! #[repr(packed)]
//! struct Meow {}
//! ```
//!
2025-06-05 05:20:02 +00:00
//! This is equivalent to `#[repr(C, packed)]` and results in a single `AttributeKind::Repr`
//! containing both `C` and `packed` annotations.
2024-12-13 13:47:11 +00:00
// tidy-alphabetical-start
#![ allow(internal_features) ]
#![ doc(rust_logo) ]
#![ feature(rustdoc_internals) ]
2025-04-29 01:57:27 +00:00
#![ recursion_limit = " 256 " ]
2024-12-13 13:47:11 +00:00
// tidy-alphabetical-end
2025-02-09 21:49:33 +00:00
#[ macro_use ]
2024-12-13 13:47:11 +00:00
mod attributes ;
2025-02-09 21:49:31 +00:00
mod context ;
2025-02-12 12:59:08 +00:00
mod lints ;
2025-02-09 21:49:31 +00:00
pub mod parser ;
2024-12-13 13:47:11 +00:00
mod session_diagnostics ;
2025-02-09 21:49:33 +00:00
pub use attributes ::cfg ::* ;
2025-05-12 13:42:35 +00:00
pub use attributes ::util ::{
find_crate_name , is_builtin_attr , is_doc_alias_attrs_contain_symbol , parse_version ,
} ;
2025-02-12 12:59:08 +00:00
pub use context ::{ AttributeParser , Early , Late , OmitDoc } ;
pub use lints ::emit_attribute_lint ;
2024-12-13 13:47:11 +00:00
rustc_fluent_macro ::fluent_messages! { " ../messages.ftl " }