Skip to content

Instantly share code, notes, and snippets.

@kemurphy
Last active December 20, 2015 06:39
Show Gist options
  • Save kemurphy/6087201 to your computer and use it in GitHub Desktop.
Save kemurphy/6087201 to your computer and use it in GitHub Desktop.
pub fn rewrite_tuple_struct_or_variant_calls(c: &Crate, dm: DefMap) -> @Crate {
fn fold_expr(e: &ast::expr_, fld: @fold::ast_fold, dm: DefMap) -> Option<ast::expr_> {
let fold_unnamed_field = |x| {
ast::Field {
ident: special_idents::unnamed_field,
expr: fld.fold_expr(x),
span: fld.new_span(x.span),
}
};
let f: @ast::expr;
let args: &~[@ast::expr];
match *e {
expr_call(f_, ref args_, _) => {
f = f_;
args = args_;
}
_ => { return None; }
};
let path;
match f.node {
expr_path(ref p) => {
path = p;
}
_ => { return None; }
};
match dm.find(&f.id) {
Some(&v) => {
match v {
ast::def_variant(*) |ast::def_struct(*) => {
return Some(expr_struct(
fld.fold_path(path),
args.map(|x| fold_unnamed_field(*x)),
None
));
}
_ => {}
}
}
_ => {}
}
None
};
let rewriter = @fold::AstFoldFns {
fold_expr: |e, s, fld| {
let f = fold_expr(e, fld, dm);
(fold::noop_fold_expr(if f.is_some() { f.get_ref() } else { e }, fld), s)
},
.. *fold::default_ast_fold()
};
@fold::make_fold(rewriter).fold_crate(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment