Skip to content

Commit

Permalink
Gensym arguments for format macro
Browse files Browse the repository at this point in the history
  • Loading branch information
zofrex committed Apr 24, 2018
1 parent d80797b commit 27b0f1e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/libsyntax_ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ impl<'a, 'b> Context<'a, 'b> {
let mut pats = Vec::new();
let mut heads = Vec::new();

let names_pos: Vec<_> = (0..self.args.len()).map(|i| {
self.ecx.ident_of(&format!("arg{}", i)).gensym()
}).collect();

// First, build up the static array which will become our precompiled
// format "string"
let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces);
Expand All @@ -560,7 +564,7 @@ impl<'a, 'b> Context<'a, 'b> {
// of each variable because we don't want to move out of the arguments
// passed to this function.
for (i, e) in self.args.into_iter().enumerate() {
let name = self.ecx.ident_of(&format!("__arg{}", i));
let name = names_pos[i];
let span =
DUMMY_SP.with_ctxt(e.span.ctxt().apply_mark(self.ecx.current_expansion.mark));
pats.push(self.ecx.pat_ident(span, name));
Expand All @@ -570,14 +574,12 @@ impl<'a, 'b> Context<'a, 'b> {
heads.push(self.ecx.expr_addr_of(e.span, e));
}
for pos in self.count_args {
let name = self.ecx.ident_of(&match pos {
Exact(i) => format!("__arg{}", i),
_ => panic!("should never happen"),
});
let span = match pos {
Exact(i) => spans_pos[i],
let index = match pos {
Exact(i) => i,
_ => panic!("should never happen"),
};
let name = names_pos[index];
let span = spans_pos[index];
counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name));
}

Expand Down

0 comments on commit 27b0f1e

Please sign in to comment.