Skip to content

Commit 27b0f1e

Browse files
committed
Gensym arguments for format macro
1 parent d80797b commit 27b0f1e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/libsyntax_ext/format.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ impl<'a, 'b> Context<'a, 'b> {
543543
let mut pats = Vec::new();
544544
let mut heads = Vec::new();
545545

546+
let names_pos: Vec<_> = (0..self.args.len()).map(|i| {
547+
self.ecx.ident_of(&format!("arg{}", i)).gensym()
548+
}).collect();
549+
546550
// First, build up the static array which will become our precompiled
547551
// format "string"
548552
let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces);
@@ -560,7 +564,7 @@ impl<'a, 'b> Context<'a, 'b> {
560564
// of each variable because we don't want to move out of the arguments
561565
// passed to this function.
562566
for (i, e) in self.args.into_iter().enumerate() {
563-
let name = self.ecx.ident_of(&format!("__arg{}", i));
567+
let name = names_pos[i];
564568
let span =
565569
DUMMY_SP.with_ctxt(e.span.ctxt().apply_mark(self.ecx.current_expansion.mark));
566570
pats.push(self.ecx.pat_ident(span, name));
@@ -570,14 +574,12 @@ impl<'a, 'b> Context<'a, 'b> {
570574
heads.push(self.ecx.expr_addr_of(e.span, e));
571575
}
572576
for pos in self.count_args {
573-
let name = self.ecx.ident_of(&match pos {
574-
Exact(i) => format!("__arg{}", i),
575-
_ => panic!("should never happen"),
576-
});
577-
let span = match pos {
578-
Exact(i) => spans_pos[i],
577+
let index = match pos {
578+
Exact(i) => i,
579579
_ => panic!("should never happen"),
580580
};
581+
let name = names_pos[index];
582+
let span = spans_pos[index];
581583
counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name));
582584
}
583585

0 commit comments

Comments
 (0)