Skip to content

Commit 219f808

Browse files
committed
remove visit_clobber and DummyAstNode
used for one specific niche purpose, turns out we can just remove them and inline dummy values into callsites. this cleans them up.
1 parent 52bf0cf commit 219f808

File tree

3 files changed

+66
-153
lines changed

3 files changed

+66
-153
lines changed

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,6 @@ pub trait MutVisitor: Sized {
390390

391391
super::common_visitor_and_walkers!((mut) MutVisitor);
392392

393-
/// Use a map-style function (`FnOnce(T) -> T`) to overwrite a `&mut T`. Useful
394-
/// when using a `flat_map_*` or `filter_map_*` method within a `visit_`
395-
/// method.
396-
//
397-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
398-
pub fn visit_clobber<T: DummyAstNode>(t: &mut T, f: impl FnOnce(T) -> T) {
399-
let old_t = std::mem::replace(t, T::dummy());
400-
*t = f(old_t);
401-
}
402-
403393
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
404394
#[inline]
405395
fn visit_vec<T, F>(elems: &mut Vec<T>, mut visit_elem: F)
@@ -1798,101 +1788,6 @@ fn walk_define_opaques<T: MutVisitor>(
17981788
}
17991789
}
18001790

1801-
/// Some value for the AST node that is valid but possibly meaningless. Similar
1802-
/// to `Default` but not intended for wide use. The value will never be used
1803-
/// meaningfully, it exists just to support unwinding in `visit_clobber` in the
1804-
/// case where its closure panics.
1805-
pub trait DummyAstNode {
1806-
fn dummy() -> Self;
1807-
}
1808-
1809-
impl<T> DummyAstNode for Option<T> {
1810-
fn dummy() -> Self {
1811-
Default::default()
1812-
}
1813-
}
1814-
1815-
impl<T: DummyAstNode + 'static> DummyAstNode for P<T> {
1816-
fn dummy() -> Self {
1817-
P(DummyAstNode::dummy())
1818-
}
1819-
}
1820-
1821-
impl DummyAstNode for Item {
1822-
fn dummy() -> Self {
1823-
Item {
1824-
attrs: Default::default(),
1825-
id: DUMMY_NODE_ID,
1826-
span: Default::default(),
1827-
vis: Visibility {
1828-
kind: VisibilityKind::Public,
1829-
span: Default::default(),
1830-
tokens: Default::default(),
1831-
},
1832-
kind: ItemKind::ExternCrate(None, Ident::dummy()),
1833-
tokens: Default::default(),
1834-
}
1835-
}
1836-
}
1837-
1838-
impl DummyAstNode for Expr {
1839-
fn dummy() -> Self {
1840-
Expr {
1841-
id: DUMMY_NODE_ID,
1842-
kind: ExprKind::Dummy,
1843-
span: Default::default(),
1844-
attrs: Default::default(),
1845-
tokens: Default::default(),
1846-
}
1847-
}
1848-
}
1849-
1850-
impl DummyAstNode for Ty {
1851-
fn dummy() -> Self {
1852-
Ty {
1853-
id: DUMMY_NODE_ID,
1854-
kind: TyKind::Dummy,
1855-
span: Default::default(),
1856-
tokens: Default::default(),
1857-
}
1858-
}
1859-
}
1860-
1861-
impl DummyAstNode for Pat {
1862-
fn dummy() -> Self {
1863-
Pat {
1864-
id: DUMMY_NODE_ID,
1865-
kind: PatKind::Wild,
1866-
span: Default::default(),
1867-
tokens: Default::default(),
1868-
}
1869-
}
1870-
}
1871-
1872-
impl DummyAstNode for Stmt {
1873-
fn dummy() -> Self {
1874-
Stmt { id: DUMMY_NODE_ID, kind: StmtKind::Empty, span: Default::default() }
1875-
}
1876-
}
1877-
1878-
impl DummyAstNode for Crate {
1879-
fn dummy() -> Self {
1880-
Crate {
1881-
attrs: Default::default(),
1882-
items: Default::default(),
1883-
spans: Default::default(),
1884-
id: DUMMY_NODE_ID,
1885-
is_placeholder: Default::default(),
1886-
}
1887-
}
1888-
}
1889-
1890-
impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNodeWrapper<N, T> {
1891-
fn dummy() -> Self {
1892-
crate::ast_traits::AstNodeWrapper::new(N::dummy(), T::dummy())
1893-
}
1894-
}
1895-
18961791
#[derive(Debug)]
18971792
pub enum FnKind<'a> {
18981793
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.

