Skip to content

Commit 1bbb915

Browse files
committed
Use Arguments::new_str() in const_format_args!().
1 parent 76c63c1 commit 1bbb915

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ fn expand_format_args<'hir>(
380380
}
381381
}
382382
}));
383-
let lit_pieces = ctx.expr_array_ref(fmt.span, lit_pieces);
384383

385384
// Whether we'll use the `Arguments::new_v1_formatted` form (true),
386385
// or the `Arguments::new_v1` form (false).
@@ -407,6 +406,24 @@ fn expand_format_args<'hir>(
407406
}
408407
}
409408

409+
let arguments = fmt.arguments.all_args();
410+
411+
if allow_const && lit_pieces.len() <= 1 && arguments.is_empty() && argmap.is_empty() {
412+
// Generate:
413+
// <core::fmt::Arguments>::new_str(literal)
414+
let new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
415+
macsp,
416+
hir::LangItem::FormatArguments,
417+
sym::new_str,
418+
));
419+
let args = if lit_pieces.is_empty() {
420+
ctx.arena.alloc_from_iter([ctx.expr_str(fmt.span, kw::Empty)]) // Empty string.
421+
} else {
422+
lit_pieces // Just one single literal string piece.
423+
};
424+
return hir::ExprKind::Call(new, args);
425+
}
426+
410427
let format_options = use_format_options.then(|| {
411428
// Generate:
412429
// &[format_spec_0, format_spec_1, format_spec_2]
@@ -421,20 +438,6 @@ fn expand_format_args<'hir>(
421438
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
422439
});
423440

424-
let arguments = fmt.arguments.all_args();
425-
426-
if allow_const && arguments.is_empty() && argmap.is_empty() {
427-
// Generate:
428-
// <core::fmt::Arguments>::new_const(lit_pieces)
429-
let new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
430-
macsp,
431-
hir::LangItem::FormatArguments,
432-
sym::new_const,
433-
));
434-
let new_args = ctx.arena.alloc_from_iter([lit_pieces]);
435-
return hir::ExprKind::Call(new, new_args);
436-
}
437-
438441
// If the args array contains exactly all the original arguments once,
439442
// in order, we can use a simple array instead of a `match` construction.
440443
// However, if there's a yield point in any argument except the first one,
@@ -555,6 +558,8 @@ fn expand_format_args<'hir>(
555558
)
556559
};
557560

561+
let lit_pieces = ctx.expr_array_ref(fmt.span, lit_pieces);
562+
558563
if let Some(format_options) = format_options {
559564
// Generate:
560565
// <core::fmt::Arguments>::new_v1_formatted(

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ symbols! {
10271027
new_lower_hex,
10281028
new_octal,
10291029
new_pointer,
1030+
new_str,
10301031
new_unchecked,
10311032
new_upper_exp,
10321033
new_upper_hex,

0 commit comments

Comments
 (0)