Skip to content

Commit 627b48c

Browse files
committed
Use super let in format_args!().
This makes it posisble to do: let f = format_args!("Hello, {name}!");
1 parent 151c5b2 commit 627b48c

File tree

26 files changed

+200
-388
lines changed

26 files changed

+200
-388
lines changed

compiler/rustc_ast_lowering/src/format.rs

+46-115
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use core::ops::ControlFlow;
21
use std::borrow::Cow;
32

4-
use rustc_ast::visit::Visitor;
53
use rustc_ast::*;
64
use rustc_data_structures::fx::FxIndexMap;
75
use rustc_hir as hir;
@@ -476,77 +474,32 @@ fn expand_format_args<'hir>(
476474
return hir::ExprKind::Call(new, new_args);
477475
}
478476

479-
// If the args array contains exactly all the original arguments once,
480-
// in order, we can use a simple array instead of a `match` construction.
481-
// However, if there's a yield point in any argument except the first one,
482-
// we don't do this, because an Argument cannot be kept across yield points.
483-
//
484-
// This is an optimization, speeding up compilation about 1-2% in some cases.
485-
// See https://github.com/rust-lang/rust/pull/106770#issuecomment-1380790609
486-
let use_simple_array = argmap.len() == arguments.len()
487-
&& argmap.iter().enumerate().all(|(i, (&(j, _), _))| i == j)
488-
&& arguments.iter().skip(1).all(|arg| !may_contain_yield_point(&arg.expr));
489-
490-
let args = if arguments.is_empty() {
477+
let (let_statements, args) = if arguments.is_empty() {
491478
// Generate:
492-
// &<core::fmt::Argument>::none()
493-
//
494-
// Note:
495-
// `none()` just returns `[]`. We use `none()` rather than `[]` to limit the lifetime.
496-
//
497-
// This makes sure that this still fails to compile, even when the argument is inlined:
498-
//
499-
// ```
500-
// let f = format_args!("{}", "a");
501-
// println!("{f}"); // error E0716
502-
// ```
503-
//
504-
// Cases where keeping the object around is allowed, such as `format_args!("a")`,
505-
// are handled above by the `allow_const` case.
506-
let none_fn = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
507-
macsp,
508-
hir::LangItem::FormatArgument,
509-
sym::none,
510-
));
511-
let none = ctx.expr_call(macsp, none_fn, &[]);
512-
ctx.expr(macsp, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, none))
513-
} else if use_simple_array {
514-
// Generate:
515-
// &[
516-
// <core::fmt::Argument>::new_display(&arg0),
517-
// <core::fmt::Argument>::new_lower_hex(&arg1),
518-
// <core::fmt::Argument>::new_debug(&arg2),
519-
// …
520-
// ]
521-
let elements = ctx.arena.alloc_from_iter(arguments.iter().zip(argmap).map(
522-
|(arg, ((_, ty), placeholder_span))| {
523-
let placeholder_span =
524-
placeholder_span.unwrap_or(arg.expr.span).with_ctxt(macsp.ctxt());
525-
let arg_span = match arg.kind {
526-
FormatArgumentKind::Captured(_) => placeholder_span,
527-
_ => arg.expr.span.with_ctxt(macsp.ctxt()),
528-
};
529-
let arg = ctx.lower_expr(&arg.expr);
530-
let ref_arg = ctx.arena.alloc(ctx.expr(
531-
arg_span,
532-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg),
533-
));
534-
make_argument(ctx, placeholder_span, ref_arg, ty)
535-
},
536-
));
537-
ctx.expr_array_ref(macsp, elements)
479+
// []
480+
(None, ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Array(&[]))))
538481
} else {
539482
// Generate:
540-
// &match (&arg0, &arg1, &…) {
541-
// args => [
542-
// <core::fmt::Argument>::new_display(args.0),
543-
// <core::fmt::Argument>::new_lower_hex(args.1),
544-
// <core::fmt::Argument>::new_debug(args.0),
545-
// …
546-
// ]
547-
// }
483+
// super let args = (&arg0, &arg1, &…);
548484
let args_ident = Ident::new(sym::args, macsp);
549485
let (args_pat, args_hir_id) = ctx.pat_ident(macsp, args_ident);
486+
let elements = ctx.arena.alloc_from_iter(arguments.iter().map(|arg| {
487+
let arg_expr = ctx.lower_expr(&arg.expr);
488+
ctx.expr(
489+
arg.expr.span.with_ctxt(macsp.ctxt()),
490+
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr),
491+
)
492+
}));
493+
let args_tuple = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Tup(elements)));
494+
let let_statement_1 = ctx.stmt_super_let_pat(macsp, args_pat, Some(args_tuple));
495+
496+
// Generate:
497+
// super let args = [
498+
// <core::fmt::Argument>::new_display(args.0),
499+
// <core::fmt::Argument>::new_lower_hex(args.1),
500+
// <core::fmt::Argument>::new_debug(args.0),
501+
// …
502+
// ];
550503
let args = ctx.arena.alloc_from_iter(argmap.iter().map(
551504
|(&(arg_index, ty), &placeholder_span)| {
552505
let arg = &arguments[arg_index];
@@ -567,29 +520,21 @@ fn expand_format_args<'hir>(
567520
make_argument(ctx, placeholder_span, arg, ty)
568521
},
569522
));
570-
let elements = ctx.arena.alloc_from_iter(arguments.iter().map(|arg| {
571-
let arg_expr = ctx.lower_expr(&arg.expr);
572-
ctx.expr(
573-
arg.expr.span.with_ctxt(macsp.ctxt()),
574-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr),
575-
)
576-
}));
577-
let args_tuple = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Tup(elements)));
578-
let array = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Array(args)));
579-
let match_arms = ctx.arena.alloc_from_iter([ctx.arm(args_pat, array)]);
580-
let match_expr = ctx.arena.alloc(ctx.expr_match(
581-
macsp,
582-
args_tuple,
583-
match_arms,
584-
hir::MatchSource::FormatArgs,
585-
));
586-
ctx.expr(
587-
macsp,
588-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, match_expr),
523+
let args = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Array(args)));
524+
let (args_pat, args_hir_id) = ctx.pat_ident(macsp, args_ident);
525+
let let_statement_2 = ctx.stmt_super_let_pat(macsp, args_pat, Some(args));
526+
(
527+
Some([let_statement_1, let_statement_2]),
528+
ctx.arena.alloc(ctx.expr_ident_mut(macsp, args_ident, args_hir_id)),
589529
)
590530
};
591531

