Skip to content

Commit a0eec98

Browse files
committed
Auto merge of #57662 - Zoxc:ast-mut-visitor, r=<try>
Add a MutVisitor for the AST
2 parents 147311c + a9ff07e commit a0eec98

File tree

10 files changed

+1206
-83
lines changed

10 files changed

+1206
-83
lines changed

src/librustc_allocator/expand.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use rustc::middle::allocator::AllocatorKind;
22
use rustc_errors;
3-
use smallvec::SmallVec;
43
use syntax::{
54
ast::{
65
self, Arg, Attribute, Crate, Expr, FnHeader, Generics, Ident, Item, ItemKind,
7-
Mac, Mod, Mutability, Ty, TyKind, Unsafety, VisibilityKind,
6+
Mod, Mutability, Ty, TyKind, Unsafety, VisibilityKind, NodeId,
87
},
98
attr,
109
source_map::{
@@ -16,7 +15,8 @@ use syntax::{
1615
expand::ExpansionConfig,
1716
hygiene::{self, Mark, SyntaxContext},
1817
},
19-
fold::{self, Folder},
18+
fold::Folder,
19+
visit_mut::{self, MutVisitor, Action},
2020
parse::ParseSess,
2121
ptr::P,
2222
symbol::Symbol
@@ -28,7 +28,7 @@ use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
2828
pub fn modify(
2929
sess: &ParseSess,
3030
resolver: &mut dyn Resolver,
31-
krate: Crate,
31+
mut krate: Crate,
3232
crate_name: String,
3333
handler: &rustc_errors::Handler,
3434
) -> ast::Crate {
@@ -39,7 +39,8 @@ pub fn modify(
3939
found: false,
4040
crate_name: Some(crate_name),
4141
in_submod: -1, // -1 to account for the "root" module
42-
}.fold_crate(krate)
42+
}.visit_crate(&mut krate);
43+
krate
4344
}
4445

4546
struct ExpandAllocatorDirectives<'a> {
@@ -54,34 +55,35 @@ struct ExpandAllocatorDirectives<'a> {
5455
in_submod: isize,
5556
}
5657

