From a9a24d510698c3aafd6f7e1b974b70ac94e0a18b Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 1 Nov 2021 11:56:38 -0500 Subject: Don't destructure args tuple in format_args! --- compiler/rustc_builtin_macros/src/format.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'compiler/rustc_builtin_macros') diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index f0056cb7976..580c3724058 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -763,13 +763,8 @@ impl<'a, 'b> Context<'a, 'b> { let mut locals = Vec::with_capacity((0..self.args.len()).map(|i| self.arg_unique_types[i].len()).sum()); let mut counts = Vec::with_capacity(self.count_args.len()); - let mut pats = Vec::with_capacity(self.args.len()); let mut heads = Vec::with_capacity(self.args.len()); - let names_pos: Vec<_> = (0..self.args.len()) - .map(|i| Ident::from_str_and_span(&format!("arg{}", i), self.macsp)) - .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); @@ -787,11 +782,8 @@ 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 = names_pos[i]; - let span = self.ecx.with_def_site_ctxt(e.span); - pats.push(self.ecx.pat_ident(span, name)); for arg_ty in self.arg_unique_types[i].iter() { - locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name)); + locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, i)); } heads.push(self.ecx.expr_addr_of(e.span, e)); } @@ -800,9 +792,8 @@ impl<'a, 'b> Context<'a, 'b> { 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)); + counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, index)); } // Now create a vector containing all the arguments @@ -838,7 +829,7 @@ impl<'a, 'b> Context<'a, 'b> { // But the nested match expression is proved to perform not as well // as series of let's; the first approach does. let args_match = { - let pat = self.ecx.pat_tuple(self.macsp, pats); + let pat = self.ecx.pat_ident(self.macsp, Ident::new(sym::_args, self.macsp)); let arm = self.ecx.arm(self.macsp, pat, args_array); let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads)); self.ecx.expr_match(self.macsp, head, vec![arm]) @@ -877,10 +868,11 @@ impl<'a, 'b> Context<'a, 'b> { macsp: Span, mut sp: Span, ty: &ArgumentType, - arg: Ident, + arg_index: usize, ) -> P { sp = ecx.with_def_site_ctxt(sp); - let arg = ecx.expr_ident(sp, arg); + let arg = ecx.expr_ident(sp, Ident::new(sym::_args, sp)); + let arg = ecx.expr(sp, ast::ExprKind::Field(arg, Ident::new(sym::integer(arg_index), sp))); let trait_ = match *ty { Placeholder(trait_) if trait_ == "" => return DummyResult::raw_expr(sp, true), Placeholder(trait_) => trait_, -- cgit 1.4.1-3-g733a5 From 9f6a58e86b00a8ff22f852c1162e3d3107d0755b Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Mon, 1 Nov 2021 14:12:17 -0500 Subject: Factor out some Vecs --- compiler/rustc_builtin_macros/src/format.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'compiler/rustc_builtin_macros') diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 580c3724058..52b00a2bc74 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -760,9 +760,9 @@ impl<'a, 'b> Context<'a, 'b> { /// Actually builds the expression which the format_args! block will be /// expanded to. fn into_expr(self) -> P { - let mut locals = - Vec::with_capacity((0..self.args.len()).map(|i| self.arg_unique_types[i].len()).sum()); - let mut counts = Vec::with_capacity(self.count_args.len()); + let mut args = Vec::with_capacity( + self.arg_unique_types.iter().map(|v| v.len()).sum::() + self.count_args.len(), + ); let mut heads = Vec::with_capacity(self.args.len()); // First, build up the static array which will become our precompiled @@ -783,7 +783,7 @@ impl<'a, 'b> Context<'a, 'b> { // passed to this function. for (i, e) in self.args.into_iter().enumerate() { for arg_ty in self.arg_unique_types[i].iter() { - locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, i)); + args.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, i)); } heads.push(self.ecx.expr_addr_of(e.span, e)); } @@ -793,13 +793,10 @@ impl<'a, 'b> Context<'a, 'b> { _ => panic!("should never happen"), }; let span = spans_pos[index]; - counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, index)); + args.push(Context::format_arg(self.ecx, self.macsp, span, &Count, index)); } - // Now create a vector containing all the arguments - let args = locals.into_iter().chain(counts.into_iter()); - - let args_array = self.ecx.expr_vec(self.macsp, args.collect()); + let args_array = self.ecx.expr_vec(self.macsp, args); // Constructs an AST equivalent to: // -- cgit 1.4.1-3-g733a5