592-
if let Some(format_options) = format_options {
532+
// Generate:
533+
// &args
534+
let args =
535+
ctx.expr(macsp, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, args));
536+
537+
let call = if let Some(format_options) = format_options {
593538
// Generate:
594539
// <core::fmt::Arguments>::new_v1_formatted(
595540
// lit_pieces,
@@ -632,35 +577,21 @@ fn expand_format_args<'hir>(
632577
));
633578
let new_args = ctx.arena.alloc_from_iter([lit_pieces, args]);
634579
hir::ExprKind::Call(new_v1, new_args)
635-
}
636-
}
637-
638-
fn may_contain_yield_point(e: &ast::Expr) -> bool {
639-
struct MayContainYieldPoint;
640-
641-
impl Visitor<'_> for MayContainYieldPoint {
642-
type Result = ControlFlow<()>;
643-
644-
fn visit_expr(&mut self, e: &ast::Expr) -> ControlFlow<()> {
645-
if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_) = e.kind {
646-
ControlFlow::Break(())
647-
} else {
648-
visit::walk_expr(self, e)
649-
}
650-
}
651-
652-
fn visit_mac_call(&mut self, _: &ast::MacCall) -> ControlFlow<()> {
653-
// Macros should be expanded at this point.
654-
unreachable!("unexpanded macro in ast lowering");
655-
}
580+
};
656581

657-
fn visit_item(&mut self, _: &ast::Item) -> ControlFlow<()> {
658-
// Do not recurse into nested items.
659-
ControlFlow::Continue(())
660-
}
582+
if let Some(let_statements) = let_statements {
583+
// Generate:
584+
// {
585+
// super let …
586+
// super let …
587+
// <core::fmt::Arguments>::new_…(…)
588+
// }
589+
let call = ctx.arena.alloc(ctx.expr(macsp, call));
590+
let block = ctx.block_all(macsp, ctx.arena.alloc_from_iter(let_statements), Some(call));
591+
hir::ExprKind::Block(block, None)
592+
} else {
593+
call
661594
}
662-
663-
MayContainYieldPoint.visit_expr(e).is_break()
664595
}
665596

666597
fn for_all_argument_indexes(template: &mut [FormatArgsPiece], mut f: impl FnMut(&mut usize)) {

compiler/rustc_ast_lowering/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22352235
self.stmt(span, hir::StmtKind::Let(self.arena.alloc(local)))
22362236
}
22372237

2238+
fn stmt_super_let_pat(
2239+
&mut self,
2240+
span: Span,
2241+
pat: &'hir hir::Pat<'hir>,
2242+
init: Option<&'hir hir::Expr<'hir>>,
2243+
) -> hir::Stmt<'hir> {
2244+
let hir_id = self.next_id();
2245+
let local = hir::LetStmt {
2246+
super_: Some(span),
2247+
hir_id,
2248+
init,
2249+
pat,
2250+
els: None,
2251+
source: hir::LocalSource::Normal,
2252+
span: self.lower_span(span),
2253+
ty: None,
2254+
};
2255+
self.stmt(span, hir::StmtKind::Let(self.arena.alloc(local)))
2256+
}
2257+
22382258
fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
22392259
self.block_all(expr.span, &[], Some(expr))
22402260
}