compiler/rustc_expand/src/expand.rs

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::rc::Rc;
33
use std::sync::Arc;
44
use std::{iter, mem};
55

6-
use rustc_ast as ast;
6+
use rustc_ast::{self as ast, DUMMY_NODE_ID};
77
use rustc_ast::mut_visit::*;
88
use rustc_ast::ptr::P;
99
use rustc_ast::tokenstream::TokenStream;
@@ -131,13 +131,9 @@ macro_rules! ast_fragments {
131131
pub(crate) fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
132132
match self {
133133
AstFragment::OptExpr(opt_expr) => {
134-
visit_clobber(opt_expr, |opt_expr| {
135-
if let Some(expr) = opt_expr {
136-
vis.filter_map_expr(expr)
137-
} else {
138-
None
139-
}
140-
});
134+
if let Some(expr) = opt_expr.take() {
135+
*opt_expr = vis.filter_map_expr(expr)
136+
}
141137
}
142138
AstFragment::MethodReceiverExpr(expr) => vis.visit_method_receiver_expr(expr),
143139
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
@@ -1782,11 +1778,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17821778
/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
17831779
/// It can be removed once that feature is stabilized.
17841780
struct MethodReceiverTag;
1785-
impl DummyAstNode for MethodReceiverTag {
1786-
fn dummy() -> MethodReceiverTag {
1787-
MethodReceiverTag
1788-
}
1789-
}
1781+
17901782
impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
17911783
type OutputTy = Self;
17921784
const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
@@ -2135,42 +2127,40 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21352127
}
21362128
}
21372129

