Skip to content

Commit 477d572

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 477d572

File tree

5 files changed

+77
-160
lines changed

5 files changed

+77
-160
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_data_structures::tagged_ptr::Tag;
3232
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3333
pub use rustc_span::AttrId;
3434
use rustc_span::source_map::{Spanned, respan};
35-
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
35+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737

3838
pub use crate::format::*;
@@ -1526,6 +1526,19 @@ impl Expr {
15261526
| ExprKind::Struct(_)
15271527
)
15281528
}
1529+
1530+
/// Creates a dummy `P<Expr>`.
1531+
///
1532+
/// Should only be used when it will be replaced afterwards or as a return value when an error was encountered.
1533+
pub fn dummy() -> P<Expr> {
1534+
P(Expr {
1535+
id: DUMMY_NODE_ID,
1536+
kind: ExprKind::Dummy,
1537+
span: DUMMY_SP,
1538+
attrs: ThinVec::new(),
1539+
tokens: None,
1540+
})
1541+
}
15291542
}
15301543

15311544
#[derive(Clone, Encodable, Decodable, Debug)]

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: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ use std::rc::Rc;
33
use std::sync::Arc;
44
use std::{iter, mem};
55

6-
use rustc_ast as ast;
76
use rustc_ast::mut_visit::*;
87
use rustc_ast::ptr::P;
98
use rustc_ast::tokenstream::TokenStream;
109
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
1110
use rustc_ast::{
12-
AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, ExprKind, ForeignItemKind,
13-
HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner, MetaItemKind, ModKind,
14-
NodeId, PatKind, StmtKind, TyKind, token,
11+
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID,
12+
ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner,
13+
MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
1514
};
1615
use rustc_ast_pretty::pprust;
1716
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
@@ -131,13 +130,9 @@ macro_rules! ast_fragments {
131130
pub(crate) fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
132131
match self {
133132
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-
});
133+
if let Some(expr) = opt_expr.take() {
134+
*opt_expr = vis.filter_map_expr(expr)
135+
}
141136
}
142137
AstFragment::MethodReceiverExpr(expr) => vis.visit_method_receiver_expr(expr),
143138
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
@@ -1782,11 +1777,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17821777
/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
17831778
/// It can be removed once that feature is stabilized.
17841779
struct MethodReceiverTag;
1785-
impl DummyAstNode for MethodReceiverTag {
1786-
fn dummy() -> MethodReceiverTag {
1787-
MethodReceiverTag
1788-
}
1789-
}
1780+
17901781
impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
17911782
type OutputTy = Self;
17921783
const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
@@ -2135,42 +2126,39 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21352126
}
21362127
}
21372128

2138-
fn visit_node<Node: InvocationCollectorNode<OutputTy = Node> + DummyAstNode>(
2129+
fn visit_node<Node: InvocationCollectorNode<OutputTy = Node>>(
21392130
&mut self,
2140-
node: &mut Node,
2141-
) {
2131+
mut node: Node,
2132+
) -> Node {
21422133
loop {
2143-
return match self.take_first_attr(node) {
2134+
return match self.take_first_attr(&mut node) {
21442135
Some((attr, pos, derives)) => match attr.name() {
21452136
Some(sym::cfg) => {
21462137
let span = attr.span;
2147-
if self.expand_cfg_true(node, attr, pos).0 {
2138+
if self.expand_cfg_true(&mut node, attr, pos).0 {
21482139
continue;
21492140
}
21502141

21512142
node.expand_cfg_false(self, pos, span);
21522143
continue;
21532144
}
21542145
Some(sym::cfg_attr) => {
2155-
self.expand_cfg_attr(node, &attr, pos);
2146+
self.expand_cfg_attr(&mut node, &attr, pos);
21562147
continue;
21572148
}
2158-
_ => visit_clobber(node, |node| {
2159-
self.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
2160-
.make_ast::<Node>()
2161-
}),
2149+
_ => self
2150+
.collect_attr((attr, pos, derives), node.to_annotatable(), Node::KIND)
2151+
.make_ast::<Node>(),
21622152
},
21632153
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-
})
2154+
let (mac, attrs, _) = node.take_mac_call();
2155+
self.check_attributes(&attrs, &mac);
2156+
self.collect_bang(mac, Node::KIND).make_ast::<Node>()
21702157
}
21712158
None if node.delegation().is_some() => unreachable!(),
21722159
None => {
2173-
assign_id!(self, node.node_id_mut(), || node.walk(self))
2160+
assign_id!(self, node.node_id_mut(), || node.walk(self));
2161+
node
21742162
}
21752163
};
21762164
}
@@ -2273,31 +2261,58 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
22732261
}
22742262