library/core/src/fmt/rt.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -107,43 +107,43 @@ impl Argument<'_> {
107107
}
108108

109109
#[inline]
110-
pub fn new_display<T: Display>(x: &T) -> Argument<'_> {
110+
pub const fn new_display<T: Display>(x: &T) -> Argument<'_> {
111111
Self::new(x, Display::fmt)
112112
}
113113
#[inline]
114-
pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
114+
pub const fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
115115
Self::new(x, Debug::fmt)
116116
}
117117
#[inline]
118-
pub fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
118+
pub const fn new_debug_noop<T: Debug>(x: &T) -> Argument<'_> {
119119
Self::new(x, |_, _| Ok(()))
120120
}
121121
#[inline]
122-
pub fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
122+
pub const fn new_octal<T: Octal>(x: &T) -> Argument<'_> {
123123
Self::new(x, Octal::fmt)
124124
}
125125
#[inline]
126-
pub fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
126+
pub const fn new_lower_hex<T: LowerHex>(x: &T) -> Argument<'_> {
127127
Self::new(x, LowerHex::fmt)
128128
}
129129
#[inline]
130-
pub fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
130+
pub const fn new_upper_hex<T: UpperHex>(x: &T) -> Argument<'_> {
131131
Self::new(x, UpperHex::fmt)
132132
}
133133
#[inline]
134-
pub fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
134+
pub const fn new_pointer<T: Pointer>(x: &T) -> Argument<'_> {
135135
Self::new(x, Pointer::fmt)
136136
}
137137
#[inline]
138-
pub fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
138+
pub const fn new_binary<T: Binary>(x: &T) -> Argument<'_> {
139139
Self::new(x, Binary::fmt)
140140
}
141141
#[inline]
142-
pub fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
142+
pub const fn new_lower_exp<T: LowerExp>(x: &T) -> Argument<'_> {
143143
Self::new(x, LowerExp::fmt)
144144
}
145145
#[inline]
146-
pub fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
146+
pub const fn new_upper_exp<T: UpperExp>(x: &T) -> Argument<'_> {
147147
Self::new(x, UpperExp::fmt)
148148
}
149149
#[inline]
@@ -189,16 +189,8 @@ impl Argument<'_> {
189189
}
190190
}
191191

192-
/// Used by `format_args` when all arguments are gone after inlining,
193-
/// when using `&[]` would incorrectly allow for a bigger lifetime.
194-
///
195-
/// This fails without format argument inlining, and that shouldn't be different
196-
/// when the argument is inlined:
197-
///
198-
/// ```compile_fail,E0716
199-
/// let f = format_args!("{}", "a");
200-
/// println!("{f}");
201-
/// ```
192+
/// Bootstrap only.
193+
#[cfg(bootstrap)]
202194
#[inline]
203195
pub const fn none() -> [Self; 0] {
204196
[]

library/coretests/tests/fmt/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ mod builders;
22
mod float;
33
mod num;
44

5+
#[test]
6+
fn test_lifetime() {
7+
// Trigger all different forms of expansion,
8+
// and check that each of them can be stored as a variable.
9+
let a = format_args!("hello");
10+
let a = format_args!("hello {a}");
11+
let a = format_args!("hello {a:1}");
12+
let a = format_args!("hello {a} {a:?}");
13+
assert_eq!(a.to_string(), "hello hello hello hello hello hello hello");
14+
15+
// It should also work in consts.
16+
const A: std::fmt::Arguments<'static> = format_args!("hello");
17+
const B: std::fmt::Arguments<'static> = format_args!("hello {A}");
18+
const C: std::fmt::Arguments<'static> = format_args!("hello {B:1}");
19+
const D: std::fmt::Arguments<'static> = format_args!("hello {C} {C:?}");
20+
assert_eq!(D.to_string(), "hello hello hello hello hello hello hello");
21+
}
22+
523
#[test]
624
fn test_format_flags() {
725
// No residual flags left by pointer formatting

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ ui/borrowck/issue-104639-lifetime-order.rs
311311
ui/borrowck/issue-10876.rs
312312
ui/borrowck/issue-109271-pass-self-into-closure.rs
313313
ui/borrowck/issue-111554.rs
314-
ui/borrowck/issue-114374-invalid-help-fmt-args.rs
315314
ui/borrowck/issue-11493.rs
316315
ui/borrowck/issue-115259-suggest-iter-mut.rs
317316
ui/borrowck/issue-119915-bad-clone-suggestion.rs

0 commit comments

Comments
 (0)