Don't allow new const panic through format flattening.
panic!("a {}", "b") is still not allowed in const, even if the hir flattens to panic!("a b").
This commit is contained in:
parent
96d252160e
commit
f2f6bcc499
7 changed files with 66 additions and 17 deletions
|
@ -13,9 +13,12 @@ use std::borrow::Cow;
|
|||
|
||||
impl<'hir> LoweringContext<'_, 'hir> {
|
||||
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
|
||||
// Never call the const constructor of `fmt::Arguments` if the
|
||||
// format_args!() had any arguments _before_ flattening/inlining.
|
||||
let allow_const = fmt.arguments.all_args().is_empty();
|
||||
let fmt = flatten_format_args(Cow::Borrowed(fmt));
|
||||
let fmt = inline_literals(fmt);
|
||||
expand_format_args(self, sp, &fmt)
|
||||
expand_format_args(self, sp, &fmt, allow_const)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,6 +345,7 @@ fn expand_format_args<'hir>(
|
|||
ctx: &mut LoweringContext<'_, 'hir>,
|
||||
macsp: Span,
|
||||
fmt: &FormatArgs,
|
||||
allow_const: bool,
|
||||
) -> hir::ExprKind<'hir> {
|
||||
let mut incomplete_lit = String::new();
|
||||
let lit_pieces =
|
||||
|
@ -411,6 +415,18 @@ fn expand_format_args<'hir>(
|
|||
|
||||
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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue