Skip to content

Commit b681dc2

Browse files
authored
Rollup merge of #92573 - petrochenkov:ltrattr3, r=Aaron1011
expand: Refactor InvocationCollector visitor for better code reuse The refactoring part of #92473. Invocation collector visitor logic now lives in two main functions: - `fn flat_map_node`, corresponding to "one to many" expansions - `fn visit_node`, corresponding to "one to one" expansions All specific mut visitor methods now use one of these functions. The new `InvocationCollectorNode` trait implemented for all `AstFragment` nodes provides the necessary small pieces of functionality required to implement the `(flat_map,visit)_node` functions. r? `@Aaron1011`
2 parents 464a081 + 4523466 commit b681dc2

File tree

6 files changed

+761
-540
lines changed

6 files changed

+761
-540
lines changed

compiler/rustc_ast/src/ast_like.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use super::{AssocItem, Expr, ForeignItem, Item, Local, MacCallStmt};
66
use super::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
77
use super::{AttrVec, Attribute, Stmt, StmtKind};
88

9-
use std::fmt::Debug;
9+
use std::fmt;
10+
use std::marker::PhantomData;
1011

1112
/// An `AstLike` represents an AST node (or some wrapper around
1213
/// and AST node) which stores some combination of attributes
1314
/// and tokens.
14-
pub trait AstLike: Sized + Debug {
15+
pub trait AstLike: Sized + fmt::Debug {
1516
/// This is `true` if this `AstLike` might support 'custom' (proc-macro) inner
1617
/// attributes. Attributes like `#![cfg]` and `#![cfg_attr]` are not
1718
/// considered 'custom' attributes
@@ -285,3 +286,37 @@ derive_has_attrs_no_tokens! {
285286
derive_has_tokens_no_attrs! {
286287
Ty, Block, AttrItem, Pat, Path, Visibility
287288
}
289+
290+
/// A newtype around an `AstLike` node that implements `AstLike` itself.
291+
pub struct AstLikeWrapper<Wrapped, Tag> {
292+
pub wrapped: Wrapped,
293+
pub tag: PhantomData<Tag>,
294+
}
295+
296+
impl<Wrapped, Tag> AstLikeWrapper<Wrapped, Tag> {
297+
pub fn new(wrapped: Wrapped, _tag: Tag) -> AstLikeWrapper<Wrapped, Tag> {
298+
AstLikeWrapper { wrapped, tag: Default::default() }
299+
}
300+
}
301+
302+
impl<Wrapped: fmt::Debug, Tag> fmt::Debug for AstLikeWrapper<Wrapped, Tag> {
303+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304+
f.debug_struct("AstLikeWrapper")
305+
.field("wrapped", &self.wrapped)
306+
.field("tag", &self.tag)
307+
.finish()
308+
}
309+
}
310+
311+
impl<Wrapped: AstLike, Tag> AstLike for AstLikeWrapper<Wrapped, Tag> {
312+
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = Wrapped::SUPPORTS_CUSTOM_INNER_ATTRS;
313+
fn attrs(&self) -> &[Attribute] {
314+
self.wrapped.attrs()
315+
}
316+
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
317+
self.wrapped.visit_attrs(f)
318+
}
319+
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
320+
self.wrapped.tokens_mut()
321+
}
322+
}

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub mod tokenstream;
4141
pub mod visit;
4242

4343
pub use self::ast::*;
44-
pub use self::ast_like::AstLike;
44+
pub use self::ast_like::{AstLike, AstLikeWrapper};
4545

4646
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4747

compiler/rustc_expand/src/config.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ macro_rules! configure {
238238
}
239239

240240
impl<'a> StripUnconfigured<'a> {
241-
pub fn configure<T: AstLike>(&mut self, mut node: T) -> Option<T> {
241+
pub fn configure<T: AstLike>(&self, mut node: T) -> Option<T> {
242242
self.process_cfg_attrs(&mut node);
243243
if self.in_cfg(node.attrs()) {
244244
self.try_configure_tokens(&mut node);
@@ -248,7 +248,7 @@ impl<'a> StripUnconfigured<'a> {
248248
}
249249
}
250250

251-
fn try_configure_tokens<T: AstLike>(&mut self, node: &mut T) {
251+
fn try_configure_tokens<T: AstLike>(&self, node: &mut T) {
252252
if self.config_tokens {
253253
if let Some(Some(tokens)) = node.tokens_mut() {
254254
let attr_annotated_tokens = tokens.create_token_stream();
@@ -257,10 +257,7 @@ impl<'a> StripUnconfigured<'a> {
257257
}
258258
}
259259

260-
fn configure_krate_attrs(
261-
&mut self,
262-
mut attrs: Vec<ast::Attribute>,
263-
) -> Option<Vec<ast::Attribute>> {
260+
fn configure_krate_attrs(&self, mut attrs: Vec<ast::Attribute>) -> Option<Vec<ast::Attribute>> {
264261
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
265262
if self.in_cfg(&attrs) { Some(attrs) } else { None }
266263
}
@@ -269,7 +266,7 @@ impl<'a> StripUnconfigured<'a> {
269266
/// This is only used during the invocation of `derive` proc-macros,
270267
/// which require that we cfg-expand their entire input.
271268
/// Normal cfg-expansion operates on parsed AST nodes via the `configure` method
272-
fn configure_tokens(&mut self, stream: &AttrAnnotatedTokenStream) -> AttrAnnotatedTokenStream {
269+
fn configure_tokens(&self, stream: &AttrAnnotatedTokenStream) -> AttrAnnotatedTokenStream {
273270
fn can_skip(stream: &AttrAnnotatedTokenStream) -> bool {
274271
stream.0.iter().all(|(tree, _spacing)| match tree {
275272
AttrAnnotatedTokenTree::Attributes(_) => false,
@@ -325,7 +322,7 @@ impl<'a> StripUnconfigured<'a> {
325322
/// Gives compiler warnings if any `cfg_attr` does not contain any
326323
/// attributes and is in the original source code. Gives compiler errors if
327324
/// the syntax of any `cfg_attr` is incorrect.
328-
fn process_cfg_attrs<T: AstLike>(&mut self, node: &mut T) {
325+
fn process_cfg_attrs<T: AstLike>(&self, node: &mut T) {
329326
node.visit_attrs(|attrs| {
330327
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
331328
});
@@ -338,7 +335,7 @@ impl<'a> StripUnconfigured<'a> {
338335
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
339336
/// is in the original source file. Gives a compiler error if the syntax of
340337
/// the attribute is incorrect.
341-
fn process_cfg_attr(&mut self, attr: Attribute) -> Vec<Attribute> {
338+
fn process_cfg_attr(&self, attr: Attribute) -> Vec<Attribute> {
342339
if !attr.has_name(sym::cfg_attr) {
343340
return vec![attr];
344341
}
@@ -461,7 +458,7 @@ impl<'a> StripUnconfigured<'a> {
461458
}
462459
}
463460

464-
pub fn configure_expr(&mut self, expr: &mut P<ast::Expr>) {
461+
pub fn configure_expr(&self, expr: &mut P<ast::Expr>) {
465462
for attr in expr.attrs.iter() {
466463
self.maybe_emit_expr_attr_err(attr);
467464
}

0 commit comments

Comments
 (0)