Skip to content

Commit b8cd7f7

Browse files
committed
add PatIdentRenamer
1 parent 9fdaa94 commit b8cd7f7

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use codemap;
2121
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
2222
use crateid::CrateId;
2323
use ext::base::*;
24+
use fold;
2425
use fold::*;
2526
use parse;
2627
use parse::token::{fresh_mark, fresh_name, intern};
@@ -856,15 +857,36 @@ impl<'a> Folder for IdentRenamer<'a> {
856857
}
857858
}
858859

859-
fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
860-
/* this discards information in the case of macro-defining macros */
861-
Span {
862-
lo: sp.lo,
863-
hi: sp.hi,
864-
expn_info: cx.backtrace(),
860+
/// A tree-folder that applies every rename in its list to
861+
/// the idents that are in PatIdent patterns. This is more narrowly
862+
/// focused than IdentRenamer, and is needed for FnDecl,
863+
/// where we want to rename the args but not the fn name or the generics etc.
864+
pub struct PatIdentRenamer<'a> {
865+
renames: &'a mtwt::RenameList,
866+
}
867+
868+
impl<'a> Folder for PatIdentRenamer<'a> {
869+
fn fold_pat(&mut self, pat: Gc<ast::Pat>) -> Gc<ast::Pat> {
870+
match pat.node {
871+
ast::PatIdent(binding_mode, Spanned{span: ref sp, node: id}, ref sub) => {
872+
let new_ident = Ident{name: id.name,
873+
ctxt: mtwt::new_renames(self.renames, id.ctxt)};
874+
let new_node =
875+
ast::PatIdent(binding_mode,
876+
Spanned{span: self.new_span(*sp), node: new_ident},
877+
sub.map(|p| self.fold_pat(p)));
878+
box(GC) ast::Pat {
879+
id: pat.id,
880+
span: self.new_span(pat.span),
881+
node: new_node,
882+
}
883+
},
884+
_ => noop_fold_pat(pat, self)
885+
}
865886
}
866887
}
867888

889+
/// A tree-folder that performs macro expansion
868890
pub struct MacroExpander<'a, 'b> {
869891
pub extsbox: SyntaxEnv,
870892
pub cx: &'a mut ExtCtxt<'b>,
@@ -900,6 +922,15 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
900922
}
901923
}
902924

925+
fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
926+
/* this discards information in the case of macro-defining macros */
927+
Span {
928+
lo: sp.lo,
929+
hi: sp.hi,
930+
expn_info: cx.backtrace(),
931+
}
932+
}
933+
903934
pub struct ExpansionConfig {
904935
pub deriving_hash_type_parameter: bool,
905936
pub crate_id: CrateId,

0 commit comments

Comments
 (0)