Skip to content

Commit 800ba8f

Browse files
committed
expand: Refactor InvocationCollector visitor for better code reuse
1 parent 4fd2335 commit 800ba8f

File tree

4 files changed

+724
-500
lines changed

4 files changed

+724
-500
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

0 commit comments

Comments
 (0)