From a2eca35c15a1d594dea89543baa38499249f50ee Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 2 Dec 2024 18:36:29 -0800 Subject: [PATCH 1/2] Visit BinOp span in MutVisitor::visit_expr --- compiler/rustc_ast/src/mut_visit.rs | 3 ++- tests/ui-fulldeps/pprust-parenthesis-insertion.rs | 7 ------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 0aceed45028..1e02dc8fb24 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1628,9 +1628,10 @@ pub fn walk_expr(vis: &mut T, Expr { kind, id, span, attrs, token visit_thin_exprs(vis, call_args); vis.visit_span(span); } - ExprKind::Binary(_binop, lhs, rhs) => { + ExprKind::Binary(binop, lhs, rhs) => { vis.visit_expr(lhs); vis.visit_expr(rhs); + vis.visit_span(&mut binop.span); } ExprKind::Unary(_unop, ohs) => vis.visit_expr(ohs), ExprKind::Cast(expr, ty) => { diff --git a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs index fd6644d73c1..75caea057ed 100644 --- a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs +++ b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs @@ -165,13 +165,6 @@ impl MutVisitor for Normalize { *span = DUMMY_SP; } - fn visit_expr(&mut self, expr: &mut P) { - if let ExprKind::Binary(binop, _left, _right) = &mut expr.kind { - self.visit_span(&mut binop.span); - } - mut_visit::walk_expr(self, expr); - } - fn flat_map_stmt(&mut self, mut stmt: Stmt) -> SmallVec<[Stmt; 1]> { self.visit_span(&mut stmt.span); mut_visit::walk_flat_map_stmt(self, stmt) From a3cfe2fd083408b53fa02a90af718b49d2bbe83a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 2 Dec 2024 18:38:45 -0800 Subject: [PATCH 2/2] Visit Stmt span in MutVisitor::flat_map_stmt --- compiler/rustc_ast/src/mut_visit.rs | 13 +++++++------ tests/ui-fulldeps/pprust-parenthesis-insertion.rs | 9 +-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 1e02dc8fb24..0c2fe61a698 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1789,20 +1789,21 @@ pub fn noop_filter_map_expr(vis: &mut T, mut e: P) -> Optio pub fn walk_flat_map_stmt( vis: &mut T, - Stmt { kind, mut span, mut id }: Stmt, + Stmt { kind, span, mut id }: Stmt, ) -> SmallVec<[Stmt; 1]> { vis.visit_id(&mut id); - let stmts: SmallVec<_> = walk_flat_map_stmt_kind(vis, kind) + let mut stmts: SmallVec<[Stmt; 1]> = walk_flat_map_stmt_kind(vis, kind) .into_iter() .map(|kind| Stmt { id, kind, span }) .collect(); - if stmts.len() > 1 { - panic!( + match stmts.len() { + 0 => {} + 1 => vis.visit_span(&mut stmts[0].span), + 2.. => panic!( "cloning statement `NodeId`s is prohibited by default, \ the visitor should implement custom statement visiting" - ); + ), } - vis.visit_span(&mut span); stmts } diff --git a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs index 75caea057ed..b83e576076d 100644 --- a/tests/ui-fulldeps/pprust-parenthesis-insertion.rs +++ b/tests/ui-fulldeps/pprust-parenthesis-insertion.rs @@ -36,12 +36,11 @@ extern crate rustc_errors; extern crate rustc_parse; extern crate rustc_session; extern crate rustc_span; -extern crate smallvec; use std::mem; use std::process::ExitCode; -use rustc_ast::ast::{DUMMY_NODE_ID, Expr, ExprKind, Stmt}; +use rustc_ast::ast::{DUMMY_NODE_ID, Expr, ExprKind}; use rustc_ast::mut_visit::{self, DummyAstNode as _, MutVisitor}; use rustc_ast::node_id::NodeId; use rustc_ast::ptr::P; @@ -50,7 +49,6 @@ use rustc_errors::Diag; use rustc_parse::parser::Recovery; use rustc_session::parse::ParseSess; use rustc_span::{DUMMY_SP, FileName, Span}; -use smallvec::SmallVec; // Every parenthesis in the following expressions is re-inserted by the // pretty-printer. @@ -164,11 +162,6 @@ impl MutVisitor for Normalize { fn visit_span(&mut self, span: &mut Span) { *span = DUMMY_SP; } - - fn flat_map_stmt(&mut self, mut stmt: Stmt) -> SmallVec<[Stmt; 1]> { - self.visit_span(&mut stmt.span); - mut_visit::walk_flat_map_stmt(self, stmt) - } } fn parse_expr(psess: &ParseSess, source_code: &str) -> Option> {