diff options
| author | bors <bors@rust-lang.org> | 2025-07-23 04:24:50 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-23 04:24:50 +0000 |
| commit | 20aa182235d6b27ecee519f1d18ee30f0d1c4a61 (patch) | |
| tree | b7ff14e7715b6cf5e274e44c73e5b6c7cbab66c1 /compiler/rustc_ast/src/format.rs | |
| parent | a7a1618e6c835f1f00940ad72203d05808209a0d (diff) | |
| parent | 3c81faec23161d3f997666e90e6e34747ace6d28 (diff) | |
| download | rust-20aa182235d6b27ecee519f1d18ee30f0d1c4a61.tar.gz rust-20aa182235d6b27ecee519f1d18ee30f0d1c4a61.zip | |
Auto merge of #143897 - cjgillot:derive-walk, r=petrochenkov
Implement AST visitors using a derive macro. AST visitors are large and error-prone beasts. This PR attempts to write them using a derive macro. The design uses three traits: `Visitor`, `Visitable`, `Walkable`. - `Visitor` is the trait implemented by downstream crates, it lists `visit_stuff` methods, which call `Walkable::walk_ref` by default; - `Walkable` is derived using the macro, the generated `walk_ref` method calls `Visitable::visit` on each component; - `Visitable` is implemented by `common_visitor_and_walkers` macro, to call the proper `Visitor::visit_stuff` method if it exists, to call `Walkable::walk_ref` if there is none. I agree this is quite a lot of spaghetti macros. I'm open to suggestions on how to reduce the amount of boilerplate code. If this PR is accepted, I believe the same design can be used for the HIR visitor.
Diffstat (limited to 'compiler/rustc_ast/src/format.rs')
| -rw-r--r-- | compiler/rustc_ast/src/format.rs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/compiler/rustc_ast/src/format.rs b/compiler/rustc_ast/src/format.rs index 28d260419c5..c2a1de60a98 100644 --- a/compiler/rustc_ast/src/format.rs +++ b/compiler/rustc_ast/src/format.rs @@ -1,5 +1,5 @@ use rustc_data_structures::fx::FxHashMap; -use rustc_macros::{Decodable, Encodable}; +use rustc_macros::{Decodable, Encodable, Walkable}; use rustc_span::{Ident, Span, Symbol}; use crate::Expr; @@ -41,7 +41,7 @@ use crate::token::LitKind; /// Basically the "AST" for a complete `format_args!()`. /// /// E.g., `format_args!("hello {name}");`. -#[derive(Clone, Encodable, Decodable, Debug)] +#[derive(Clone, Encodable, Decodable, Debug, Walkable)] pub struct FormatArgs { pub span: Span, pub template: Vec<FormatArgsPiece>, @@ -63,7 +63,7 @@ pub struct FormatArgs { /// A piece of a format template string. /// /// E.g. "hello" or "{name}". -#[derive(Clone, Encodable, Decodable, Debug)] +#[derive(Clone, Encodable, Decodable, Debug, Walkable)] pub enum FormatArgsPiece { Literal(Symbol), Placeholder(FormatPlaceholder), @@ -73,7 +73,7 @@ pub enum FormatArgsPiece { /// /// E.g. `1, 2, name="ferris", n=3`, /// but also implicit captured arguments like `x` in `format_args!("{x}")`. -#[derive(Clone, Encodable, Decodable, Debug)] +#[derive(Clone, Encodable, Decodable, Debug, Walkable)] pub struct FormatArguments { arguments: Vec<FormatArgument>, num_unnamed_args: usize, @@ -144,13 +144,13 @@ impl FormatArguments { } } -#[derive(Clone, Encodable, Decodable, Debug)] +#[derive(Clone, Encodable, Decodable, Debug, Walkable)] pub struct FormatArgument { pub kind: FormatArgumentKind, pub expr: P<Expr>, } -#[derive(Clone, Encodable, Decodable, Debug)] +#[derive(Clone, Encodable, Decodable, Debug, Walkable)] pub enum FormatArgumentKind { /// `format_args(…, arg)` Normal, @@ -170,24 +170,28 @@ impl FormatArgumentKind { } } -#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)] +#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, Walkable)] pub struct FormatPlaceholder { /// Index into [`FormatArgs::arguments`]. pub argument: FormatArgPosition, /// The span inside the format string for the full `{…}` placeholder. pub span: Option<Span>, /// `{}`, `{:?}`, or `{:x}`, etc. + #[visitable(ignore)] pub format_trait: FormatTrait, /// `{}` or `{:.5}` or `{:-^20}`, etc. + #[visitable(ignore)] pub format_options: FormatOptions, } -#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)] +#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, Walkable)] pub struct FormatArgPosition { /// Which argument this position refers to (Ok), /// or would've referred to if it existed (Err). + #[visitable(ignore)] pub index: Result<usize, usize>, /// What kind of position this is. See [`FormatArgPositionKind`]. + #[visitable(ignore)] pub kind: FormatArgPositionKind, /// The span of the name or number. pub span: Option<Span>, |
