Skip to content

Commit

Permalink
Visit Stmt span in MutVisitor::flat_map_stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 3, 2024
1 parent a2eca35 commit a3cfe2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
13 changes: 7 additions & 6 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,20 +1789,21 @@ pub fn noop_filter_map_expr<T: MutVisitor>(vis: &mut T, mut e: P<Expr>) -> Optio

pub fn walk_flat_map_stmt<T: MutVisitor>(
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
}

Expand Down
9 changes: 1 addition & 8 deletions tests/ui-fulldeps/pprust-parenthesis-insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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<P<Expr>> {
Expand Down

0 comments on commit a3cfe2f

Please sign in to comment.