Skip to content

Commit 60952bc

Browse files
committed
Auto merge of #90485 - camsteffen:fmt-args-less-bind, r=m-ou-se
Don't destructure args tuple in format_args! This allows Clippy to parse the HIR more simply since `arg0` is changed to `_args.0`. (cc rust-lang/rust-clippy#7843). From rustc's perspective, I think this is something between a lateral move and a tiny improvement since there are fewer bindings. r? `@m-ou-se`
2 parents c57704f + 8e21f3a commit 60952bc

File tree

8 files changed

+21
-37
lines changed

8 files changed

+21
-37
lines changed

compiler/rustc_builtin_macros/src/format.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -760,16 +760,11 @@ impl<'a, 'b> Context<'a, 'b> {
760760
/// Actually builds the expression which the format_args! block will be
761761
/// expanded to.
762762
fn into_expr(self) -> P<ast::Expr> {
763-
let mut locals =
764-
Vec::with_capacity((0..self.args.len()).map(|i| self.arg_unique_types[i].len()).sum());
765-
let mut counts = Vec::with_capacity(self.count_args.len());
766-
let mut pats = Vec::with_capacity(self.args.len());
763+
let mut args = Vec::with_capacity(
764+
self.arg_unique_types.iter().map(|v| v.len()).sum::<usize>() + self.count_args.len(),
765+
);
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+
args.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,15 +792,11 @@ 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+
args.push(Context::format_arg(self.ecx, self.macsp, span, &Count, index));
806797
}
807798

808-
// Now create a vector containing all the arguments
809-
let args = locals.into_iter().chain(counts.into_iter());
810-
811-
let args_array = self.ecx.expr_vec(self.macsp, args.collect());
799+
let args_array = self.ecx.expr_vec(self.macsp, args);
812800

813801
// Constructs an AST equivalent to:
814802
//
@@ -838,7 +826,7 @@ impl<'a, 'b> Context<'a, 'b> {
838826
// But the nested match expression is proved to perform not as well
839827
// as series of let's; the first approach does.
840828
let args_match = {
841-
let pat = self.ecx.pat_tuple(self.macsp, pats);
829+
let pat = self.ecx.pat_ident(self.macsp, Ident::new(sym::_args, self.macsp));
842830
let arm = self.ecx.arm(self.macsp, pat, args_array);
843831
let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
844832
self.ecx.expr_match(self.macsp, head, vec![arm])
@@ -877,10 +865,11 @@ impl<'a, 'b> Context<'a, 'b> {
877865
macsp: Span,
878866
mut sp: Span,
879867
ty: &ArgumentType,
880-
arg: Ident,
868+
arg_index: usize,
881869
) -> P<ast::Expr> {
882870
sp = ecx.with_def_site_ctxt(sp);
883-
let arg = ecx.expr_ident(sp, arg);
871+
let arg = ecx.expr_ident(sp, Ident::new(sym::_args, sp));
872+
let arg = ecx.expr(sp, ast::ExprKind::Field(arg, Ident::new(sym::integer(arg_index), sp)));
884873
let trait_ = match *ty {
885874
Placeholder(trait_) if trait_ == "<invalid>" => return DummyResult::raw_expr(sp, true),
886875
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();

src/tools/clippy/clippy_utils/src/higher.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#![deny(clippy::missing_docs_in_private_items)]
44

55
use crate::ty::is_type_diagnostic_item;
6-
use crate::{is_expn_of, last_path_segment, match_def_path, path_to_local_id, paths};
6+
use crate::{is_expn_of, last_path_segment, match_def_path, paths};
77
use if_chain::if_chain;
88
use rustc_ast::ast::{self, LitKind};
99
use rustc_hir as hir;
1010
use rustc_hir::{
11-
Arm, Block, BorrowKind, Expr, ExprKind, HirId, LoopSource, MatchSource, Node, Pat, PatKind, QPath, StmtKind, UnOp,
11+
Arm, Block, BorrowKind, Expr, ExprKind, HirId, LoopSource, MatchSource, Node, Pat, QPath, StmtKind, UnOp,
1212
};
1313
use rustc_lint::LateContext;
1414
use rustc_span::{sym, symbol, ExpnKind, Span, Symbol};
@@ -513,8 +513,6 @@ pub struct FormatArgsExpn<'tcx> {
513513
pub format_string_parts: &'tcx [Expr<'tcx>],
514514
/// Symbols corresponding to [`Self::format_string_parts`]
515515
pub format_string_symbols: Vec<Symbol>,
516-
/// Match arm patterns, the `arg0`, etc. from the next field `args`
517-
pub arg_names: &'tcx [Pat<'tcx>],
518516
/// Expressions like `ArgumentV1::new(arg0, Debug::fmt)`
519517
pub args: &'tcx [Expr<'tcx>],
520518
/// The final argument passed to `Arguments::new_v1_formatted`, if applicable
@@ -559,15 +557,13 @@ impl FormatArgsExpn<'tcx> {
559557
_ => None,
560558
})
561559
.collect();
562-
if let PatKind::Tuple(arg_names, None) = arm.pat.kind;
563560
if let ExprKind::Array(args) = arm.body.kind;
564561
then {
565562
Some(FormatArgsExpn {
566563
format_string_span: strs_ref.span,
567564
value_args,
568565
format_string_parts,
569566
format_string_symbols,
570-
arg_names,
571567
args,
572568
fmt_expr,
573569
})
@@ -594,10 +590,8 @@ impl FormatArgsExpn<'tcx> {
594590
if let Ok(i) = usize::try_from(position);
595591
let arg = &self.args[i];
596592
if let ExprKind::Call(_, [arg_name, _]) = arg.kind;
597-
if let Some(j) = self
598-
.arg_names
599-
.iter()
600-
.position(|pat| path_to_local_id(arg_name, pat.hir_id));
593+
if let ExprKind::Field(_, j) = arg_name.kind;
594+
if let Ok(j) = j.name.as_str().parse::<usize>();
601595
then {
602596
Some(FormatArgsArg { value: self.value_args[j], arg, fmt: Some(fmt) })
603597
} else {

0 commit comments

Comments
 (0)