Skip to content

Commit efa9baf

Browse files
pcwaltonpnkfelix
authored andcommitted
libsyntax: eagerly-bind references to pass into by-value closures.
1 parent 869d60d commit efa9baf

File tree

11 files changed

+85
-44
lines changed

11 files changed

+85
-44
lines changed

src/libsyntax/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ pub fn lookup(name: &str) -> Option<Abi> {
9999
*/
100100

101101
let mut res = None;
102-
102+
let res_ptr = &mut res;
103103
each_abi(|abi| {
104104
if name == abi.data().name {
105-
res = Some(abi);
105+
*res_ptr = Some(abi);
106106
false
107107
} else {
108108
true
109109
}
110110
});
111-
res
111+
*res_ptr
112112
}
113113

114114
pub fn all_names() -> Vec<&'static str> {

src/libsyntax/ast_util.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,26 +644,27 @@ pub fn is_item_impl(item: @ast::Item) -> bool {
644644
}
645645
}
646646

647-
pub fn walk_pat(pat: &Pat, it: |&Pat| -> bool) -> bool {
647+
pub fn walk_pat(pat: &Pat, mut it: |&Pat| -> bool) -> bool {
648648
if !it(pat) {
649649
return false;
650650
}
651651

652+
let it_ptr = &mut it;
652653
match pat.node {
653-
PatIdent(_, _, Some(p)) => walk_pat(p, it),
654+
PatIdent(_, _, Some(p)) => walk_pat(p, |p| (*it_ptr)(p)),
654655
PatStruct(_, ref fields, _) => {
655-
fields.iter().advance(|f| walk_pat(f.pat, |p| it(p)))
656+
fields.iter().advance(|f| walk_pat(f.pat, |p| (*it_ptr)(p)))
656657
}
657658
PatEnum(_, Some(ref s)) | PatTup(ref s) => {
658-
s.iter().advance(|&p| walk_pat(p, |p| it(p)))
659+
s.iter().advance(|&p| walk_pat(p, |p| (*it_ptr)(p)))
659660
}
660661
PatUniq(s) | PatRegion(s) => {
661-
walk_pat(s, it)
662+
walk_pat(s, |p| (*it_ptr)(p))
662663
}
663664
PatVec(ref before, ref slice, ref after) => {
664-
before.iter().advance(|&p| walk_pat(p, |p| it(p))) &&
665-
slice.iter().advance(|&p| walk_pat(p, |p| it(p))) &&
666-
after.iter().advance(|&p| walk_pat(p, |p| it(p)))
665+
before.iter().advance(|&p| walk_pat(p, |p| (*it_ptr)(p))) &&
666+
slice.iter().advance(|&p| walk_pat(p, |p| (*it_ptr)(p))) &&
667+
after.iter().advance(|&p| walk_pat(p, |p| (*it_ptr)(p)))
667668
}
668669
PatMac(_) => fail!("attempted to analyze unexpanded pattern"),
669670
PatWild | PatWildMulti | PatLit(_) | PatRange(_, _) | PatIdent(_, _, _) |

src/libsyntax/attr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,15 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
349349
(cfg: &[@MetaItem], mut metas: It) -> bool {
350350
// having no #[cfg(...)] attributes counts as matching.
351351
let mut no_cfgs = true;
352+
let no_cfgs_ptr = &mut no_cfgs;
352353

353354
// this would be much nicer as a chain of iterator adaptors, but
354355
// this doesn't work.
355356
let some_cfg_matches = metas.fold(false, |matches, mi| {
356357
debug!("testing name: {}", mi.name());
357358
let this_matches = if mi.check_name("cfg") { // it is a #[cfg()] attribute
358359
debug!("is cfg");
359-
no_cfgs = false;
360+
*no_cfgs_ptr = false;
360361
// only #[cfg(...)] ones are understood.
361362
match mi.meta_item_list() {
362363
Some(cfg_meta) => {
@@ -385,8 +386,10 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
385386
};
386387
matches || this_matches
387388
});
388-
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})", no_cfgs, some_cfg_matches);
389-
no_cfgs || some_cfg_matches
389+
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})",
390+
*no_cfgs_ptr,
391+
some_cfg_matches);
392+
*no_cfgs_ptr || some_cfg_matches
390393
}
391394

392395
/// Represents the #[deprecated="foo"] and friends attributes.

