Skip to content

Commit b5058b6

Browse files
Rollup merge of #157423 - mejrs:rustc_attr_parsing-docs, r=JonathanBrouwer
Refactor/expand rustc_attr_parsing docs
2 parents 7ff0bb8 + 8832c42 commit b5058b6

5 files changed

Lines changed: 33 additions & 21 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Traits for parsing attributes.
2+
//!
13
//! This module defines traits for attribute parsers, little state machines that recognize and parse
24
//! attributes out of a longer list of attributes. The main trait is called [`AttributeParser`].
35
//! You can find more docs about [`AttributeParser`]s on the trait itself.
@@ -7,9 +9,11 @@
79
//! Specifically, you might not care about managing the state of your [`AttributeParser`]
810
//! state machine yourself. In this case you can choose to implement:
911
//!
10-
//! - [`SingleAttributeParser`](crate::attributes::SingleAttributeParser): makes it easy to implement an attribute which should error if it
12+
//! - [`NoArgsAttributeParser`]: used for implementing an attribute that appears only once and
13+
//! accepts no arguments
14+
//! - [`SingleAttributeParser`]: makes it easy to implement an attribute which should error if it
1115
//! appears more than once in a list of attributes
12-
//! - [`CombineAttributeParser`](crate::attributes::CombineAttributeParser): makes it easy to implement an attribute which should combine the
16+
//! - [`CombineAttributeParser`]: makes it easy to implement an attribute which should combine the
1317
//! contents of attributes, if an attribute appear multiple times in a list
1418
//!
1519
//! Attributes should be added to `crate::context::ATTRIBUTE_PARSERS` to be parsed.

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Context given to attribute parsers when parsing.
12
use std::cell::RefCell;
23
use std::collections::BTreeMap;
34
use std::collections::btree_map::Entry;
@@ -846,6 +847,9 @@ impl ShouldEmit {
846847
}
847848
}
848849

850+
/// The interface for issuing argument parsing related diagnostics.
851+
///
852+
/// It can be obtained through the [`adcx`](AcceptContext::adcx) method on [`AcceptContext`].
849853
pub(crate) struct AttributeDiagnosticContext<'a, 'f, 'sess> {
850854
ctx: &'a mut AcceptContext<'f, 'sess>,
851855
custom_suggestions: Vec<Suggestion>,

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! API for other crates to parse attributes themselves.
12
use std::convert::identity;
23
#[cfg(debug_assertions)]
34
use std::sync::atomic::{AtomicBool, Ordering};

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//!
33
//! ## Architecture
44
//! This crate is part of a series of crates and modules that handle attribute processing.
5-
//! - [rustc_hir::attrs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/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, logic currently handled in `rustc_passes`
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
88
//!
99
//! The separation between data structures and parsing follows the principle of separation of concerns.
1010
//! Data structures (`rustc_hir::attrs`) define what attributes look like after parsing.
@@ -13,7 +13,7 @@
1313
//! the parsing logic, making the codebase more modular and maintainable.
1414
//!
1515
//! ## Background
16-
//! Previously, the compiler had a single attribute definition (`ast::Attribute`) with parsing and
16+
//! Previously, the compiler had a single attribute definition ([`ast::Attribute`]) with parsing and
1717
//! validation scattered throughout the codebase. This was reorganized for better maintainability
1818
//! (see [#131229](https://github.com/rust-lang/rust/issues/131229)).
1919
//!
@@ -61,7 +61,7 @@
6161
//! `#[stable(...)]` and `#[unstable()]` cannot occur together, and both semantically define
6262
//! a "stability" of an item. So, the stability attribute has an
6363
//! [`AttributeParser`](attributes::AttributeParser) that recognizes both the `#[stable()]`
64-
//! and `#[unstable()]` syntactic attributes, and at the end produce a single
64+
//! and `#[unstable()]` syntactic attributes, and at the end produces a single
6565
//! [`AttributeKind::Stability`](rustc_hir::attrs::AttributeKind::Stability).
6666
//!
6767
//! When multiple instances of the same attribute are allowed, they're combined into a single
@@ -82,6 +82,9 @@
8282
//! However, sometimes an attributes' parsed form is needed before the HIR is constructed.
8383
//! This is referred to as "early" attribute parsing,
8484
//! 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
8588
8689
// tidy-alphabetical-start
8790
#![feature(decl_macro)]
@@ -91,24 +94,13 @@
9194
// tidy-alphabetical-end
9295

9396
#[macro_use]
94-
/// All the individual attribute parsers for each of rustc's built-in attributes.
9597
mod attributes;
96-
97-
/// All the important types given to attribute parsers when parsing
98-
pub(crate) mod context;
99-
100-
/// Code that other crates interact with, to actually parse a list (or sometimes single)
101-
/// attribute.
102-
mod interface;
103-
104-
/// Despite this entire module called attribute parsing and the term being a little overloaded,
105-
/// in this module the code lives that actually breaks up tokenstreams into semantic pieces of attributes,
106-
/// like lists or name-value pairs.
107-
pub mod parser;
108-
10998
mod check_cfg;
99+
mod context;
110100
mod early_parsed;
111101
mod errors;
102+
mod interface;
103+
pub mod parser;
112104
mod safety;
113105
mod session_diagnostics;
114106
mod stability;

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Parsing of attribute arguments.
2+
//!
3+
//! Depending on the attribute parser, an [`ArgParser`] can be used to parse the arguments given to
4+
//! an attribute. See its documentation for more information.
5+
//!
16
//! This is in essence an (improved) duplicate of `rustc_ast/attr/mod.rs`.
27
//! That module is intended to be deleted in its entirety.
38
//!
@@ -89,6 +94,12 @@ impl<P: Borrow<Path>> Display for PathParser<P> {
8994
}
9095
}
9196

97+
/// Used for parsing attribute arguments.
98+
///
99+
/// See also [`AttributeDiagnosticContext`], which is the preferred interface for issuing argument
100+
/// parsing related diagnostics.
101+
///
102+
/// [`AttributeDiagnosticContext`]: crate::context::AttributeDiagnosticContext
92103
#[derive(Debug)]
93104
#[must_use]
94105
pub enum ArgParser {

0 commit comments

Comments
 (0)