Skip to content

Commit 3752488

Browse files
committed
Don't allocate the format_args template string as an expression
1 parent 3fa0bf0 commit 3752488

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

crates/hir-def/src/body/lower.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
hir::{
3333
dummy_expr_id,
3434
format_args::{
35-
self, FormatAlignment, FormatArgsPiece, FormatArgument, FormatArgumentKind,
35+
self, FormatAlignment, FormatArgs, FormatArgsPiece, FormatArgument, FormatArgumentKind,
3636
FormatArgumentsCollector, FormatCount, FormatDebugHex, FormatOptions,
3737
FormatPlaceholder, FormatSign, FormatTrait,
3838
},
@@ -1568,6 +1568,24 @@ impl ExprCollector<'_> {
15681568
// endregion: labels
15691569

15701570
// region: format
1571+
fn expand_macros_to_string(&mut self, expr: ast::Expr) -> Option<(ast::String, bool)> {
1572+
let m = match expr {
1573+
ast::Expr::MacroExpr(m) => m,
1574+
ast::Expr::Literal(l) => {
1575+
return match l.kind() {
1576+
ast::LiteralKind::String(s) => Some((s, true)),
1577+
_ => None,
1578+
}
1579+
}
1580+
_ => return None,
1581+
};
1582+
let e = m.macro_call()?;
1583+
let macro_ptr = AstPtr::new(&e);
1584+
let (exp, _) = self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
1585+
expansion.and_then(|it| this.expand_macros_to_string(it))
1586+
})?;
1587+
Some((exp, false))
1588+
}
15711589

15721590
fn collect_format_args(
15731591
&mut self,
@@ -1586,31 +1604,13 @@ impl ExprCollector<'_> {
15861604
});
15871605
let template = f.template();
15881606
let fmt_snippet = template.as_ref().map(ToString::to_string);
1589-
1590-
// FIXME: We shouldn't allocate this one, just resolve and expand the macros to fetch the
1591-
// string literal!
1592-
let expr = self.collect_expr_opt(template);
1593-
1594-
let fmt = 'b: {
1595-
if let Expr::Literal(Literal::String(_)) = self.body[expr] {
1596-
let source = self.source_map.expr_map_back[expr].clone();
1597-
let is_direct_literal = source.file_id == self.expander.current_file_id;
1598-
if let ast::Expr::Literal(l) =
1599-
source.value.to_node(&self.db.parse_or_expand(source.file_id))
1600-
{
1601-
if let ast::LiteralKind::String(s) = l.kind() {
1602-
break 'b format_args::parse(
1603-
expr,
1604-
&s,
1605-
fmt_snippet,
1606-
args,
1607-
is_direct_literal,
1608-
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
1609-
);
1610-
}
1611-
}
1607+
let fmt = match template.and_then(|it| self.expand_macros_to_string(it)) {
1608+
Some((s, is_direct_literal)) => {
1609+
format_args::parse(&s, fmt_snippet, args, is_direct_literal, |name| {
1610+
self.alloc_expr_desugared(Expr::Path(Path::from(name)))
1611+
})
16121612
}
1613-
return self.missing_expr();
1613+
None => FormatArgs { template: Default::default(), arguments: args.finish() },
16141614
};
16151615

16161616
// Create a list of all _unique_ (argument, format trait) combinations.

crates/hir-def/src/hir/format_args.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ mod parse;
1212

1313
#[derive(Debug, Clone, PartialEq, Eq)]
1414
pub struct FormatArgs {
15-
pub template_expr: ExprId,
1615
pub template: Box<[FormatArgsPiece]>,
1716
pub arguments: FormatArguments,
1817
}
@@ -166,7 +165,6 @@ enum PositionUsedAs {
166165
use PositionUsedAs::*;
167166

168167
pub(crate) fn parse(
169-
expr: ExprId,
170168
s: &ast::String,
171169
fmt_snippet: Option<String>,
172170
mut args: FormatArgumentsCollector,
@@ -195,11 +193,7 @@ pub(crate) fn parse(
195193
let is_source_literal = parser.is_source_literal;
196194
if !parser.errors.is_empty() {
197195
// FIXME: Diagnose
198-
return FormatArgs {
199-
template_expr: expr,
200-
template: Default::default(),
201-
arguments: args.finish(),
202-
};
196+
return FormatArgs { template: Default::default(), arguments: args.finish() };
203197
}
204198

205199
let to_span = |inner_span: parse::InnerSpan| {
@@ -419,11 +413,7 @@ pub(crate) fn parse(
419413
// FIXME: Diagnose
420414
}
421415

422-
FormatArgs {
423-
template_expr: expr,
424-
template: template.into_boxed_slice(),
425-
arguments: args.finish(),
426-
}
416+
FormatArgs { template: template.into_boxed_slice(), arguments: args.finish() }
427417
}
428418

429419
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)