]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/expand.rs
rollup merge of #21438: taralx/kill-racycell
[rust.git] / src / libsyntax / ext / expand.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast::{Block, Crate, DeclLocal, ExprMac, PatMac};
12 use ast::{Local, Ident, MacInvocTT};
13 use ast::{ItemMac, MacStmtWithSemicolon, Mrk, Stmt, StmtDecl, StmtMac};
14 use ast::{StmtExpr, StmtSemi};
15 use ast::TokenTree;
16 use ast;
17 use ast_util::path_to_ident;
18 use ext::mtwt;
19 use ext::build::AstBuilder;
20 use attr;
21 use attr::AttrMetaMethods;
22 use codemap;
23 use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
24 use ext::base::*;
25 use fold;
26 use fold::*;
27 use parse;
28 use parse::token::{fresh_mark, fresh_name, intern};
29 use parse::token;
30 use ptr::P;
31 use util::small_vector::SmallVector;
32 use visit;
33 use visit::Visitor;
34
35 pub fn expand_type(t: P<ast::Ty>,
36                    fld: &mut MacroExpander,
37                    impl_ty: Option<P<ast::Ty>>)
38                    -> P<ast::Ty> {
39     debug!("expanding type {:?} with impl_ty {:?}", t, impl_ty);
40     let t = match (t.node.clone(), impl_ty) {
41         // Expand uses of `Self` in impls to the concrete type.
42         (ast::Ty_::TyPath(ref path, _), Some(ref impl_ty)) => {
43             let path_as_ident = path_to_ident(path);
44             // Note unhygenic comparison here. I think this is correct, since
45             // even though `Self` is almost just a type parameter, the treatment
46             // for this expansion is as if it were a keyword.
47             if path_as_ident.is_some() &&
48                path_as_ident.unwrap().name == token::special_idents::type_self.name {
49                 impl_ty.clone()
50             } else {
51                 t
52             }
53         }
54         _ => t
55     };
56     fold::noop_fold_ty(t, fld)
57 }
58
59 pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
60     e.and_then(|ast::Expr {id, node, span}| match node {
61         // expr_mac should really be expr_ext or something; it's the
62         // entry-point for all syntax extensions.
63         ast::ExprMac(mac) => {
64             let expanded_expr = match expand_mac_invoc(mac, span,
65                                                        |r| r.make_expr(),
66                                                        mark_expr, fld) {
67                 Some(expr) => expr,
68                 None => {
69                     return DummyResult::raw_expr(span);
70                 }
71             };
72
73             // Keep going, outside-in.
74             //
75             let fully_expanded = fld.fold_expr(expanded_expr);
76             fld.cx.bt_pop();
77
78             fully_expanded.map(|e| ast::Expr {
79                 id: ast::DUMMY_NODE_ID,
80                 node: e.node,
81                 span: span,
82             })
83         }
84
85         ast::ExprWhile(cond, body, opt_ident) => {
86             let cond = fld.fold_expr(cond);
87             let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
88             fld.cx.expr(span, ast::ExprWhile(cond, body, opt_ident))
89         }
90
91         // Desugar ExprWhileLet
92         // From: `[opt_ident]: while let <pat> = <expr> <body>`
93         ast::ExprWhileLet(pat, expr, body, opt_ident) => {
94             // to:
95             //
96             //   [opt_ident]: loop {
97             //     match <expr> {
98             //       <pat> => <body>,
99             //       _ => break
100             //     }
101             //   }
102
103             // `<pat> => <body>`
104             let pat_arm = {
105                 let body_expr = fld.cx.expr_block(body);
106                 fld.cx.arm(pat.span, vec![pat], body_expr)
107             };
108
109             // `_ => break`
110             let break_arm = {
111                 let pat_under = fld.cx.pat_wild(span);
112                 let break_expr = fld.cx.expr_break(span);
113                 fld.cx.arm(span, vec![pat_under], break_expr)
114             };
115
116             // `match <expr> { ... }`
117             let arms = vec![pat_arm, break_arm];
118             let match_expr = fld.cx.expr(span,
119                                     ast::ExprMatch(expr, arms, ast::MatchSource::WhileLetDesugar));
120
121             // `[opt_ident]: loop { ... }`
122             let loop_block = fld.cx.block_expr(match_expr);
123             let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
124             fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident))
125         }
126
127         // Desugar ExprIfLet
128         // From: `if let <pat> = <expr> <body> [<elseopt>]`
129         ast::ExprIfLet(pat, expr, body, mut elseopt) => {
130             // to:
131             //
132             //   match <expr> {
133             //     <pat> => <body>,
134             //     [_ if <elseopt_if_cond> => <elseopt_if_body>,]
135             //     _ => [<elseopt> | ()]
136             //   }
137
138             // `<pat> => <body>`
139             let pat_arm = {
140                 let body_expr = fld.cx.expr_block(body);
141                 fld.cx.arm(pat.span, vec![pat], body_expr)
142             };
143
144             // `[_ if <elseopt_if_cond> => <elseopt_if_body>,]`
145             let else_if_arms = {
146                 let mut arms = vec![];
147                 loop {
148                     let elseopt_continue = elseopt
149                         .and_then(|els| els.and_then(|els| match els.node {
150                         // else if
151                         ast::ExprIf(cond, then, elseopt) => {
152                             let pat_under = fld.cx.pat_wild(span);
153                             arms.push(ast::Arm {
154                                 attrs: vec![],
155                                 pats: vec![pat_under],
156                                 guard: Some(cond),
157                                 body: fld.cx.expr_block(then)
158                             });
159                             elseopt.map(|elseopt| (elseopt, true))
160                         }
161                         _ => Some((P(els), false))
162                     }));
163                     match elseopt_continue {
164                         Some((e, true)) => {
165                             elseopt = Some(e);
166                         }
167                         Some((e, false)) => {
168                             elseopt = Some(e);
169                             break;
170                         }
171                         None => {
172                             elseopt = None;
173                             break;
174                         }
175                     }
176                 }
177                 arms
178             };
179
180             let contains_else_clause = elseopt.is_some();
181
182             // `_ => [<elseopt> | ()]`
183             let else_arm = {
184                 let pat_under = fld.cx.pat_wild(span);
185                 let else_expr = elseopt.unwrap_or_else(|| fld.cx.expr_tuple(span, vec![]));
186                 fld.cx.arm(span, vec![pat_under], else_expr)
187             };
188
189             let mut arms = Vec::with_capacity(else_if_arms.len() + 2);
190             arms.push(pat_arm);
191             arms.extend(else_if_arms.into_iter());
192             arms.push(else_arm);
193
194             let match_expr = fld.cx.expr(span,
195                                          ast::ExprMatch(expr, arms,
196                                                 ast::MatchSource::IfLetDesugar {
197                                                     contains_else_clause: contains_else_clause,
198                                                 }));
199             fld.fold_expr(match_expr)
200         }
201
202         // Desugar support for ExprIfLet in the ExprIf else position
203         ast::ExprIf(cond, blk, elseopt) => {
204             let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
205                 ast::ExprIfLet(..) => {
206                     // wrap the if-let expr in a block
207                     let span = els.span;
208                     let blk = P(ast::Block {
209                         view_items: vec![],
210                         stmts: vec![],
211                         expr: Some(P(els)),
212                         id: ast::DUMMY_NODE_ID,
213                         rules: ast::DefaultBlock,
214                         span: span
215                     });
216                     fld.cx.expr_block(blk)
217                 }
218                 _ => P(els)
219             }));
220             let if_expr = fld.cx.expr(span, ast::ExprIf(cond, blk, elseopt));
221             if_expr.map(|e| noop_fold_expr(e, fld))
222         }
223
224         ast::ExprLoop(loop_block, opt_ident) => {
225             let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
226             fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident))
227         }
228
229         ast::ExprForLoop(pat, head, body, opt_ident) => {
230             let pat = fld.fold_pat(pat);
231             let head = fld.fold_expr(head);
232             let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
233             fld.cx.expr(span, ast::ExprForLoop(pat, head, body, opt_ident))
234         }
235
236         ast::ExprClosure(capture_clause, opt_kind, fn_decl, block) => {
237             let (rewritten_fn_decl, rewritten_block)
238                 = expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
239             let new_node = ast::ExprClosure(capture_clause,
240                                             opt_kind,
241                                             rewritten_fn_decl,
242                                             rewritten_block);
243             P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)})
244         }
245
246         _ => {
247             P(noop_fold_expr(ast::Expr {
248                 id: id,
249                 node: node,
250                 span: span
251             }, fld))
252         }
253     })
254 }
255
256 /// Expand a (not-ident-style) macro invocation. Returns the result
257 /// of expansion and the mark which must be applied to the result.
258 /// Our current interface doesn't allow us to apply the mark to the
259 /// result until after calling make_expr, make_items, etc.
260 fn expand_mac_invoc<T, F, G>(mac: ast::Mac, span: codemap::Span,
261                              parse_thunk: F,
262                              mark_thunk: G,
263                              fld: &mut MacroExpander)
264                              -> Option<T> where
265     F: FnOnce(Box<MacResult>) -> Option<T>,
266     G: FnOnce(T, Mrk) -> T,
267 {
268     match mac.node {
269         // it would almost certainly be cleaner to pass the whole
270         // macro invocation in, rather than pulling it apart and
271         // marking the tts and the ctxt separately. This also goes
272         // for the other three macro invocation chunks of code
273         // in this file.
274         // Token-tree macros:
275         MacInvocTT(pth, tts, _) => {
276             if pth.segments.len() > 1us {
277                 fld.cx.span_err(pth.span,
278                                 "expected macro name without module \
279                                 separators");
280                 // let compilation continue
281                 return None;
282             }
283             let extname = pth.segments[0].identifier;
284             let extnamestr = token::get_ident(extname);
285             match fld.cx.syntax_env.find(&extname.name) {
286                 None => {
287                     fld.cx.span_err(
288                         pth.span,
289                         &format!("macro undefined: '{}!'",
290                                 extnamestr.get())[]);
291
292                     // let compilation continue
293                     None
294                 }
295                 Some(rc) => match *rc {
296                     NormalTT(ref expandfun, exp_span) => {
297                         fld.cx.bt_push(ExpnInfo {
298                                 call_site: span,
299                                 callee: NameAndSpan {
300                                     name: extnamestr.get().to_string(),
301                                     format: MacroBang,
302                                     span: exp_span,
303                                 },
304                             });
305                         let fm = fresh_mark();
306                         let marked_before = mark_tts(&tts[], fm);
307
308                         // The span that we pass to the expanders we want to
309                         // be the root of the call stack. That's the most
310                         // relevant span and it's the actual invocation of
311                         // the macro.
312                         let mac_span = fld.cx.original_span();
313
314                         let opt_parsed = {
315                             let expanded = expandfun.expand(fld.cx,
316                                                             mac_span,
317                                                             &marked_before[]);
318                             parse_thunk(expanded)
319                         };
320                         let parsed = match opt_parsed {
321                             Some(e) => e,
322                             None => {
323                                 fld.cx.span_err(
324                                     pth.span,
325                                     &format!("non-expression macro in expression position: {}",
326                                             &extnamestr.get()[]
327                                             )[]);
328                                 return None;
329                             }
330                         };
331                         Some(mark_thunk(parsed,fm))
332                     }
333                     _ => {
334                         fld.cx.span_err(
335                             pth.span,
336                             &format!("'{}' is not a tt-style macro",
337                                     extnamestr.get())[]);
338                         None
339                     }
340                 }
341             }
342         }
343     }
344 }
345
346 /// Rename loop label and expand its loop body
347 ///
348 /// The renaming procedure for loop is different in the sense that the loop
349 /// body is in a block enclosed by loop head so the renaming of loop label
350 /// must be propagated to the enclosed context.
351 fn expand_loop_block(loop_block: P<Block>,
352                      opt_ident: Option<Ident>,
353                      fld: &mut MacroExpander) -> (P<Block>, Option<Ident>) {
354     match opt_ident {
355         Some(label) => {
356             let new_label = fresh_name(&label);
357             let rename = (label, new_label);
358
359             // The rename *must not* be added to the pending list of current
360             // syntax context otherwise an unrelated `break` or `continue` in
361             // the same context will pick that up in the deferred renaming pass
362             // and be renamed incorrectly.
363             let mut rename_list = vec!(rename);
364             let mut rename_fld = IdentRenamer{renames: &mut rename_list};
365             let renamed_ident = rename_fld.fold_ident(label);
366
367             // The rename *must* be added to the enclosed syntax context for
368             // `break` or `continue` to pick up because by definition they are
369             // in a block enclosed by loop head.
370             fld.cx.syntax_env.push_frame();
371             fld.cx.syntax_env.info().pending_renames.push(rename);
372             let expanded_block = expand_block_elts(loop_block, fld);
373             fld.cx.syntax_env.pop_frame();
374
375             (expanded_block, Some(renamed_ident))
376         }
377         None => (fld.fold_block(loop_block), opt_ident)
378     }
379 }
380
381 // eval $e with a new exts frame.
382 // must be a macro so that $e isn't evaluated too early.
383 macro_rules! with_exts_frame {
384     ($extsboxexpr:expr,$macros_escape:expr,$e:expr) =>
385     ({$extsboxexpr.push_frame();
386       $extsboxexpr.info().macros_escape = $macros_escape;
387       let result = $e;
388       $extsboxexpr.pop_frame();
389       result
390      })
391 }
392
393 // When we enter a module, record it, for the sake of `module!`
394 pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander)
395                    -> SmallVector<P<ast::Item>> {
396     let it = expand_item_modifiers(it, fld);
397
398     expand_annotatable(Annotatable::Item(it), fld)
399         .into_iter().map(|i| i.expect_item()).collect()
400 }
401
402 fn expand_item_modifiers(mut it: P<ast::Item>, fld: &mut MacroExpander)
403                          -> P<ast::Item> {
404     // partition the attributes into ItemModifiers and others
405     let (modifiers, other_attrs) = modifiers(&it.attrs, fld);
406
407     // update the attrs, leave everything else alone. Is this mutation really a good idea?
408     it = P(ast::Item {
409         attrs: other_attrs,
410         ..(*it).clone()
411     });
412
413     if modifiers.is_empty() {
414         let it = expand_item_multi_modifier(Annotatable::Item(it), fld);
415         return it.expect_item();
416     }
417
418     for attr in modifiers.iter() {
419         let mname = attr.name();
420
421         match fld.cx.syntax_env.find(&intern(mname.get())) {
422             Some(rc) => match *rc {
423                 Modifier(ref mac) => {
424                     attr::mark_used(attr);
425                     fld.cx.bt_push(ExpnInfo {
426                         call_site: attr.span,
427                         callee: NameAndSpan {
428                             name: mname.get().to_string(),
429                             format: MacroAttribute,
430                             span: None,
431                         }
432                     });
433                     it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
434                     fld.cx.bt_pop();
435                 }
436                 _ => unreachable!()
437             },
438             _ => unreachable!()
439         }
440     }
441
442     // Expansion may have added new ItemModifiers.
443     // It is possible, that an item modifier could expand to a multi-modifier or
444     // vice versa. In this case we will expand all modifiers before multi-modifiers,
445     // which might give an odd ordering. However, I think it is unlikely that the
446     // two kinds will be mixed, and I old-style multi-modifiers should be deprecated
447     // anyway.
448     expand_item_modifiers(it, fld)
449 }
450
451 /// Expand item_underscore
452 fn expand_item_underscore(item: ast::Item_, fld: &mut MacroExpander) -> ast::Item_ {
453     match item {
454         ast::ItemFn(decl, fn_style, abi, generics, body) => {
455             let (rewritten_fn_decl, rewritten_body)
456                 = expand_and_rename_fn_decl_and_block(decl, body, fld);
457             let expanded_generics = fold::noop_fold_generics(generics,fld);
458             ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body)
459         }
460         _ => noop_fold_item_underscore(item, fld)
461     }
462 }
463
464 // does this attribute list contain "macro_use" ?
465 fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool {
466     for attr in attrs.iter() {
467         let mut is_use = attr.check_name("macro_use");
468         if attr.check_name("macro_escape") {
469             fld.cx.span_warn(attr.span, "macro_escape is a deprecated synonym for macro_use");
470             is_use = true;
471             if let ast::AttrInner = attr.node.style {
472                 fld.cx.span_help(attr.span, "consider an outer attribute, \
473                                              #[macro_use] mod ...");
474             }
475         };
476
477         if is_use {
478             match attr.node.value.node {
479                 ast::MetaWord(..) => (),
480                 _ => fld.cx.span_err(attr.span, "arguments to macro_use are not allowed here"),
481             }
482             return true;
483         }
484     }
485     false
486 }
487
488 // Support for item-position macro invocations, exactly the same
489 // logic as for expression-position macro invocations.
490 pub fn expand_item_mac(it: P<ast::Item>,
491                        fld: &mut MacroExpander) -> SmallVector<P<ast::Item>> {
492     let (extname, path_span, tts) = match it.node {
493         ItemMac(codemap::Spanned {
494             node: MacInvocTT(ref pth, ref tts, _),
495             ..
496         }) => {
497             (pth.segments[0].identifier, pth.span, (*tts).clone())
498         }
499         _ => fld.cx.span_bug(it.span, "invalid item macro invocation")
500     };
501
502     let extnamestr = token::get_ident(extname);
503     let fm = fresh_mark();
504     let items = {
505         let expanded = match fld.cx.syntax_env.find(&extname.name) {
506             None => {
507                 fld.cx.span_err(path_span,
508                                 &format!("macro undefined: '{}!'",
509                                         extnamestr)[]);
510                 // let compilation continue
511                 return SmallVector::zero();
512             }
513
514             Some(rc) => match *rc {
515                 NormalTT(ref expander, span) => {
516                     if it.ident.name != parse::token::special_idents::invalid.name {
517                         fld.cx
518                             .span_err(path_span,
519                                       &format!("macro {}! expects no ident argument, \
520                                         given '{}'",
521                                       extnamestr,
522                                       token::get_ident(it.ident))[]);
523                         return SmallVector::zero();
524                     }
525                     fld.cx.bt_push(ExpnInfo {
526                         call_site: it.span,
527                         callee: NameAndSpan {
528                             name: extnamestr.get().to_string(),
529                             format: MacroBang,
530                             span: span
531                         }
532                     });
533                     // mark before expansion:
534                     let marked_before = mark_tts(&tts[], fm);
535                     expander.expand(fld.cx, it.span, &marked_before[])
536                 }
537                 IdentTT(ref expander, span) => {
538                     if it.ident.name == parse::token::special_idents::invalid.name {
539                         fld.cx.span_err(path_span,
540                                         &format!("macro {}! expects an ident argument",
541                                                 extnamestr.get())[]);
542                         return SmallVector::zero();
543                     }
544                     fld.cx.bt_push(ExpnInfo {
545                         call_site: it.span,
546                         callee: NameAndSpan {
547                             name: extnamestr.get().to_string(),
548                             format: MacroBang,
549                             span: span
550                         }
551                     });
552                     // mark before expansion:
553                     let marked_tts = mark_tts(&tts[], fm);
554                     expander.expand(fld.cx, it.span, it.ident, marked_tts)
555                 }
556                 MacroRulesTT => {
557                     if it.ident.name == parse::token::special_idents::invalid.name {
558                         fld.cx.span_err(path_span,
559                                         &format!("macro_rules! expects an ident argument")
560                                         []);
561                         return SmallVector::zero();
562                     }
563                     fld.cx.bt_push(ExpnInfo {
564                         call_site: it.span,
565                         callee: NameAndSpan {
566                             name: extnamestr.get().to_string(),
567                             format: MacroBang,
568                             span: None,
569                         }
570                     });
571                     // DON'T mark before expansion.
572
573                     let def = ast::MacroDef {
574                         ident: it.ident,
575                         attrs: it.attrs.clone(),
576                         id: ast::DUMMY_NODE_ID,
577                         span: it.span,
578                         imported_from: None,
579                         export: attr::contains_name(it.attrs.as_slice(), "macro_export"),
580                         use_locally: true,
581                         body: tts,
582                     };
583                     fld.cx.insert_macro(def);
584
585                     // macro_rules! has a side effect but expands to nothing.
586                     fld.cx.bt_pop();
587                     return SmallVector::zero();
588                 }
589                 _ => {
590                     fld.cx.span_err(it.span,
591                                     &format!("{}! is not legal in item position",
592                                             extnamestr.get())[]);
593                     return SmallVector::zero();
594                 }
595             }
596         };
597
598         expanded.make_items()
599     };
600
601     let items = match items {
602         Some(items) => {
603             items.into_iter()
604                 .map(|i| mark_item(i, fm))
605                 .flat_map(|i| fld.fold_item(i).into_iter())
606                 .collect()
607         }
608         None => {
609             fld.cx.span_err(path_span,
610                             &format!("non-item macro in item position: {}",
611                                     extnamestr.get())[]);
612             return SmallVector::zero();
613         }
614     };
615
616     fld.cx.bt_pop();
617     items
618 }
619
620 /// Expand a stmt
621 fn expand_stmt(s: Stmt, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> {
622     let (mac, style) = match s.node {
623         StmtMac(mac, style) => (mac, style),
624         _ => return expand_non_macro_stmt(s, fld)
625     };
626     let expanded_stmt = match expand_mac_invoc(mac.and_then(|m| m), s.span,
627                                                 |r| r.make_stmt(),
628                                                 mark_stmt, fld) {
629         Some(stmt) => stmt,
630         None => {
631             return SmallVector::zero();
632         }
633     };
634
635     // Keep going, outside-in.
636     let fully_expanded = fld.fold_stmt(expanded_stmt);
637     fld.cx.bt_pop();
638
639     if style == MacStmtWithSemicolon {
640         fully_expanded.into_iter().map(|s| s.map(|Spanned {node, span}| {
641             Spanned {
642                 node: match node {
643                     StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
644                     _ => node /* might already have a semi */
645                 },
646                 span: span
647             }
648         })).collect()
649     } else {
650         fully_expanded
651     }
652 }
653
654 // expand a non-macro stmt. this is essentially the fallthrough for
655 // expand_stmt, above.
656 fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroExpander)
657                          -> SmallVector<P<Stmt>> {
658     // is it a let?
659     match node {
660         StmtDecl(decl, node_id) => decl.and_then(|Spanned {node: decl, span}| match decl {
661             DeclLocal(local) => {
662                 // take it apart:
663                 let rewritten_local = local.map(|Local {id, pat, ty, init, source, span}| {
664                     // expand the ty since TyFixedLengthVec contains an Expr
665                     // and thus may have a macro use
666                     let expanded_ty = ty.map(|t| fld.fold_ty(t));
667                     // expand the pat (it might contain macro uses):
668                     let expanded_pat = fld.fold_pat(pat);
669                     // find the PatIdents in the pattern:
670                     // oh dear heaven... this is going to include the enum
671                     // names, as well... but that should be okay, as long as
672                     // the new names are gensyms for the old ones.
673                     // generate fresh names, push them to a new pending list
674                     let idents = pattern_bindings(&*expanded_pat);
675                     let mut new_pending_renames =
676                         idents.iter().map(|ident| (*ident, fresh_name(ident))).collect();
677                     // rewrite the pattern using the new names (the old
678                     // ones have already been applied):
679                     let rewritten_pat = {
680                         // nested binding to allow borrow to expire:
681                         let mut rename_fld = IdentRenamer{renames: &mut new_pending_renames};
682                         rename_fld.fold_pat(expanded_pat)
683                     };
684                     // add them to the existing pending renames:
685                     fld.cx.syntax_env.info().pending_renames
686                           .extend(new_pending_renames.into_iter());
687                     Local {
688                         id: id,
689                         ty: expanded_ty,
690                         pat: rewritten_pat,
691                         // also, don't forget to expand the init:
692                         init: init.map(|e| fld.fold_expr(e)),
693                         source: source,
694                         span: span
695                     }
696                 });
697                 SmallVector::one(P(Spanned {
698                     node: StmtDecl(P(Spanned {
699                             node: DeclLocal(rewritten_local),
700                             span: span
701                         }),
702                         node_id),
703                     span: stmt_span
704                 }))
705             }
706             _ => {
707                 noop_fold_stmt(Spanned {
708                     node: StmtDecl(P(Spanned {
709                             node: decl,
710                             span: span
711                         }),
712                         node_id),
713                     span: stmt_span
714                 }, fld)
715             }
716         }),
717         _ => {
718             noop_fold_stmt(Spanned {
719                 node: node,
720                 span: stmt_span
721             }, fld)
722         }
723     }
724 }
725
726 // expand the arm of a 'match', renaming for macro hygiene
727 fn expand_arm(arm: ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
728     // expand pats... they might contain macro uses:
729     let expanded_pats = arm.pats.move_map(|pat| fld.fold_pat(pat));
730     if expanded_pats.len() == 0 {
731         panic!("encountered match arm with 0 patterns");
732     }
733     // all of the pats must have the same set of bindings, so use the
734     // first one to extract them and generate new names:
735     let idents = pattern_bindings(&*expanded_pats[0]);
736     let new_renames = idents.into_iter().map(|id| (id, fresh_name(&id))).collect();
737     // apply the renaming, but only to the PatIdents:
738     let mut rename_pats_fld = PatIdentRenamer{renames:&new_renames};
739     let rewritten_pats = expanded_pats.move_map(|pat| rename_pats_fld.fold_pat(pat));
740     // apply renaming and then expansion to the guard and the body:
741     let mut rename_fld = IdentRenamer{renames:&new_renames};
742     let rewritten_guard =
743         arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g)));
744     let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body));
745     ast::Arm {
746         attrs: arm.attrs.move_map(|x| fld.fold_attribute(x)),
747         pats: rewritten_pats,
748         guard: rewritten_guard,
749         body: rewritten_body,
750     }
751 }
752
753 /// A visitor that extracts the PatIdent (binding) paths
754 /// from a given thingy and puts them in a mutable
755 /// array
756 #[derive(Clone)]
757 struct PatIdentFinder {
758     ident_accumulator: Vec<ast::Ident>
759 }
760
761 impl<'v> Visitor<'v> for PatIdentFinder {
762     fn visit_pat(&mut self, pattern: &ast::Pat) {
763         match *pattern {
764             ast::Pat { id: _, node: ast::PatIdent(_, ref path1, ref inner), span: _ } => {
765                 self.ident_accumulator.push(path1.node);
766                 // visit optional subpattern of PatIdent:
767                 for subpat in inner.iter() {
768                     self.visit_pat(&**subpat)
769                 }
770             }
771             // use the default traversal for non-PatIdents
772             _ => visit::walk_pat(self, pattern)
773         }
774     }
775 }
776
777 /// find the PatIdent paths in a pattern
778 fn pattern_bindings(pat: &ast::Pat) -> Vec<ast::Ident> {
779     let mut name_finder = PatIdentFinder{ident_accumulator:Vec::new()};
780     name_finder.visit_pat(pat);
781     name_finder.ident_accumulator
782 }
783
784 /// find the PatIdent paths in a
785 fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> {
786     let mut pat_idents = PatIdentFinder{ident_accumulator:Vec::new()};
787     for arg in fn_decl.inputs.iter() {
788         pat_idents.visit_pat(&*arg.pat);
789     }
790     pat_idents.ident_accumulator
791 }
792
793 // expand a block. pushes a new exts_frame, then calls expand_block_elts
794 pub fn expand_block(blk: P<Block>, fld: &mut MacroExpander) -> P<Block> {
795     // see note below about treatment of exts table
796     with_exts_frame!(fld.cx.syntax_env,false,
797                      expand_block_elts(blk, fld))
798 }
799
800 // expand the elements of a block.
801 pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {
802     b.map(|Block {id, view_items, stmts, expr, rules, span}| {
803         let new_view_items = view_items.into_iter().map(|x| fld.fold_view_item(x)).collect();
804         let new_stmts = stmts.into_iter().flat_map(|x| {
805             // perform all pending renames
806             let renamed_stmt = {
807                 let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
808                 let mut rename_fld = IdentRenamer{renames:pending_renames};
809                 rename_fld.fold_stmt(x).expect_one("rename_fold didn't return one value")
810             };
811             // expand macros in the statement
812             fld.fold_stmt(renamed_stmt).into_iter()
813         }).collect();
814         let new_expr = expr.map(|x| {
815             let expr = {
816                 let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
817                 let mut rename_fld = IdentRenamer{renames:pending_renames};
818                 rename_fld.fold_expr(x)
819             };
820             fld.fold_expr(expr)
821         });
822         Block {
823             id: fld.new_id(id),
824             view_items: new_view_items,
825             stmts: new_stmts,
826             expr: new_expr,
827             rules: rules,
828             span: span
829         }
830     })
831 }
832
833 fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
834     match p.node {
835         PatMac(_) => {}
836         _ => return noop_fold_pat(p, fld)
837     }
838     p.map(|ast::Pat {node, span, ..}| {
839         let (pth, tts) = match node {
840             PatMac(mac) => match mac.node {
841                 MacInvocTT(pth, tts, _) => {
842                     (pth, tts)
843                 }
844             },
845             _ => unreachable!()
846         };
847         if pth.segments.len() > 1us {
848             fld.cx.span_err(pth.span, "expected macro name without module separators");
849             return DummyResult::raw_pat(span);
850         }
851         let extname = pth.segments[0].identifier;
852         let extnamestr = token::get_ident(extname);
853         let marked_after = match fld.cx.syntax_env.find(&extname.name) {
854             None => {
855                 fld.cx.span_err(pth.span,
856                                 &format!("macro undefined: '{}!'",
857                                         extnamestr)[]);
858                 // let compilation continue
859                 return DummyResult::raw_pat(span);
860             }
861
862             Some(rc) => match *rc {
863                 NormalTT(ref expander, tt_span) => {
864                     fld.cx.bt_push(ExpnInfo {
865                         call_site: span,
866                         callee: NameAndSpan {
867                             name: extnamestr.get().to_string(),
868                             format: MacroBang,
869                             span: tt_span
870                         }
871                     });
872
873                     let fm = fresh_mark();
874                     let marked_before = mark_tts(&tts[], fm);
875                     let mac_span = fld.cx.original_span();
876                     let expanded = match expander.expand(fld.cx,
877                                         mac_span,
878                                         &marked_before[]).make_pat() {
879                         Some(e) => e,
880                         None => {
881                             fld.cx.span_err(
882                                 pth.span,
883                                 &format!(
884                                     "non-pattern macro in pattern position: {}",
885                                     extnamestr.get()
886                                     )[]
887                             );
888                             return DummyResult::raw_pat(span);
889                         }
890                     };
891
892                     // mark after:
893                     mark_pat(expanded,fm)
894                 }
895                 _ => {
896                     fld.cx.span_err(span,
897                                     &format!("{}! is not legal in pattern position",
898                                             extnamestr.get())[]);
899                     return DummyResult::raw_pat(span);
900                 }
901             }
902         };
903
904         let fully_expanded =
905             fld.fold_pat(marked_after).node.clone();
906         fld.cx.bt_pop();
907
908         ast::Pat {
909             id: ast::DUMMY_NODE_ID,
910             node: fully_expanded,
911             span: span
912         }
913     })
914 }
915
916 /// A tree-folder that applies every rename in its (mutable) list
917 /// to every identifier, including both bindings and varrefs
918 /// (and lots of things that will turn out to be neither)
919 pub struct IdentRenamer<'a> {
920     renames: &'a mtwt::RenameList,
921 }
922
923 impl<'a> Folder for IdentRenamer<'a> {
924     fn fold_ident(&mut self, id: Ident) -> Ident {
925         Ident {
926             name: id.name,
927             ctxt: mtwt::apply_renames(self.renames, id.ctxt),
928         }
929     }
930     fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
931         fold::noop_fold_mac(mac, self)
932     }
933 }
934
935 /// A tree-folder that applies every rename in its list to
936 /// the idents that are in PatIdent patterns. This is more narrowly
937 /// focused than IdentRenamer, and is needed for FnDecl,
938 /// where we want to rename the args but not the fn name or the generics etc.
939 pub struct PatIdentRenamer<'a> {
940     renames: &'a mtwt::RenameList,
941 }
942
943 impl<'a> Folder for PatIdentRenamer<'a> {
944     fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
945         match pat.node {
946             ast::PatIdent(..) => {},
947             _ => return noop_fold_pat(pat, self)
948         }
949
950         pat.map(|ast::Pat {id, node, span}| match node {
951             ast::PatIdent(binding_mode, Spanned{span: sp, node: ident}, sub) => {
952                 let new_ident = Ident{name: ident.name,
953                                       ctxt: mtwt::apply_renames(self.renames, ident.ctxt)};
954                 let new_node =
955                     ast::PatIdent(binding_mode,
956                                   Spanned{span: self.new_span(sp), node: new_ident},
957                                   sub.map(|p| self.fold_pat(p)));
958                 ast::Pat {
959                     id: id,
960                     node: new_node,
961                     span: self.new_span(span)
962                 }
963             },
964             _ => unreachable!()
965         })
966     }
967     fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
968         fold::noop_fold_mac(mac, self)
969     }
970 }
971
972 fn expand_annotatable(a: Annotatable,
973                       fld: &mut MacroExpander)
974                       -> SmallVector<Annotatable> {
975     let a = expand_item_multi_modifier(a, fld);
976
977     let mut decorator_items = SmallVector::zero();
978     let mut new_attrs = Vec::new();
979     for attr in a.attrs().iter() {
980         let mname = attr.name();
981
982         match fld.cx.syntax_env.find(&intern(mname.get())) {
983             Some(rc) => match *rc {
984                 Decorator(ref dec) => {
985                     let it = match a {
986                         Annotatable::Item(ref it) => it,
987                         // ItemDecorators are only implemented for Items.
988                         _ => break,
989                     };
990
991                     attr::mark_used(attr);
992
993                     fld.cx.bt_push(ExpnInfo {
994                         call_site: attr.span,
995                         callee: NameAndSpan {
996                             name: mname.get().to_string(),
997                             format: MacroAttribute,
998                             span: None
999                         }
1000                     });
1001
1002                     // we'd ideally decorator_items.push_all(expand_item(item, fld)),
1003                     // but that double-mut-borrows fld
1004                     let mut items: SmallVector<P<ast::Item>> = SmallVector::zero();
1005                     dec.expand(fld.cx, attr.span, &*attr.node.value, &**it,
1006                                box |&mut: item| items.push(item));
1007                     decorator_items.extend(items.into_iter()
1008                         .flat_map(|item| expand_item(item, fld).into_iter()));
1009
1010                     fld.cx.bt_pop();
1011                 }
1012                 _ => new_attrs.push((*attr).clone()),
1013             },
1014             _ => new_attrs.push((*attr).clone()),
1015         }
1016     }
1017
1018     let mut new_items: SmallVector<Annotatable> = match a {
1019         Annotatable::Item(it) => match it.node {
1020             ast::ItemMac(..) => {
1021                 expand_item_mac(it, fld).into_iter().map(|i| Annotatable::Item(i)).collect()
1022             }
1023             ast::ItemMod(_) | ast::ItemForeignMod(_) => {
1024                 let valid_ident =
1025                     it.ident.name != parse::token::special_idents::invalid.name;
1026
1027                 if valid_ident {
1028                     fld.cx.mod_push(it.ident);
1029                 }
1030                 let macro_use = contains_macro_use(fld, &new_attrs[]);
1031                 let result = with_exts_frame!(fld.cx.syntax_env,
1032                                               macro_use,
1033                                               noop_fold_item(it, fld));
1034                 if valid_ident {
1035                     fld.cx.mod_pop();
1036                 }
1037                 result.into_iter().map(|i| Annotatable::Item(i)).collect()
1038             },
1039             _ => {
1040                 let it = P(ast::Item {
1041                     attrs: new_attrs,
1042                     ..(*it).clone()
1043                 });
1044                 noop_fold_item(it, fld).into_iter().map(|i| Annotatable::Item(i)).collect()
1045             }
1046         },
1047         Annotatable::TraitItem(it) => match it {
1048             ast::TraitItem::ProvidedMethod(m) => {
1049                 expand_method(m, fld).into_iter().map(|m|
1050                     Annotatable::TraitItem(ast::TraitItem::ProvidedMethod(m))).collect()
1051             }
1052             ast::TraitItem::RequiredMethod(m) => {
1053                 SmallVector::one(Annotatable::TraitItem(
1054                     ast::TraitItem::RequiredMethod(fld.fold_type_method(m))))
1055             }
1056             ast::TraitItem::TypeTraitItem(t) => {
1057                 SmallVector::one(Annotatable::TraitItem(
1058                     ast::TraitItem::TypeTraitItem(P(fld.fold_associated_type((*t).clone())))))
1059             }
1060         },
1061         Annotatable::ImplItem(it) => match it {
1062             ast::ImplItem::MethodImplItem(m) => {
1063                 expand_method(m, fld).into_iter().map(|m|
1064                     Annotatable::ImplItem(ast::ImplItem::MethodImplItem(m))).collect()
1065             }
1066             ast::ImplItem::TypeImplItem(t) => {
1067                 SmallVector::one(Annotatable::ImplItem(
1068                     ast::ImplItem::TypeImplItem(P(fld.fold_typedef((*t).clone())))))
1069             }
1070         }
1071     };
1072
1073     new_items.push_all(decorator_items.into_iter().map(|i| Annotatable::Item(i)).collect());
1074     new_items
1075 }
1076
1077 fn expand_trait_item(i: ast::TraitItem,
1078                      fld: &mut MacroExpander)
1079                      -> SmallVector<ast::TraitItem> {
1080     expand_annotatable(Annotatable::TraitItem(i), fld)
1081         .into_iter().map(|i| i.expect_trait_item()).collect()
1082
1083 }
1084
1085 fn expand_impl_item(i: ast::ImplItem,
1086                     fld: &mut MacroExpander)
1087                     -> SmallVector<ast::ImplItem> {
1088     expand_annotatable(Annotatable::ImplItem(i), fld)
1089         .into_iter().map(|i| i.expect_impl_item()).collect()
1090 }
1091
1092 // partition the attributes into ItemModifiers and others
1093 fn modifiers(attrs: &Vec<ast::Attribute>,
1094              fld: &MacroExpander)
1095              -> (Vec<ast::Attribute>, Vec<ast::Attribute>) {
1096     attrs.iter().cloned().partition(|attr| {
1097         match fld.cx.syntax_env.find(&intern(attr.name().get())) {
1098             Some(rc) => match *rc {
1099                 Modifier(_) => true,
1100                 _ => false
1101             },
1102             _ => false
1103         }
1104     })
1105 }
1106
1107 // partition the attributes into MultiModifiers and others
1108 fn multi_modifiers(attrs: &[ast::Attribute],
1109                    fld: &MacroExpander)
1110                    -> (Vec<ast::Attribute>, Vec<ast::Attribute>) {
1111     attrs.iter().cloned().partition(|attr| {
1112         match fld.cx.syntax_env.find(&intern(attr.name().get())) {
1113             Some(rc) => match *rc {
1114                 MultiModifier(_) => true,
1115                 _ => false
1116             },
1117             _ => false
1118         }
1119     })
1120 }
1121
1122 fn expand_item_multi_modifier(mut it: Annotatable,
1123                               fld: &mut MacroExpander)
1124                               -> Annotatable {
1125     let (modifiers, other_attrs) = multi_modifiers(it.attrs(), fld);
1126
1127     // Update the attrs, leave everything else alone. Is this mutation really a good idea?
1128     it = it.fold_attrs(other_attrs);
1129
1130     if modifiers.is_empty() {
1131         return it
1132     }
1133
1134     for attr in modifiers.iter() {
1135         let mname = attr.name();
1136
1137         match fld.cx.syntax_env.find(&intern(mname.get())) {
1138             Some(rc) => match *rc {
1139                 MultiModifier(ref mac) => {
1140                     attr::mark_used(attr);
1141                     fld.cx.bt_push(ExpnInfo {
1142                         call_site: attr.span,
1143                         callee: NameAndSpan {
1144                             name: mname.get().to_string(),
1145                             format: MacroAttribute,
1146                             span: None,
1147                         }
1148                     });
1149                     it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
1150                     fld.cx.bt_pop();
1151                 }
1152                 _ => unreachable!()
1153             },
1154             _ => unreachable!()
1155         }
1156     }
1157
1158     // Expansion may have added new ItemModifiers.
1159     expand_item_multi_modifier(it, fld)
1160 }
1161
1162 // expand a method
1163 fn expand_method(m: P<ast::Method>, fld: &mut MacroExpander) -> SmallVector<P<ast::Method>> {
1164     m.and_then(|m| match m.node {
1165         ast::MethDecl(ident,
1166                       generics,
1167                       abi,
1168                       explicit_self,
1169                       fn_style,
1170                       decl,
1171                       body,
1172                       vis) => {
1173             let id = fld.new_id(m.id);
1174             let (rewritten_fn_decl, rewritten_body)
1175                 = expand_and_rename_fn_decl_and_block(decl, body, fld);
1176             SmallVector::one(P(ast::Method {
1177                     attrs: m.attrs.move_map(|a| fld.fold_attribute(a)),
1178                     id: id,
1179                     span: fld.new_span(m.span),
1180                     node: ast::MethDecl(fld.fold_ident(ident),
1181                                         noop_fold_generics(generics, fld),
1182                                         abi,
1183                                         fld.fold_explicit_self(explicit_self),
1184                                         fn_style,
1185                                         rewritten_fn_decl,
1186                                         rewritten_body,
1187                                         vis)
1188                 }))
1189         },
1190         ast::MethMac(mac) => {
1191             let maybe_new_methods =
1192                 expand_mac_invoc(mac, m.span,
1193                                  |r| r.make_methods(),
1194                                  |meths, mark| meths.move_map(|m| mark_method(m, mark)),
1195                                  fld);
1196
1197             match maybe_new_methods {
1198                 Some(methods) => {
1199                     // expand again if necessary
1200                     let new_methods = methods.into_iter()
1201                                              .flat_map(|m| fld.fold_method(m).into_iter())
1202                                              .collect();
1203                     fld.cx.bt_pop();
1204                     new_methods
1205                 }
1206                 None => SmallVector::zero()
1207             }
1208         }
1209     })
1210 }
1211
1212 /// Given a fn_decl and a block and a MacroExpander, expand the fn_decl, then use the
1213 /// PatIdents in its arguments to perform renaming in the FnDecl and
1214 /// the block, returning both the new FnDecl and the new Block.
1215 fn expand_and_rename_fn_decl_and_block(fn_decl: P<ast::FnDecl>, block: P<ast::Block>,
1216                                        fld: &mut MacroExpander)
1217     -> (P<ast::FnDecl>, P<ast::Block>) {
1218     let expanded_decl = fld.fold_fn_decl(fn_decl);
1219     let idents = fn_decl_arg_bindings(&*expanded_decl);
1220     let renames =
1221         idents.iter().map(|id : &ast::Ident| (*id,fresh_name(id))).collect();
1222     // first, a renamer for the PatIdents, for the fn_decl:
1223     let mut rename_pat_fld = PatIdentRenamer{renames: &renames};
1224     let rewritten_fn_decl = rename_pat_fld.fold_fn_decl(expanded_decl);
1225     // now, a renamer for *all* idents, for the body:
1226     let mut rename_fld = IdentRenamer{renames: &renames};
1227     let rewritten_body = fld.fold_block(rename_fld.fold_block(block));
1228     (rewritten_fn_decl,rewritten_body)
1229 }
1230
1231 /// A tree-folder that performs macro expansion
1232 pub struct MacroExpander<'a, 'b:'a> {
1233     pub cx: &'a mut ExtCtxt<'b>,
1234     // The type of the impl currently being expanded.
1235     current_impl_type: Option<P<ast::Ty>>,
1236 }
1237
1238 impl<'a, 'b> MacroExpander<'a, 'b> {
1239     pub fn new(cx: &'a mut ExtCtxt<'b>) -> MacroExpander<'a, 'b> {
1240         MacroExpander { cx: cx, current_impl_type: None }
1241     }
1242 }
1243
1244 impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1245     fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
1246         expand_expr(expr, self)
1247     }
1248
1249     fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
1250         expand_pat(pat, self)
1251     }
1252
1253     fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
1254         let prev_type = self.current_impl_type.clone();
1255         if let ast::Item_::ItemImpl(_, _, _, _, ref ty, _) = item.node {
1256             self.current_impl_type = Some(ty.clone());
1257         }
1258
1259         let result = expand_item(item, self);
1260         self.current_impl_type = prev_type;
1261         result
1262     }
1263
1264     fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ {
1265         expand_item_underscore(item, self)
1266     }
1267
1268     fn fold_stmt(&mut self, stmt: P<ast::Stmt>) -> SmallVector<P<ast::Stmt>> {
1269         stmt.and_then(|stmt| expand_stmt(stmt, self))
1270     }
1271
1272     fn fold_block(&mut self, block: P<Block>) -> P<Block> {
1273         expand_block(block, self)
1274     }
1275
1276     fn fold_arm(&mut self, arm: ast::Arm) -> ast::Arm {
1277         expand_arm(arm, self)
1278     }
1279
1280     fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector<ast::TraitItem> {
1281         expand_trait_item(i, self)
1282     }
1283
1284     fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector<ast::ImplItem> {
1285         expand_impl_item(i, self)
1286     }
1287
1288     fn fold_method(&mut self, method: P<ast::Method>) -> SmallVector<P<ast::Method>> {
1289         expand_method(method, self)
1290     }
1291
1292     fn fold_ty(&mut self, t: P<ast::Ty>) -> P<ast::Ty> {
1293         let impl_type = self.current_impl_type.clone();
1294         expand_type(t, self, impl_type)
1295     }
1296
1297     fn new_span(&mut self, span: Span) -> Span {
1298         new_span(self.cx, span)
1299     }
1300 }
1301
1302 fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
1303     /* this discards information in the case of macro-defining macros */
1304     Span {
1305         lo: sp.lo,
1306         hi: sp.hi,
1307         expn_id: cx.backtrace(),
1308     }
1309 }
1310
1311 pub struct ExpansionConfig {
1312     pub crate_name: String,
1313     pub enable_quotes: bool,
1314     pub recursion_limit: usize,
1315 }
1316
1317 impl ExpansionConfig {
1318     pub fn default(crate_name: String) -> ExpansionConfig {
1319         ExpansionConfig {
1320             crate_name: crate_name,
1321             enable_quotes: false,
1322             recursion_limit: 64,
1323         }
1324     }
1325 }
1326
1327 pub fn expand_crate(parse_sess: &parse::ParseSess,
1328                     cfg: ExpansionConfig,
1329                     // these are the macros being imported to this crate:
1330                     imported_macros: Vec<ast::MacroDef>,
1331                     user_exts: Vec<NamedSyntaxExtension>,
1332                     c: Crate) -> Crate {
1333     let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
1334     let mut expander = MacroExpander::new(&mut cx);
1335
1336     for def in imported_macros.into_iter() {
1337         expander.cx.insert_macro(def);
1338     }
1339
1340     for (name, extension) in user_exts.into_iter() {
1341         expander.cx.syntax_env.insert(name, extension);
1342     }
1343
1344     let mut ret = expander.fold_crate(c);
1345     ret.exported_macros = expander.cx.exported_macros.clone();
1346     parse_sess.span_diagnostic.handler().abort_if_errors();
1347     return ret;
1348 }
1349
1350 // HYGIENIC CONTEXT EXTENSION:
1351 // all of these functions are for walking over
1352 // ASTs and making some change to the context of every
1353 // element that has one. a CtxtFn is a trait-ified
1354 // version of a closure in (SyntaxContext -> SyntaxContext).
1355 // the ones defined here include:
1356 // Marker - add a mark to a context
1357
1358 // A Marker adds the given mark to the syntax context
1359 struct Marker { mark: Mrk }
1360
1361 impl Folder for Marker {
1362     fn fold_ident(&mut self, id: Ident) -> Ident {
1363         ast::Ident {
1364             name: id.name,
1365             ctxt: mtwt::apply_mark(self.mark, id.ctxt)
1366         }
1367     }
1368     fn fold_mac(&mut self, Spanned {node, span}: ast::Mac) -> ast::Mac {
1369         Spanned {
1370             node: match node {
1371                 MacInvocTT(path, tts, ctxt) => {
1372                     MacInvocTT(self.fold_path(path),
1373                                self.fold_tts(&tts[]),
1374                                mtwt::apply_mark(self.mark, ctxt))
1375                 }
1376             },
1377             span: span,
1378         }
1379     }
1380 }
1381
1382 // apply a given mark to the given token trees. Used prior to expansion of a macro.
1383 fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
1384     noop_fold_tts(tts, &mut Marker{mark:m})
1385 }
1386
1387 // apply a given mark to the given expr. Used following the expansion of a macro.
1388 fn mark_expr(expr: P<ast::Expr>, m: Mrk) -> P<ast::Expr> {
1389     Marker{mark:m}.fold_expr(expr)
1390 }
1391
1392 // apply a given mark to the given pattern. Used following the expansion of a macro.
1393 fn mark_pat(pat: P<ast::Pat>, m: Mrk) -> P<ast::Pat> {
1394     Marker{mark:m}.fold_pat(pat)
1395 }
1396
1397 // apply a given mark to the given stmt. Used following the expansion of a macro.
1398 fn mark_stmt(expr: P<ast::Stmt>, m: Mrk) -> P<ast::Stmt> {
1399     Marker{mark:m}.fold_stmt(expr)
1400         .expect_one("marking a stmt didn't return exactly one stmt")
1401 }
1402
1403 // apply a given mark to the given item. Used following the expansion of a macro.
1404 fn mark_item(expr: P<ast::Item>, m: Mrk) -> P<ast::Item> {
1405     Marker{mark:m}.fold_item(expr)
1406         .expect_one("marking an item didn't return exactly one item")
1407 }
1408
1409 // apply a given mark to the given item. Used following the expansion of a macro.
1410 fn mark_method(expr: P<ast::Method>, m: Mrk) -> P<ast::Method> {
1411     Marker{mark:m}.fold_method(expr)
1412         .expect_one("marking an item didn't return exactly one method")
1413 }
1414
1415 /// Check that there are no macro invocations left in the AST:
1416 pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
1417     visit::walk_crate(&mut MacroExterminator{sess:sess}, krate);
1418 }
1419
1420 /// A visitor that ensures that no macro invocations remain in an AST.
1421 struct MacroExterminator<'a>{
1422     sess: &'a parse::ParseSess
1423 }
1424
1425 impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> {
1426     fn visit_mac(&mut self, mac: &ast::Mac) {
1427         self.sess.span_diagnostic.span_bug(mac.span,
1428                                            "macro exterminator: expected AST \
1429                                            with no macro invocations");
1430     }
1431 }
1432
1433
1434 #[cfg(test)]
1435 mod test {
1436     use super::{pattern_bindings, expand_crate};
1437     use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig};
1438     use ast;
1439     use ast::{Attribute_, AttrOuter, MetaWord, Name};
1440     use attr;
1441     use codemap;
1442     use codemap::Spanned;
1443     use ext::mtwt;
1444     use fold::Folder;
1445     use parse;
1446     use parse::token;
1447     use ptr::P;
1448     use util::parser_testing::{string_to_parser};
1449     use util::parser_testing::{string_to_pat, string_to_crate, strs_to_idents};
1450     use visit;
1451     use visit::Visitor;
1452
1453     // a visitor that extracts the paths
1454     // from a given thingy and puts them in a mutable
1455     // array (passed in to the traversal)
1456     #[derive(Clone)]
1457     struct PathExprFinderContext {
1458         path_accumulator: Vec<ast::Path> ,
1459     }
1460
1461     impl<'v> Visitor<'v> for PathExprFinderContext {
1462         fn visit_expr(&mut self, expr: &ast::Expr) {
1463             match expr.node {
1464                 ast::ExprPath(ref p) => {
1465                     self.path_accumulator.push(p.clone());
1466                     // not calling visit_path, but it should be fine.
1467                 }
1468                 _ => visit::walk_expr(self, expr)
1469             }
1470         }
1471     }
1472
1473     // find the variable references in a crate
1474     fn crate_varrefs(the_crate : &ast::Crate) -> Vec<ast::Path> {
1475         let mut path_finder = PathExprFinderContext{path_accumulator:Vec::new()};
1476         visit::walk_crate(&mut path_finder, the_crate);
1477         path_finder.path_accumulator
1478     }
1479
1480     /// A Visitor that extracts the identifiers from a thingy.
1481     // as a side note, I'm starting to want to abstract over these....
1482     struct IdentFinder {
1483         ident_accumulator: Vec<ast::Ident>
1484     }
1485
1486     impl<'v> Visitor<'v> for IdentFinder {
1487         fn visit_ident(&mut self, _: codemap::Span, id: ast::Ident){
1488             self.ident_accumulator.push(id);
1489         }
1490     }
1491
1492     /// Find the idents in a crate
1493     fn crate_idents(the_crate: &ast::Crate) -> Vec<ast::Ident> {
1494         let mut ident_finder = IdentFinder{ident_accumulator: Vec::new()};
1495         visit::walk_crate(&mut ident_finder, the_crate);
1496         ident_finder.ident_accumulator
1497     }
1498
1499     // these following tests are quite fragile, in that they don't test what
1500     // *kind* of failure occurs.
1501
1502     fn test_ecfg() -> ExpansionConfig {
1503         ExpansionConfig::default("test".to_string())
1504     }
1505
1506     // make sure that macros can't escape fns
1507     #[should_fail]
1508     #[test] fn macros_cant_escape_fns_test () {
1509         let src = "fn bogus() {macro_rules! z (() => (3+4));}\
1510                    fn inty() -> i32 { z!() }".to_string();
1511         let sess = parse::new_parse_sess();
1512         let crate_ast = parse::parse_crate_from_source_str(
1513             "<test>".to_string(),
1514             src,
1515             Vec::new(), &sess);
1516         // should fail:
1517         expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast);
1518     }
1519
1520     // make sure that macros can't escape modules
1521     #[should_fail]
1522     #[test] fn macros_cant_escape_mods_test () {
1523         let src = "mod foo {macro_rules! z (() => (3+4));}\
1524                    fn inty() -> i32 { z!() }".to_string();
1525         let sess = parse::new_parse_sess();
1526         let crate_ast = parse::parse_crate_from_source_str(
1527             "<test>".to_string(),
1528             src,
1529             Vec::new(), &sess);
1530         expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast);
1531     }
1532
1533     // macro_use modules should allow macros to escape
1534     #[test] fn macros_can_escape_flattened_mods_test () {
1535         let src = "#[macro_use] mod foo {macro_rules! z (() => (3+4));}\
1536                    fn inty() -> i32 { z!() }".to_string();
1537         let sess = parse::new_parse_sess();
1538         let crate_ast = parse::parse_crate_from_source_str(
1539             "<test>".to_string(),
1540             src,
1541             Vec::new(), &sess);
1542         expand_crate(&sess, test_ecfg(), vec!(), vec!(), crate_ast);
1543     }
1544
1545     fn expand_crate_str(crate_str: String) -> ast::Crate {
1546         let ps = parse::new_parse_sess();
1547         let crate_ast = string_to_parser(&ps, crate_str).parse_crate_mod();
1548         // the cfg argument actually does matter, here...
1549         expand_crate(&ps,test_ecfg(),vec!(),vec!(),crate_ast)
1550     }
1551
1552     // find the pat_ident paths in a crate
1553     fn crate_bindings(the_crate : &ast::Crate) -> Vec<ast::Ident> {
1554         let mut name_finder = PatIdentFinder{ident_accumulator:Vec::new()};
1555         visit::walk_crate(&mut name_finder, the_crate);
1556         name_finder.ident_accumulator
1557     }
1558
1559     #[test] fn macro_tokens_should_match(){
1560         expand_crate_str(
1561             "macro_rules! m((a)=>(13)) ;fn main(){m!(a);}".to_string());
1562     }
1563
1564     // should be able to use a bound identifier as a literal in a macro definition:
1565     #[test] fn self_macro_parsing(){
1566         expand_crate_str(
1567             "macro_rules! foo ((zz) => (287;));
1568             fn f(zz: i32) {foo!(zz);}".to_string()
1569             );
1570     }
1571
1572     // renaming tests expand a crate and then check that the bindings match
1573     // the right varrefs. The specification of the test case includes the
1574     // text of the crate, and also an array of arrays.  Each element in the
1575     // outer array corresponds to a binding in the traversal of the AST
1576     // induced by visit.  Each of these arrays contains a list of indexes,
1577     // interpreted as the varrefs in the varref traversal that this binding
1578     // should match.  So, for instance, in a program with two bindings and
1579     // three varrefs, the array ~[~[1,2],~[0]] would indicate that the first
1580     // binding should match the second two varrefs, and the second binding
1581     // should match the first varref.
1582     //
1583     // Put differently; this is a sparse representation of a boolean matrix
1584     // indicating which bindings capture which identifiers.
1585     //
1586     // Note also that this matrix is dependent on the implicit ordering of
1587     // the bindings and the varrefs discovered by the name-finder and the path-finder.
1588     //
1589     // The comparisons are done post-mtwt-resolve, so we're comparing renamed
1590     // names; differences in marks don't matter any more.
1591     //
1592     // oog... I also want tests that check "bound-identifier-=?". That is,
1593     // not just "do these have the same name", but "do they have the same
1594     // name *and* the same marks"? Understanding this is really pretty painful.
1595     // in principle, you might want to control this boolean on a per-varref basis,
1596     // but that would make things even harder to understand, and might not be
1597     // necessary for thorough testing.
1598     type RenamingTest = (&'static str, Vec<Vec<usize>>, bool);
1599
1600     #[test]
1601     fn automatic_renaming () {
1602         let tests: Vec<RenamingTest> =
1603             vec!(// b & c should get new names throughout, in the expr too:
1604                 ("fn a() -> i32 { let b = 13; let c = b; b+c }",
1605                  vec!(vec!(0,1),vec!(2)), false),
1606                 // both x's should be renamed (how is this causing a bug?)
1607                 ("fn main () {let x: i32 = 13;x;}",
1608                  vec!(vec!(0)), false),
1609                 // the use of b after the + should be renamed, the other one not:
1610                 ("macro_rules! f (($x:ident) => (b + $x)); fn a() -> i32 { let b = 13; f!(b)}",
1611                  vec!(vec!(1)), false),
1612                 // the b before the plus should not be renamed (requires marks)
1613                 ("macro_rules! f (($x:ident) => ({let b=9; ($x + b)})); fn a() -> i32 { f!(b)}",
1614                  vec!(vec!(1)), false),
1615                 // the marks going in and out of letty should cancel, allowing that $x to
1616                 // capture the one following the semicolon.
1617                 // this was an awesome test case, and caught a *lot* of bugs.
1618                 ("macro_rules! letty(($x:ident) => (let $x = 15;));
1619                   macro_rules! user(($x:ident) => ({letty!($x); $x}));
1620                   fn main() -> i32 {user!(z)}",
1621                  vec!(vec!(0)), false)
1622                 );
1623         for (idx,s) in tests.iter().enumerate() {
1624             run_renaming_test(s,idx);
1625         }
1626     }
1627
1628     // no longer a fixme #8062: this test exposes a *potential* bug; our system does
1629     // not behave exactly like MTWT, but a conversation with Matthew Flatt
1630     // suggests that this can only occur in the presence of local-expand, which
1631     // we have no plans to support. ... unless it's needed for item hygiene....
1632     #[ignore]
1633     #[test] fn issue_8062(){
1634         run_renaming_test(
1635             &("fn main() {let hrcoo = 19; macro_rules! getx(()=>(hrcoo)); getx!();}",
1636               vec!(vec!(0)), true), 0)
1637     }
1638
1639     // FIXME #6994:
1640     // the z flows into and out of two macros (g & f) along one path, and one
1641     // (just g) along the other, so the result of the whole thing should
1642     // be "let z_123 = 3; z_123"
1643     #[ignore]
1644     #[test] fn issue_6994(){
1645         run_renaming_test(
1646             &("macro_rules! g (($x:ident) =>
1647               ({macro_rules! f(($y:ident)=>({let $y=3;$x}));f!($x)}));
1648               fn a(){g!(z)}",
1649               vec!(vec!(0)),false),
1650             0)
1651     }
1652
1653     // match variable hygiene. Should expand into
1654     // fn z() {match 8 {x_1 => {match 9 {x_2 | x_2 if x_2 == x_1 => x_2 + x_1}}}}
1655     #[test] fn issue_9384(){
1656         run_renaming_test(
1657             &("macro_rules! bad_macro (($ex:expr) => ({match 9 {x | x if x == $ex => x + $ex}}));
1658               fn z() {match 8 {x => bad_macro!(x)}}",
1659               // NB: the third "binding" is the repeat of the second one.
1660               vec!(vec!(1,3),vec!(0,2),vec!(0,2)),
1661               true),
1662             0)
1663     }
1664
1665     // interpolated nodes weren't getting labeled.
1666     // should expand into
1667     // fn main(){let g1_1 = 13; g1_1}}
1668     #[test] fn pat_expand_issue_15221(){
1669         run_renaming_test(
1670             &("macro_rules! inner ( ($e:pat ) => ($e));
1671               macro_rules! outer ( ($e:pat ) => (inner!($e)));
1672               fn main() { let outer!(g) = 13; g;}",
1673               vec!(vec!(0)),
1674               true),
1675             0)
1676     }
1677
1678     // create a really evil test case where a $x appears inside a binding of $x
1679     // but *shouldn't* bind because it was inserted by a different macro....
1680     // can't write this test case until we have macro-generating macros.
1681
1682     // method arg hygiene
1683     // method expands to fn get_x(&self_0, x_1: i32) {self_0 + self_2 + x_3 + x_1}
1684     #[test] fn method_arg_hygiene(){
1685         run_renaming_test(
1686             &("macro_rules! inject_x (()=>(x));
1687               macro_rules! inject_self (()=>(self));
1688               struct A;
1689               impl A{fn get_x(&self, x: i32) {self + inject_self!() + inject_x!() + x;} }",
1690               vec!(vec!(0),vec!(3)),
1691               true),
1692             0)
1693     }
1694
1695     // ooh, got another bite?
1696     // expands to struct A; impl A {fn thingy(&self_1) {self_1;}}
1697     #[test] fn method_arg_hygiene_2(){
1698         run_renaming_test(
1699             &("struct A;
1700               macro_rules! add_method (($T:ty) =>
1701               (impl $T {  fn thingy(&self) {self;} }));
1702               add_method!(A);",
1703               vec!(vec!(0)),
1704               true),
1705             0)
1706     }
1707
1708     // item fn hygiene
1709     // expands to fn q(x_1: i32){fn g(x_2: i32){x_2 + x_1};}
1710     #[test] fn issue_9383(){
1711         run_renaming_test(
1712             &("macro_rules! bad_macro (($ex:expr) => (fn g(x: i32){ x + $ex }));
1713               fn q(x: i32) { bad_macro!(x); }",
1714               vec!(vec!(1),vec!(0)),true),
1715             0)
1716     }
1717
1718     // closure arg hygiene (ExprClosure)
1719     // expands to fn f(){(|x_1 : i32| {(x_2 + x_1)})(3);}
1720     #[test] fn closure_arg_hygiene(){
1721         run_renaming_test(
1722             &("macro_rules! inject_x (()=>(x));
1723             fn f(){(|x : i32| {(inject_x!() + x)})(3);}",
1724               vec!(vec!(1)),
1725               true),
1726             0)
1727     }
1728
1729     // macro_rules in method position. Sadly, unimplemented.
1730     #[test] fn macro_in_method_posn(){
1731         expand_crate_str(
1732             "macro_rules! my_method (() => (fn thirteen(&self) -> i32 {13}));
1733             struct A;
1734             impl A{ my_method!(); }
1735             fn f(){A.thirteen;}".to_string());
1736     }
1737
1738     // another nested macro
1739     // expands to impl Entries {fn size_hint(&self_1) {self_1;}
1740     #[test] fn item_macro_workaround(){
1741         run_renaming_test(
1742             &("macro_rules! item { ($i:item) => {$i}}
1743               struct Entries;
1744               macro_rules! iterator_impl {
1745               () => { item!( impl Entries { fn size_hint(&self) { self;}});}}
1746               iterator_impl! { }",
1747               vec!(vec!(0)), true),
1748             0)
1749     }
1750
1751     // run one of the renaming tests
1752     fn run_renaming_test(t: &RenamingTest, test_idx: usize) {
1753         let invalid_name = token::special_idents::invalid.name;
1754         let (teststr, bound_connections, bound_ident_check) = match *t {
1755             (ref str,ref conns, bic) => (str.to_string(), conns.clone(), bic)
1756         };
1757         let cr = expand_crate_str(teststr.to_string());
1758         let bindings = crate_bindings(&cr);
1759         let varrefs = crate_varrefs(&cr);
1760
1761         // must be one check clause for each binding:
1762         assert_eq!(bindings.len(),bound_connections.len());
1763         for (binding_idx,shouldmatch) in bound_connections.iter().enumerate() {
1764             let binding_name = mtwt::resolve(bindings[binding_idx]);
1765             let binding_marks = mtwt::marksof(bindings[binding_idx].ctxt, invalid_name);
1766             // shouldmatch can't name varrefs that don't exist:
1767             assert!((shouldmatch.len() == 0) ||
1768                     (varrefs.len() > *shouldmatch.iter().max().unwrap()));
1769             for (idx,varref) in varrefs.iter().enumerate() {
1770                 let print_hygiene_debug_info = |&:| {
1771                     // good lord, you can't make a path with 0 segments, can you?
1772                     let final_varref_ident = match varref.segments.last() {
1773                         Some(pathsegment) => pathsegment.identifier,
1774                         None => panic!("varref with 0 path segments?")
1775                     };
1776                     let varref_name = mtwt::resolve(final_varref_ident);
1777                     let varref_idents : Vec<ast::Ident>
1778                         = varref.segments.iter().map(|s| s.identifier)
1779                         .collect();
1780                     println!("varref #{}: {:?}, resolves to {}",idx, varref_idents, varref_name);
1781                     let string = token::get_ident(final_varref_ident);
1782                     println!("varref's first segment's string: \"{}\"", string.get());
1783                     println!("binding #{}: {}, resolves to {}",
1784                              binding_idx, bindings[binding_idx], binding_name);
1785                     mtwt::with_sctable(|x| mtwt::display_sctable(x));
1786                 };
1787                 if shouldmatch.contains(&idx) {
1788                     // it should be a path of length 1, and it should
1789                     // be free-identifier=? or bound-identifier=? to the given binding
1790                     assert_eq!(varref.segments.len(),1);
1791                     let varref_name = mtwt::resolve(varref.segments[0].identifier);
1792                     let varref_marks = mtwt::marksof(varref.segments[0]
1793                                                            .identifier
1794                                                            .ctxt,
1795                                                      invalid_name);
1796                     if !(varref_name==binding_name) {
1797                         println!("uh oh, should match but doesn't:");
1798                         print_hygiene_debug_info();
1799                     }
1800                     assert_eq!(varref_name,binding_name);
1801                     if bound_ident_check {
1802                         // we're checking bound-identifier=?, and the marks
1803                         // should be the same, too:
1804                         assert_eq!(varref_marks,binding_marks.clone());
1805                     }
1806                 } else {
1807                     let varref_name = mtwt::resolve(varref.segments[0].identifier);
1808                     let fail = (varref.segments.len() == 1)
1809                         && (varref_name == binding_name);
1810                     // temp debugging:
1811                     if fail {
1812                         println!("failure on test {}",test_idx);
1813                         println!("text of test case: \"{}\"", teststr);
1814                         println!("");
1815                         println!("uh oh, matches but shouldn't:");
1816                         print_hygiene_debug_info();
1817                     }
1818                     assert!(!fail);
1819                 }
1820             }
1821         }
1822     }
1823
1824     #[test] fn fmt_in_macro_used_inside_module_macro() {
1825         let crate_str = "macro_rules! fmt_wrap(($b:expr)=>($b.to_string()));
1826 macro_rules! foo_module (() => (mod generated { fn a() { let xx = 147; fmt_wrap!(xx);}}));
1827 foo_module!();
1828 ".to_string();
1829         let cr = expand_crate_str(crate_str);
1830         // find the xx binding
1831         let bindings = crate_bindings(&cr);
1832         let cxbinds: Vec<&ast::Ident> =
1833             bindings.iter().filter(|b| {
1834                 let ident = token::get_ident(**b);
1835                 let string = ident.get();
1836                 "xx" == string
1837             }).collect();
1838         let cxbinds: &[&ast::Ident] = &cxbinds[];
1839         let cxbind = match cxbinds {
1840             [b] => b,
1841             _ => panic!("expected just one binding for ext_cx")
1842         };
1843         let resolved_binding = mtwt::resolve(*cxbind);
1844         let varrefs = crate_varrefs(&cr);
1845
1846         // the xx binding should bind all of the xx varrefs:
1847         for (idx,v) in varrefs.iter().filter(|p| {
1848             p.segments.len() == 1
1849             && "xx" == token::get_ident(p.segments[0].identifier).get()
1850         }).enumerate() {
1851             if mtwt::resolve(v.segments[0].identifier) != resolved_binding {
1852                 println!("uh oh, xx binding didn't match xx varref:");
1853                 println!("this is xx varref \\# {}", idx);
1854                 println!("binding: {}", cxbind);
1855                 println!("resolves to: {}", resolved_binding);
1856                 println!("varref: {}", v.segments[0].identifier);
1857                 println!("resolves to: {}",
1858                          mtwt::resolve(v.segments[0].identifier));
1859                 mtwt::with_sctable(|x| mtwt::display_sctable(x));
1860             }
1861             assert_eq!(mtwt::resolve(v.segments[0].identifier),
1862                        resolved_binding);
1863         };
1864     }
1865
1866     #[test]
1867     fn pat_idents(){
1868         let pat = string_to_pat(
1869             "(a,Foo{x:c @ (b,9),y:Bar(4,d)})".to_string());
1870         let idents = pattern_bindings(&*pat);
1871         assert_eq!(idents, strs_to_idents(vec!("a","c","b","d")));
1872     }
1873
1874     // test the list of identifier patterns gathered by the visitor. Note that
1875     // 'None' is listed as an identifier pattern because we don't yet know that
1876     // it's the name of a 0-ary variant, and that 'i' appears twice in succession.
1877     #[test]
1878     fn crate_bindings_test(){
1879         let the_crate = string_to_crate("fn main (a: i32) -> i32 {|b| {
1880         match 34 {None => 3, Some(i) | i => j, Foo{k:z,l:y} => \"banana\"}} }".to_string());
1881         let idents = crate_bindings(&the_crate);
1882         assert_eq!(idents, strs_to_idents(vec!("a","b","None","i","i","z","y")));
1883     }
1884
1885     // test the IdentRenamer directly
1886     #[test]
1887     fn ident_renamer_test () {
1888         let the_crate = string_to_crate("fn f(x: i32){let x = x; x}".to_string());
1889         let f_ident = token::str_to_ident("f");
1890         let x_ident = token::str_to_ident("x");
1891         let int_ident = token::str_to_ident("i32");
1892         let renames = vec!((x_ident,Name(16)));
1893         let mut renamer = IdentRenamer{renames: &renames};
1894         let renamed_crate = renamer.fold_crate(the_crate);
1895         let idents = crate_idents(&renamed_crate);
1896         let resolved : Vec<ast::Name> = idents.iter().map(|id| mtwt::resolve(*id)).collect();
1897         assert_eq!(resolved,vec!(f_ident.name,Name(16),int_ident.name,Name(16),Name(16),Name(16)));
1898     }
1899
1900     // test the PatIdentRenamer; only PatIdents get renamed
1901     #[test]
1902     fn pat_ident_renamer_test () {
1903         let the_crate = string_to_crate("fn f(x: i32){let x = x; x}".to_string());
1904         let f_ident = token::str_to_ident("f");
1905         let x_ident = token::str_to_ident("x");
1906         let int_ident = token::str_to_ident("i32");
1907         let renames = vec!((x_ident,Name(16)));
1908         let mut renamer = PatIdentRenamer{renames: &renames};
1909         let renamed_crate = renamer.fold_crate(the_crate);
1910         let idents = crate_idents(&renamed_crate);
1911         let resolved : Vec<ast::Name> = idents.iter().map(|id| mtwt::resolve(*id)).collect();
1912         let x_name = x_ident.name;
1913         assert_eq!(resolved,vec!(f_ident.name,Name(16),int_ident.name,Name(16),x_name,x_name));
1914     }
1915
1916
1917 }