Skip to content

Commit 06f0c62

Browse files
committed
Add a MutVisitor for the AST, and use it for ExpandAllocatorDirectives
1 parent 43b4c4a commit 06f0c62

File tree

6 files changed

+939
-20
lines changed

6 files changed

+939
-20
lines changed

src/librustc_allocator/expand.rs

Lines changed: 16 additions & 19 deletions
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},
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>) -> Option<Vec<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 None;
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 None;
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 None;
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 None;
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+
Some(vec![item.clone(), 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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ impl<T> ::std::ops::Deref for ThinVec<T> {
3939
}
4040
}
4141

42+
impl<T> ::std::ops::DerefMut for ThinVec<T> {
43+
#[inline]
44+
fn deref_mut(&mut self) -> &mut [T] {
45+
match *self {
46+
ThinVec(None) => &mut [],
47+
ThinVec(Some(ref mut vec)) => vec,
48+
}
49+
}
50+
}
51+
4252
impl<T> Extend<T> for ThinVec<T> {
4353
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
4454
match *self {

src/libsyntax/ast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,13 @@ impl VariantData {
21142114
_ => &[],
21152115
}
21162116
}
2117+
pub fn fields_mut(&mut self) -> &mut [StructField] {
2118+
match *self {
2119+
VariantData::Struct(ref mut fields, _) |
2120+
VariantData::Tuple(ref mut fields, _) => fields,
2121+
_ => &mut [],
2122+
}
2123+
}
21172124
pub fn id(&self) -> NodeId {
21182125
match *self {
21192126
VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id,

src/libsyntax/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ pub use syntax_pos::edition;
160160
pub use syntax_pos::symbol;
161161
pub mod test;
162162
pub mod tokenstream;
163+
#[macro_use]
163164
pub mod visit;
165+
pub mod visit_mut;
164166

165167
pub mod print {
166168
pub mod pp;

0 commit comments

Comments
 (0)