Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New fmt::Arguments representation. #115129

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ fn expand_format_args<'hir>(
}
}
}));
let lit_pieces = ctx.expr_array_ref(fmt.span, lit_pieces);

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

let arguments = fmt.arguments.all_args();

if allow_const && lit_pieces.len() <= 1 && arguments.is_empty() && argmap.is_empty() {
// Generate:
// <core::fmt::Arguments>::new_str(literal)
let new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
macsp,
hir::LangItem::FormatArguments,
sym::new_str,
));
let args = if lit_pieces.is_empty() {
ctx.arena.alloc_from_iter([ctx.expr_str(fmt.span, kw::Empty)]) // Empty string.
} else {
lit_pieces // Just one single literal string piece.
};
return hir::ExprKind::Call(new, args);
}

let format_options = use_format_options.then(|| {
// Generate:
// &[format_spec_0, format_spec_1, format_spec_2]
Expand All @@ -421,20 +438,6 @@ fn expand_format_args<'hir>(
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
});

let arguments = fmt.arguments.all_args();

if allow_const && arguments.is_empty() && argmap.is_empty() {
// Generate:
// <core::fmt::Arguments>::new_const(lit_pieces)
let new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
macsp,
hir::LangItem::FormatArguments,
sym::new_const,
));
let new_args = ctx.arena.alloc_from_iter([lit_pieces]);
return hir::ExprKind::Call(new, new_args);
}

// If the args array contains exactly all the original arguments once,
// in order, we can use a simple array instead of a `match` construction.
// However, if there's a yield point in any argument except the first one,
Expand Down Expand Up @@ -555,6 +558,8 @@ fn expand_format_args<'hir>(
)
};

let lit_pieces = ctx.expr_array_ref(fmt.span, lit_pieces);

if let Some(format_options) = format_options {
// Generate:
// <core::fmt::Arguments>::new_v1_formatted(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ symbols! {
new_lower_hex,
new_octal,
new_pointer,
new_str,
new_unchecked,
new_upper_exp,
new_upper_hex,
Expand Down
Loading