Skip to content

Commit a9a24d5

Browse files
committed
Don't destructure args tuple in format_args!
1 parent 3326f19 commit a9a24d5

File tree

7 files changed

+13
-20
lines changed

7 files changed

+13
-20
lines changed

compiler/rustc_builtin_macros/src/format.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,8 @@ impl<'a, 'b> Context<'a, 'b> {
763763
let mut locals =
764764
Vec::with_capacity((0..self.args.len()).map(|i| self.arg_unique_types[i].len()).sum());
765765
let mut counts = Vec::with_capacity(self.count_args.len());
766-
let mut pats = Vec::with_capacity(self.args.len());
767766
let mut heads = Vec::with_capacity(self.args.len());
768767

769-
let names_pos: Vec<_> = (0..self.args.len())
770-
.map(|i| Ident::from_str_and_span(&format!("arg{}", i), self.macsp))
771-
.collect();
772-
773768
// First, build up the static array which will become our precompiled
774769
// format "string"
775770
let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces);
@@ -787,11 +782,8 @@ impl<'a, 'b> Context<'a, 'b> {
787782
// of each variable because we don't want to move out of the arguments
788783
// passed to this function.
789784
for (i, e) in self.args.into_iter().enumerate() {
790-
let name = names_pos[i];
791-
let span = self.ecx.with_def_site_ctxt(e.span);
792-
pats.push(self.ecx.pat_ident(span, name));
793785
for arg_ty in self.arg_unique_types[i].iter() {
794-
locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name));
786+
locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, i));
795787
}
796788
heads.push(self.ecx.expr_addr_of(e.span, e));
797789
}
@@ -800,9 +792,8 @@ impl<'a, 'b> Context<'a, 'b> {
800792
Exact(i) => i,
801793
_ => panic!("should never happen"),
802794
};
803-
let name = names_pos[index];
804795
let span = spans_pos[index];
805-
counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name));
796+
counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, index));
806797
}
807798

808799
// Now create a vector containing all the arguments
@@ -838,7 +829,7 @@ impl<'a, 'b> Context<'a, 'b> {
838829
// But the nested match expression is proved to perform not as well
839830
// as series of let's; the first approach does.
840831
let args_match = {
841-
let pat = self.ecx.pat_tuple(self.macsp, pats);
832+
let pat = self.ecx.pat_ident(self.macsp, Ident::new(sym::_args, self.macsp));
842833
let arm = self.ecx.arm(self.macsp, pat, args_array);
843834
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
844835
self.ecx.expr_match(self.macsp, head, vec![arm])
@@ -877,10 +868,11 @@ impl<'a, 'b> Context<'a, 'b> {
877868
macsp: Span,
878869
mut sp: Span,
879870
ty: &ArgumentType,
880-
arg: Ident,
871+
arg_index: usize,
881872
) -> P<ast::Expr> {
882873
sp = ecx.with_def_site_ctxt(sp);
883-
let arg = ecx.expr_ident(sp, arg);
874+
let arg = ecx.expr_ident(sp, Ident::new(sym::_args, sp));
875+
let arg = ecx.expr(sp, ast::ExprKind::Field(arg, Ident::new(sym::integer(arg_index), sp)));
884876
let trait_ = match *ty {
885877
Placeholder(trait_) if trait_ == "<invalid>" => return DummyResult::raw_expr(sp, true),
886878
Placeholder(trait_) => trait_,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ symbols! {
271271
__S,
272272
__next,
273273
__try_var,
274+
_args,
274275
_d,
275276
_e,
276277
_task_context,

src/test/pretty/dollar-crate.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{
1313
::std::io::_print(::core::fmt::Arguments::new_v1(&["rust\n"],
1414
&match () {
15-
() => [],
15+
_args => [],
1616
}));
1717
};
1818
}

src/test/pretty/issue-4264.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
as
4646
())
4747
{
48-
()
48+
_args
4949
=>
5050
([]
5151
as

src/test/ui/attributes/key-value-expansion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ error: unexpected token: `{
1919
let res =
2020
::alloc::fmt::format(::core::fmt::Arguments::new_v1(&[""],
2121
&match (&"u8",) {
22-
(arg0,) =>
23-
[::core::fmt::ArgumentV1::new(arg0,
22+
_args =>
23+
[::core::fmt::ArgumentV1::new(_args.0,
2424
::core::fmt::Display::fmt)],
2525
}));
2626
res

src/test/ui/closures/print/closure-print-generic-trim-off-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `[mod1::f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#25t, extern "rust-call" fn(()), _#26t]]`
12+
found closure `[mod1::f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#22t, extern "rust-call" fn(()), _#23t]]`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

src/test/ui/closures/print/closure-print-generic-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `[f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#25t, extern "rust-call" fn(()), _#26t]]`
12+
found closure `[f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#22t, extern "rust-call" fn(()), _#23t]]`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

0 commit comments

Comments
 (0)