@@ -32,7 +32,7 @@ use crate::{
32
32
hir:: {
33
33
dummy_expr_id,
34
34
format_args:: {
35
- self , FormatAlignment , FormatArgsPiece , FormatArgument , FormatArgumentKind ,
35
+ self , FormatAlignment , FormatArgs , FormatArgsPiece , FormatArgument , FormatArgumentKind ,
36
36
FormatArgumentsCollector , FormatCount , FormatDebugHex , FormatOptions ,
37
37
FormatPlaceholder , FormatSign , FormatTrait ,
38
38
} ,
@@ -1568,6 +1568,24 @@ impl ExprCollector<'_> {
1568
1568
// endregion: labels
1569
1569
1570
1570
// 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
+ }
1571
1589
1572
1590
fn collect_format_args (
1573
1591
& mut self ,
@@ -1586,31 +1604,13 @@ impl ExprCollector<'_> {
1586
1604
} ) ;
1587
1605
let template = f. template ( ) ;
1588
1606
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
+ } )
1612
1612
}
1613
- return self . missing_expr ( ) ;
1613
+ None => FormatArgs { template : Default :: default ( ) , arguments : args . finish ( ) } ,
1614
1614
} ;
1615
1615
1616
1616
// Create a list of all _unique_ (argument, format trait) combinations.
0 commit comments