Auto merge of #142706 - fee1-dead-contrib:push-zsznlqyrzsqo, r=oli-obk

completely deduplicate `Visitor` and `MutVisitor`

r? oli-obk

This closes rust-lang/rust#127615.

### Discussion

> * Give every `MutVisitor::visit_*` method a corresponding `flat_map_*` method.

Not every AST node exists in a location where they can be mapped to multiple instances of themselves. Not every AST node exists in a location where they can be removed from existence (e.g. `filter_map_expr`). I don't think this is doable.

> * Give every `MutVisitor::visit_*` method a corresponding `Visitor` method and vice versa

The only three remaining method-level asymmetries after this PR are `visit_stmt` and `visit_nested_use_tree` (only on `Visitor`) and `visit_span` (only on `MutVisitor`).

`visit_stmt` doesn't seem applicable to `MutVisitor` because `walk_flat_map_stmt_kind` will ask `flat_map_item` / `filter_map_expr` to potentially turn a single `Stmt` to multiple based on what a visitor wants. So only using `flat_map_stmt` seems appropriate.

`visit_nested_use_tree` is used for `rustc_resolve` to track stuff. Not useful for `MutVisitor` for now.

`visit_span` is currently not used for `MutVisitor` already, it was just kept in case we want to revive rust-lang/rust#127241. cc `@cjgillot` maybe we could remove for now and re-insert later if we find a use-case? It does involve some extra effort to maintain.

* Remaining FIXMEs

`visit_lifetime` has an extra param for `Visitor` that's not in `MutVisitor`. This is again something only used by `rustc_resolve`. I think we can keep that symmetry for now.
This commit is contained in:
bors
2025-06-22 14:03:44 +00:00
10 changed files with 468 additions and 725 deletions

View File

@@ -1873,14 +1873,14 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
/// It can be removed once that feature is stabilized.
struct MethodReceiverTag;
impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
type OutputTy = Self;
impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
type OutputTy = AstNodeWrapper<P<ast::Expr>, MethodReceiverTag>;
const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
fn descr() -> &'static str {
"an expression"
}
fn to_annotatable(self) -> Annotatable {
Annotatable::Expr(self.wrapped)
Annotatable::Expr(P(self.wrapped))
}
fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
AstNodeWrapper::new(fragment.make_method_receiver_expr(), MethodReceiverTag)
@@ -1983,9 +1983,9 @@ impl DummyAstNode for ast::Expr {
}
}
impl DummyAstNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
impl DummyAstNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
fn dummy() -> Self {
AstNodeWrapper::new(P(ast::Expr::dummy()), MethodReceiverTag)
AstNodeWrapper::new(ast::Expr::dummy(), MethodReceiverTag)
}
}
@@ -2431,7 +2431,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
self.visit_node(node)
}
fn visit_method_receiver_expr(&mut self, node: &mut P<ast::Expr>) {
fn visit_method_receiver_expr(&mut self, node: &mut ast::Expr) {
self.visit_node(AstNodeWrapper::from_mut(node, MethodReceiverTag))
}