Skip to content

Commit 45088fd

Browse files
authored
Rollup merge of #133784 - dtolnay:visitspans, r=compiler-errors
Fix MutVisitor's default implementations to visit Stmt's and BinOp's spans The `Stmt` case is a bug introduced almost certainly unintentionally by #126993. The code _used_ to visit and mutate `span` correctly, but got changed as follows by that PR. Notice how `span` is **copied** into the output by `|kind| Stmt { id, kind, span }` which happens after the mutation in the correct code (red) and before the mutation in the incorrect code (green). ```diff pub fn noop_flat_map_stmt<T: MutVisitor>( Stmt { kind, mut span, mut id }: Stmt, vis: &mut T, ) -> SmallVec<[Stmt; 1]> { vis.visit_id(&mut id); - vis.visit_span(&mut span); let stmts: SmallVec<_> = noop_flat_map_stmt_kind(kind, vis) .into_iter() .map(|kind| Stmt { id, kind, span }) .collect(); if stmts.len() > 1 { panic!(...); } + vis.visit_span(&mut span); stmts } ```
2 parents afffc1a + a3cfe2f commit 45088fd

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

compiler/rustc_ast/src/mut_visit.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1625,9 +1625,10 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
16251625
visit_thin_exprs(vis, call_args);
16261626
vis.visit_span(span);
16271627
}
1628-
ExprKind::Binary(_binop, lhs, rhs) => {
1628+
ExprKind::Binary(binop, lhs, rhs) => {
16291629
vis.visit_expr(lhs);
16301630
vis.visit_expr(rhs);
1631+
vis.visit_span(&mut binop.span);
16311632
}
16321633
ExprKind::Unary(_unop, ohs) => vis.visit_expr(ohs),
16331634
ExprKind::Cast(expr, ty) => {
@@ -1785,20 +1786,21 @@ pub fn noop_filter_map_expr<T: MutVisitor>(vis: &mut T, mut e: P<Expr>) -> Optio
17851786

17861787
pub fn walk_flat_map_stmt<T: MutVisitor>(
17871788
vis: &mut T,
1788-
Stmt { kind, mut span, mut id }: Stmt,
1789+
Stmt { kind, span, mut id }: Stmt,
17891790
) -> SmallVec<[Stmt; 1]> {
17901791
vis.visit_id(&mut id);
1791-
let stmts: SmallVec<_> = walk_flat_map_stmt_kind(vis, kind)
1792+
let mut stmts: SmallVec<[Stmt; 1]> = walk_flat_map_stmt_kind(vis, kind)
17921793
.into_iter()
17931794
.map(|kind| Stmt { id, kind, span })
17941795
.collect();
1795-
if stmts.len() > 1 {
1796-
panic!(
1796+
match stmts.len() {
1797+
0 => {}
1798+
1 => vis.visit_span(&mut stmts[0].span),
1799+
2.. => panic!(
17971800
"cloning statement `NodeId`s is prohibited by default, \
17981801
the visitor should implement custom statement visiting"
1799-
);
1802+
),
18001803
}
1801-
vis.visit_span(&mut span);
18021804
stmts
18031805
}
18041806

tests/ui-fulldeps/pprust-parenthesis-insertion.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ extern crate rustc_errors;
3636
extern crate rustc_parse;
3737
extern crate rustc_session;
3838
extern crate rustc_span;
39-
extern crate smallvec;
4039

4140
use std::mem;
4241
use std::process::ExitCode;
4342

44-
use rustc_ast::ast::{DUMMY_NODE_ID, Expr, ExprKind, Stmt};
43+
use rustc_ast::ast::{DUMMY_NODE_ID, Expr, ExprKind};
4544
use rustc_ast::mut_visit::{self, DummyAstNode as _, MutVisitor};
4645
use rustc_ast::node_id::NodeId;
4746
use rustc_ast::ptr::P;
@@ -50,7 +49,6 @@ use rustc_errors::Diag;
5049
use rustc_parse::parser::Recovery;
5150
use rustc_session::parse::ParseSess;
5251
use rustc_span::{DUMMY_SP, FileName, Span};
53-
use smallvec::SmallVec;
5452

5553
// Every parenthesis in the following expressions is re-inserted by the
5654
// pretty-printer.
@@ -164,18 +162,6 @@ impl MutVisitor for Normalize {
164162
fn visit_span(&mut self, span: &mut Span) {
165163
*span = DUMMY_SP;
166164
}
167-
168-
fn visit_expr(&mut self, expr: &mut P<Expr>) {
169-
if let ExprKind::Binary(binop, _left, _right) = &mut expr.kind {
170-
self.visit_span(&mut binop.span);
171-
}
172-
mut_visit::walk_expr(self, expr);
173-
}
174-
175-
fn flat_map_stmt(&mut self, mut stmt: Stmt) -> SmallVec<[Stmt; 1]> {
176-
self.visit_span(&mut stmt.span);
177-
mut_visit::walk_flat_map_stmt(self, stmt)
178-
}
179165
}
180166

181167
fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> {

0 commit comments

Comments
 (0)