2138-
fn visit_node<Node: InvocationCollectorNode<OutputTy = Node> + DummyAstNode>(
2130+
fn visit_node<Node: InvocationCollectorNode<OutputTy = Node>>(
21392131
&mut self,
2140-
node: &mut Node,
2141-
) {
2132+
mut node: Node,
2133+
) -> Node {
21422134
loop {
2143-
return match self.take_first_attr(node) {
2135+
return match self.take_first_attr(&mut node) {
21442136
Some((attr, pos, derives)) => match attr.name() {
21452137
Some(sym::cfg) => {
21462138
let span = attr.span;
2147-
if self.expand_cfg_true(node, attr, pos).0 {
2139+
if self.expand_cfg_true(&mut node, attr, pos).0 {
21482140
continue;
21492141
}
21502142

21512143
node.expand_cfg_false(self, pos, span);
21522144
continue;
21532145
}
21542146
Some(sym::cfg_attr) => {
2155-
self.expand_cfg_attr(node, &attr, pos);
2147+
self.expand_cfg_attr(&mut node, &attr, pos);
21562148
continue;
21572149
}
2158-
_ => visit_clobber(node, |node| {
2150+
_ => {
21592151
self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
2160-
.make_ast::<Node>()
2161-
}),
2152+
.make_ast::<Node>()
2153+
}
21622154
},
21632155
None if node.is_mac_call() => {
2164-
visit_clobber(node, |node| {
2165-
// Do not clobber unless it's actually a macro (uncommon case).
2166-
let (mac, attrs, _) = node.take_mac_call();
2167-
self.check_attributes(&attrs, &mac);
2168-
self.collect_bang(mac, Node::KIND).make_ast::<Node>()
2169-
})
2156+
let (mac, attrs, _) = node.take_mac_call();
2157+
self.check_attributes(&attrs, &mac);
2158+
self.collect_bang(mac, Node::KIND).make_ast::<Node>()
21702159
}
21712160
None if node.delegation().is_some() => unreachable!(),
21722161
None => {
2173-
assign_id!(self, node.node_id_mut(), || node.walk(self))
2162+
assign_id!(self, node.node_id_mut(), || node.walk(self));
2163+
node
21742164
}
21752165
};
21762166
}
@@ -2273,31 +2263,61 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
22732263
}
22742264

22752265
fn visit_crate(&mut self, node: &mut ast::Crate) {
2276-
self.visit_node(node)
2266+
let krate = mem::replace(node, ast::Crate {
2267+
attrs: Default::default(),
2268+
items: Default::default(),
2269+
spans: Default::default(),
2270+
id: DUMMY_NODE_ID,
2271+
is_placeholder: Default::default(),
2272+
});
2273+
*node = self.visit_node(krate);
22772274
}
22782275

22792276
fn visit_ty(&mut self, node: &mut P<ast::Ty>) {
2280-
self.visit_node(node)
2277+
let ty = mem::replace(node, P(ast::Ty {
2278+
id: DUMMY_NODE_ID,
2279+
kind: TyKind::Dummy,
2280+
span: Default::default(),
2281+
tokens: Default::default(),
2282+
}));
2283+
*node = self.visit_node(ty);
22812284
}
22822285

22832286
fn visit_pat(&mut self, node: &mut P<ast::Pat>) {
2284-
self.visit_node(node)
2287+
let pat = mem::replace(node, P(ast::Pat {
2288+
id: DUMMY_NODE_ID,
2289+
kind: PatKind::Wild,
2290+
span: Default::default(),
2291+
tokens: Default::default(),
2292+
}));
2293+
*node = self.visit_node(pat)
22852294
}
22862295

22872296
fn visit_expr(&mut self, node: &mut P<ast::Expr>) {
22882297
// FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
22892298
if let Some(attr) = node.attrs.first() {
22902299
self.cfg().maybe_emit_expr_attr_err(attr);
22912300
}
2292-
self.visit_node(node)
2301+
let expr = mem::replace(node, P(ast::Expr {
2302+
id: DUMMY_NODE_ID,
2303+
kind: ExprKind::Dummy,
2304+
span: Default::default(),
2305+
attrs: Default::default(),
2306+
tokens: Default::default(),
2307+
}));
2308+
*node = self.visit_node(expr);
22932309
}
22942310

22952311
fn visit_method_receiver_expr(&mut self, node: &mut P<ast::Expr>) {
2296-
visit_clobber(node, |node| {
2297-
let mut wrapper = AstNodeWrapper::new(node, MethodReceiverTag);
2298-
self.visit_node(&mut wrapper);
2299-
wrapper.wrapped
2300-
})
2312+
let expr = mem::replace(node, P(ast::Expr {
2313+
id: DUMMY_NODE_ID,
2314+
kind: ExprKind::Dummy,
2315+
span: Default::default(),
2316+
attrs: Default::default(),
2317+
tokens: Default::default(),
2318+
}));
2319+
let wrapper = AstNodeWrapper::new(expr, MethodReceiverTag);
2320+
*node = self.visit_node(wrapper).wrapped;
23012321
}
23022322

23032323
fn filter_map_expr(&mut self, node: P<ast::Expr>) -> Option<P<ast::Expr>> {

tests/ui-fulldeps/pprust-expr-roundtrip.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern crate thin_vec;
3434
extern crate rustc_driver;
3535

3636
use parser::parse_expr;
37-
use rustc_ast::mut_visit::{visit_clobber, MutVisitor};
37+
use rustc_ast::mut_visit::MutVisitor;
3838
use rustc_ast::ptr::P;
3939
use rustc_ast::*;
4040
use rustc_ast_pretty::pprust;
@@ -202,15 +202,13 @@ struct AddParens;
202202
impl MutVisitor for AddParens {
203203
fn visit_expr(&mut self, e: &mut P<Expr>) {
204204
mut_visit::walk_expr(self, e);
205-
visit_clobber(e, |e| {
206-
P(Expr {
207-
id: DUMMY_NODE_ID,
208-
kind: ExprKind::Paren(e),
209-
span: DUMMY_SP,
210-
attrs: AttrVec::new(),
211-
tokens: None,
212-
})
213-
});
205+
*e = P(Expr {
206+
id: DUMMY_NODE_ID,
207+
kind: ExprKind::Paren(e),
208+
span: DUMMY_SP,
209+
attrs: AttrVec::new(),
210+
tokens: None,
211+
})
214212
}
215213
}
216214

0 commit comments

Comments
 (0)