src/libsyntax/ext/deriving/generic/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,11 +690,12 @@ impl<'a> MethodDef<'a> {
690690

691691
// transpose raw_fields
692692
let fields = if raw_fields.len() > 0 {
693-
raw_fields.get(0)
694-
.iter()
695-
.enumerate()
696-
.map(|(i, &(span, opt_id, field))| {
697-
let other_fields = raw_fields.tail().iter().map(|l| {
693+
let raw_fields_ptr = &raw_fields;
694+
raw_fields_ptr.get(0)
695+
.iter()
696+
.enumerate()
697+
.map(|(i, &(span, opt_id, field))| {
698+
let other_fields = raw_fields_ptr.tail().iter().map(|l| {
698699
match l.get(i) {
699700
&(_, _, ex) => ex
700701
}
@@ -1256,27 +1257,30 @@ fields. `use_foldl` controls whether this is done left-to-right
12561257
*/
12571258
#[inline]
12581259
pub fn cs_same_method_fold(use_foldl: bool,
1259-
f: |&mut ExtCtxt, Span, @Expr, @Expr| -> @Expr,
1260+
mut f: |&mut ExtCtxt, Span, @Expr, @Expr| -> @Expr,
12601261
base: @Expr,
12611262
enum_nonmatch_f: EnumNonMatchFunc,
12621263
cx: &mut ExtCtxt,
12631264
trait_span: Span,
12641265
substructure: &Substructure)
12651266
-> @Expr {
1267+
let f_ptr = &mut f;
12661268
cs_same_method(
12671269
|cx, span, vals| {
12681270
if use_foldl {
12691271
vals.iter().fold(base, |old, &new| {
1270-
f(cx, span, old, new)
1272+
(*f_ptr)(cx, span, old, new)
12711273
})
12721274
} else {
12731275
vals.iter().rev().fold(base, |old, &new| {
1274-
f(cx, span, old, new)
1276+
(*f_ptr)(cx, span, old, new)
12751277
})
12761278
}
12771279
},
12781280
enum_nonmatch_f,
1279-
cx, trait_span, substructure)
1281+
cx,
1282+
trait_span,
1283+
substructure)
12801284
}
12811285

12821286
/**

src/libsyntax/ext/deriving/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
4949
_span: Span,
5050
mitem: @MetaItem,
5151
item: @Item,
52-
push: |@Item|) {
52+
mut push: |@Item|) {
53+
let push_ref = &mut push;
5354
match mitem.node {
5455
MetaNameValue(_, ref l) => {
5556
cx.span_err(l.span, "unexpected value in `deriving`");
@@ -68,7 +69,7 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
6869
MetaWord(ref tname) => {
6970
macro_rules! expand(($func:path) => ($func(cx, titem.span,
7071
titem, item,
71-
|i| push(i))));
72+
|i| (*push_ref)(i))));
7273
match tname.get() {
7374
"Clone" => expand!(clone::expand_deriving_clone),
7475

src/libsyntax/ext/expand.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,14 @@ pub fn expand_item(it: @ast::Item, fld: &mut MacroExpander)
306306
// we'd ideally decorator_items.push_all(expand_item(item, fld)),
307307
// but that double-mut-borrows fld
308308
let mut items: SmallVector<@ast::Item> = SmallVector::zero();
309-
dec_fn(fld.cx, attr.span, attr.node.value, it,
310-
|item| items.push(item));
309+
{
310+
let items_ptr = &mut items;
311+
dec_fn(fld.cx,
312+
attr.span,
313+
attr.node.value,
314+
it,
315+
|item| items_ptr.push(item));
316+
}
311317
decorator_items.extend(items.move_iter()
312318
.flat_map(|item| expand_item(item, fld).move_iter()));
313319

src/libsyntax/parse/comments.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,13 @@ pub fn gather_comments_and_literals(span_diagnostic:
397397
//discard, and look ahead; we're working with internal state
398398
let TokenAndSpan {tok: tok, sp: sp} = rdr.peek();
399399
if token::is_lit(&tok) {
400+
let literals_ptr = &mut literals;
400401
with_str_from(&rdr, bstart, |s| {
401402
debug!("tok lit: {}", s);
402-
literals.push(Literal {lit: s.to_string(), pos: sp.lo});
403+
literals_ptr.push(Literal {
404+
lit: s.to_string(),
405+
pos: sp.lo,
406+
});
403407
})
404408
} else {
405409
debug!("tok: {}", token::to_str(&tok));

src/libsyntax/parse/lexer.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,20 @@ fn fatal_span_char(rdr: &mut StringReader,
158158
m: &str, c: char) -> ! {
159159
let mut m = m.to_string();
160160
m.push_str(": ");
161-
char::escape_default(c, |c| m.push_char(c));
161+
{
162+
let m_ptr = &mut m;
163+
char::escape_default(c, |c| m_ptr.push_char(c));
164+
}
162165
fatal_span(rdr, from_pos, to_pos, m.as_slice());
163166
}
164167

165168
fn err_span_char(rdr: &mut StringReader, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) {
166169
let mut m = m.to_string();
167170
m.push_str(": ");
168-
char::escape_default(c, |c| m.push_char(c));
171+
{
172+
let m_ptr = &mut m;
173+
char::escape_default(c, |c| m_ptr.push_char(c));
174+
}
169175
err_span(rdr, from_pos, to_pos, m.as_slice());
170176
}
171177

@@ -324,12 +330,14 @@ fn consume_any_line_comment(rdr: &mut StringReader)
324330
while !rdr.curr_is('\n') && !is_eof(rdr) {
325331
bump(rdr);
326332
}
333+
334+
let rdr_pos = rdr.pos;
327335
let ret = with_str_from(rdr, start_bpos, |string| {
328336
// but comments with only more "/"s are not
329337
if !is_line_non_doc_comment(string) {
330338
Some(TokenAndSpan{
331339
tok: token::DOC_COMMENT(str_to_ident(string)),
332-
sp: codemap::mk_sp(start_bpos, rdr.pos)
340+
sp: codemap::mk_sp(start_bpos, rdr_pos)
333341
})
334342
} else {
335343
None
@@ -405,12 +413,13 @@ fn consume_block_comment(rdr: &mut StringReader) -> Option<TokenAndSpan> {
405413
}
406414

407415
let res = if is_doc_comment {
416+
let rdr_pos = rdr.pos;
408417
with_str_from(rdr, start_bpos, |string| {
409418
// but comments with only "*"s between two "/"s are not
410419
if !is_block_non_doc_comment(string) {
411420
Some(TokenAndSpan{
412421
tok: token::DOC_COMMENT(str_to_ident(string)),
413-
sp: codemap::mk_sp(start_bpos, rdr.pos)
422+
sp: codemap::mk_sp(start_bpos, rdr_pos)
414423
})
415424
} else {
416425
None
@@ -674,11 +683,13 @@ fn next_token_inner(rdr: &mut StringReader) -> token::Token {
674683
bump(rdr);
675684
}
676685

677-
return with_str_from(rdr, start, |string| {
686+
let rdr_ptr = &rdr;
687+
return with_str_from(*rdr_ptr, start, |string| {
678688
if string == "_" {
679689
token::UNDERSCORE
680690
} else {
681-
let is_mod_name = rdr.curr_is(':') && nextch_is(rdr, ':');
691+
let is_mod_name = rdr_ptr.curr_is(':') &&
692+
nextch_is(*rdr_ptr, ':');
682693

683694
// FIXME: perform NFKC normalization here. (Issue #2253)
684695
token::IDENT(str_to_ident(string), is_mod_name)

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,18 +3442,22 @@ impl<'a> Parser<'a> {
34423442
if self.eat(&token::LT) {
34433443
let lifetimes = self.parse_lifetimes();
34443444
let mut seen_default = false;
3445+
let seen_default_ptr = &mut seen_default;
34453446
let ty_params = self.parse_seq_to_gt(Some(token::COMMA), |p| {
34463447
p.forbid_lifetime();
34473448
let ty_param = p.parse_ty_param();
34483449
if ty_param.default.is_some() {
3449-
seen_default = true;
3450-
} else if seen_default {
3450+
*seen_default_ptr = true;
3451+
} else if *seen_default_ptr {
34513452
p.span_err(p.last_span,
34523453
"type parameters with a default must be trailing");
34533454
}
34543455
ty_param
34553456
});
3456-
ast::Generics { lifetimes: lifetimes, ty_params: ty_params }
3457+
ast::Generics {
3458+
lifetimes: lifetimes,
3459+
ty_params: ty_params,
3460+
}
34573461
} else {
34583462
ast_util::empty_generics()
34593463
}
@@ -4235,20 +4239,21 @@ impl<'a> Parser<'a> {
42354239
name: String,
42364240
id_sp: Span) -> (ast::Item_, Vec<ast::Attribute> ) {
42374241
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
4238-
match included_mod_stack.iter().position(|p| *p == path) {
4242+
let path_ptr = &path;
4243+
match included_mod_stack.iter().position(|p| *p == *path_ptr) {
42394244
Some(i) => {
42404245
let mut err = String::from_str("circular modules: ");
42414246
let len = included_mod_stack.len();
42424247
for p in included_mod_stack.slice(i, len).iter() {
42434248
err.push_str(p.display().as_maybe_owned().as_slice());
42444249
err.push_str(" -> ");
42454250
}
4246-
err.push_str(path.display().as_maybe_owned().as_slice());
4251+
err.push_str(path_ptr.display().as_maybe_owned().as_slice());
42474252
self.span_fatal(id_sp, err.as_slice());
42484253
}
42494254
None => ()
42504255
}
4251-
included_mod_stack.push(path.clone());
4256+
included_mod_stack.push((*path_ptr).clone());
42524257
drop(included_mod_stack);
42534258

42544259
let mut p0 =

src/libsyntax/parse/token.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ pub fn to_str(t: &Token) -> String {
195195
/* Literals */
196196
LIT_CHAR(c) => {
197197
let mut res = String::from_str("'");
198-
c.escape_default(|c| {
199-
res.push_char(c);
200-
});
198+
{
199+
let res_ptr = &mut res;
200+
c.escape_default(|c| {
201+
res_ptr.push_char(c);
202+
});
203+
}
201204
res.push_char('\'');
202205
res
203206
}

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,10 @@ impl<'a> State<'a> {
22342234
ast::LitStr(ref st, style) => self.print_string(st.get(), style),
22352235
ast::LitChar(ch) => {
22362236
let mut res = String::from_str("'");
2237-
ch.escape_default(|c| res.push_char(c));
2237+
{
2238+
let res_ptr = &mut res;
2239+
ch.escape_default(|c| res_ptr.push_char(c));
2240+
}
22382241
res.push_char('\'');
22392242
word(&mut self.s, res.as_slice())
22402243
}

0 commit comments

Comments
 (0)