57-
impl<'a> Folder for ExpandAllocatorDirectives<'a> {
58-
fn fold_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
58+
impl<'a> MutVisitor for ExpandAllocatorDirectives<'a> {
59+
fn visit_item(&mut self, item: &mut P<Item>) -> Action<P<Item>> {
5960
debug!("in submodule {}", self.in_submod);
6061

6162
let name = if attr::contains_name(&item.attrs, "global_allocator") {
6263
"global_allocator"
6364
} else {
64-
return fold::noop_fold_item(item, self);
65+
visit_mut::walk_item(self, item);
66+
return Action::Reuse;
6567
};
6668
match item.node {
6769
ItemKind::Static(..) => {}
6870
_ => {
6971
self.handler
7072
.span_err(item.span, "allocators must be statics");
71-
return smallvec![item];
73+
return Action::Reuse;
7274
}
7375
}
7476

7577
if self.in_submod > 0 {
7678
self.handler
7779
.span_err(item.span, "`global_allocator` cannot be used in submodules");
78-
return smallvec![item];
80+
return Action::Reuse;
7981
}
8082

8183
if self.found {
8284
self.handler
8385
.span_err(item.span, "cannot define more than one #[global_allocator]");
84-
return smallvec![item];
86+
return Action::Reuse;
8587
}
8688
self.found = true;
8789

@@ -142,23 +144,18 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
142144
let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
143145

144146
// Return the item and new submodule
145-
smallvec![item, module]
147+
Action::Add(vec![module])
146148
}
147149

148150
// If we enter a submodule, take note.
149-
fn fold_mod(&mut self, m: Mod) -> Mod {
151+
fn visit_mod(&mut self, m: &mut Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
150152
debug!("enter submodule");
151153
self.in_submod += 1;
152-
let ret = fold::noop_fold_mod(m, self);
154+
let ret = visit_mut::walk_mod(self, m);
153155
self.in_submod -= 1;
154156
debug!("exit submodule");
155157
ret
156158
}
157-
158-
// `fold_mac` is disabled by default. Enable it here.
159-
fn fold_mac(&mut self, mac: Mac) -> Mac {
160-
fold::noop_fold_mac(mac, self)
161-
}
162159
}
163160

164161
struct AllocFnFactory<'a> {

src/librustc_allocator/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ extern crate rustc_errors;
88
extern crate rustc_target;
99
extern crate syntax;
1010
extern crate syntax_pos;
11-
#[macro_use]
1211
extern crate smallvec;
1312

1413
pub mod expand;

src/librustc_data_structures/thin_vec.rs

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ impl<T> ThinVec<T> {
88
pub fn new() -> Self {
99
ThinVec(None)
1010
}
11+
12+
#[inline]
13+
pub fn as_vec(&mut self) -> Option<&mut Vec<T>> {
14+
match *self {
15+
ThinVec(None) => None,
16+
ThinVec(Some(ref mut vec)) => Some(vec),
17+
}
18+
}
1119
}
1220

1321
impl<T> From<Vec<T>> for ThinVec<T> {
@@ -39,6 +47,16 @@ impl<T> ::std::ops::Deref for ThinVec<T> {
3947
}
4048
}
4149

50+
impl<T> ::std::ops::DerefMut for ThinVec<T> {
51+
#[inline]
52+
fn deref_mut(&mut self) -> &mut [T] {
53+
match *self {
54+
ThinVec(None) => &mut [],
55+
ThinVec(Some(ref mut vec)) => vec,
56+
}
57+
}
58+
}
59+
4260
impl<T> Extend<T> for ThinVec<T> {
4361
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
4462
match *self {

src/libsyntax/attr/mod.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,17 @@ impl LitKind {
695695

696696
pub trait HasAttrs: Sized {
697697
fn attrs(&self) -> &[ast::Attribute];
698+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>>;
698699
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
699700
}
700701

701702
impl<T: HasAttrs> HasAttrs for Spanned<T> {
702-
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
703+
fn attrs(&self) -> &[ast::Attribute] {
704+
self.node.attrs()
705+
}
706+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
707+
self.node.attrs_mut()
708+
}
703709
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
704710
respan(self.span, self.node.map_attrs(f))
705711
}
@@ -709,6 +715,9 @@ impl HasAttrs for Vec<Attribute> {
709715
fn attrs(&self) -> &[Attribute] {
710716
self
711717
}
718+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
719+
Some(self)
720+
}
712721
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
713722
f(self)
714723
}
@@ -718,6 +727,9 @@ impl HasAttrs for ThinVec<Attribute> {
718727
fn attrs(&self) -> &[Attribute] {
719728
self
720729
}
730+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
731+
self.as_vec()
732+
}
721733
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
722734
f(self.into()).into()
723735
}
@@ -727,6 +739,9 @@ impl<T: HasAttrs + 'static> HasAttrs for P<T> {
727739
fn attrs(&self) -> &[Attribute] {
728740
(**self).attrs()
729741
}
742+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
743+
(**self).attrs_mut()
744+
}
730745
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
731746
self.map(|t| t.map_attrs(f))
732747
}
@@ -745,6 +760,18 @@ impl HasAttrs for StmtKind {
745760
}
746761
}
747762

763+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
764+
match *self {
765+
StmtKind::Local(ref mut local) => local.attrs_mut(),
766+
StmtKind::Item(..) => None,
767+
StmtKind::Expr(ref mut expr) | StmtKind::Semi(ref mut expr) => expr.attrs_mut(),
768+
StmtKind::Mac(ref mut mac) => {
769+
let (_, _, ref mut attrs) = **mac;
770+
attrs.attrs_mut()
771+
}
772+
}
773+
}
774+
748775
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
749776
match self {
750777
StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
@@ -760,6 +787,9 @@ impl HasAttrs for StmtKind {
760787

761788
impl HasAttrs for Stmt {
762789
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
790+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
791+
self.node.attrs_mut()
792+
}
763793
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
764794
Stmt { id: self.id, node: self.node.map_attrs(f), span: self.span }
765795
}
@@ -770,6 +800,10 @@ impl HasAttrs for GenericParam {
770800
&self.attrs
771801
}
772802

803+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
804+
self.attrs.attrs_mut()
805+
}
806+
773807
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(mut self, f: F) -> Self {
774808
self.attrs = self.attrs.map_attrs(f);
775809
self
@@ -783,6 +817,10 @@ macro_rules! derive_has_attrs {
783817
&self.attrs
784818
}
785819

820+
fn attrs_mut(&mut self) -> Option<&mut Vec<ast::Attribute>> {
821+
self.attrs.attrs_mut()
822+
}
823+
786824
fn map_attrs<F>(mut self, f: F) -> Self
787825
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
788826
{

0 commit comments

Comments
 (0)