22752263
fn visit_crate(&mut self, node: &mut ast::Crate) {
2276-
self.visit_node(node)
2264+
let krate = mem::replace(
2265+
node,
2266+
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+
);
2274+
*node = self.visit_node(krate);
22772275
}
22782276

22792277
fn visit_ty(&mut self, node: &mut P<ast::Ty>) {
2280-
self.visit_node(node)
2278+
let ty = mem::replace(
2279+
node,
2280+
P(ast::Ty {
2281+
id: DUMMY_NODE_ID,
2282+
kind: TyKind::Dummy,
2283+
span: Default::default(),
2284+
tokens: Default::default(),
2285+
}),
2286+
);
2287+
*node = self.visit_node(ty);
22812288
}
22822289

22832290
fn visit_pat(&mut self, node: &mut P<ast::Pat>) {
2284-
self.visit_node(node)
2291+
let pat = mem::replace(
2292+
node,
2293+
P(ast::Pat {
2294+
id: DUMMY_NODE_ID,
2295+
kind: PatKind::Wild,
2296+
span: Default::default(),
2297+
tokens: Default::default(),
2298+
}),
2299+
);
2300+
*node = self.visit_node(pat)
22852301
}
22862302

22872303
fn visit_expr(&mut self, node: &mut P<ast::Expr>) {
22882304
// FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
22892305
if let Some(attr) = node.attrs.first() {
22902306
self.cfg().maybe_emit_expr_attr_err(attr);
22912307
}
2292-
self.visit_node(node)
2308+
let expr = mem::replace(node, ast::Expr::dummy());
2309+
*node = self.visit_node(expr);
22932310
}
22942311

22952312
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-
})
2313+
let expr = mem::replace(node, ast::Expr::dummy());
2314+
let wrapper = AstNodeWrapper::new(expr, MethodReceiverTag);
2315+
*node = self.visit_node(wrapper).wrapped;
23012316
}
23022317

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

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

Lines changed: 4 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,9 @@ 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+
let expr = std::mem::replace(e, Expr::dummy());
206+
207+
e.kind = ExprKind::Paren(expr);
214208
}
215209
}
216210

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::process::ExitCode;
4242

4343
use parser::parse_expr;
4444
use rustc_ast::ast::{Expr, ExprKind};
45-
use rustc_ast::mut_visit::{self, DummyAstNode as _, MutVisitor};
45+
use rustc_ast::mut_visit::{self, MutVisitor};
4646
use rustc_ast::ptr::P;
4747
use rustc_ast_pretty::pprust;
4848
use rustc_session::parse::ParseSess;
@@ -154,7 +154,7 @@ struct Unparenthesize;
154154
impl MutVisitor for Unparenthesize {
155155
fn visit_expr(&mut self, e: &mut P<Expr>) {
156156
while let ExprKind::Paren(paren) = &mut e.kind {
157-
**e = mem::replace(&mut *paren, Expr::dummy());
157+
*e = mem::replace(paren, Expr::dummy());
158158
}
159159
mut_visit::walk_expr(self, e);
160160
}

0 commit comments

Comments
 (0)