@@ -29,9 +29,13 @@ use crate::{
29
29
db:: DefDatabase ,
30
30
expander:: Expander ,
31
31
hir:: {
32
- dummy_expr_id, Array , Binding , BindingAnnotation , BindingId , BindingProblems , CaptureBy ,
33
- ClosureKind , Expr , ExprId , InlineAsm , Label , LabelId , Literal , LiteralOrConst , MatchArm ,
34
- Movability , OffsetOf , Pat , PatId , RecordFieldPat , RecordLitField , Statement ,
32
+ dummy_expr_id,
33
+ format_args:: {
34
+ self , FormatArgs , FormatArgument , FormatArgumentKind , FormatArgumentsCollector ,
35
+ } ,
36
+ Array , Binding , BindingAnnotation , BindingId , BindingProblems , CaptureBy , ClosureKind ,
37
+ Expr , ExprId , InlineAsm , Label , LabelId , Literal , LiteralOrConst , MatchArm , Movability ,
38
+ OffsetOf , Pat , PatId , RecordFieldPat , RecordLitField , Statement ,
35
39
} ,
36
40
item_scope:: BuiltinShadowMode ,
37
41
lang_item:: LangItem ,
@@ -649,15 +653,58 @@ impl ExprCollector<'_> {
649
653
}
650
654
ast:: Expr :: UnderscoreExpr ( _) => self . alloc_expr ( Expr :: Underscore , syntax_ptr) ,
651
655
ast:: Expr :: AsmExpr ( e) => {
652
- let expr = Expr :: InlineAsm ( InlineAsm { e : self . collect_expr_opt ( e. expr ( ) ) } ) ;
653
- self . alloc_expr ( expr , syntax_ptr)
656
+ let e = self . collect_expr_opt ( e. expr ( ) ) ;
657
+ self . alloc_expr ( Expr :: InlineAsm ( InlineAsm { e } ) , syntax_ptr)
654
658
}
655
659
ast:: Expr :: OffsetOfExpr ( e) => {
656
660
let container = Interned :: new ( TypeRef :: from_ast_opt ( & self . ctx ( ) , e. ty ( ) ) ) ;
657
661
let fields = e. fields ( ) . map ( |it| it. as_name ( ) ) . collect ( ) ;
658
662
self . alloc_expr ( Expr :: OffsetOf ( OffsetOf { container, fields } ) , syntax_ptr)
659
663
}
660
- ast:: Expr :: FormatArgsExpr ( _) => self . missing_expr ( ) ,
664
+ ast:: Expr :: FormatArgsExpr ( f) => {
665
+ let mut args = FormatArgumentsCollector :: new ( ) ;
666
+ f. args ( ) . for_each ( |arg| {
667
+ args. add ( FormatArgument {
668
+ kind : match arg. name ( ) {
669
+ Some ( name) => FormatArgumentKind :: Named ( name. as_name ( ) ) ,
670
+ None => FormatArgumentKind :: Normal ,
671
+ } ,
672
+ expr : self . collect_expr_opt ( arg. expr ( ) ) ,
673
+ } ) ;
674
+ } ) ;
675
+ let template = f. template ( ) ;
676
+ let fmt_snippet = template. as_ref ( ) . map ( ToString :: to_string) ;
677
+ let expr = self . collect_expr_opt ( f. template ( ) ) ;
678
+ if let Expr :: Literal ( Literal :: String ( _) ) = self . body [ expr] {
679
+ let source = self . source_map . expr_map_back [ expr] . clone ( ) ;
680
+ let is_direct_literal = source. file_id == self . expander . current_file_id ;
681
+ if let ast:: Expr :: Literal ( l) =
682
+ source. value . to_node ( & self . db . parse_or_expand ( source. file_id ) )
683
+ {
684
+ if let ast:: LiteralKind :: String ( s) = l. kind ( ) {
685
+ return Some ( self . alloc_expr (
686
+ Expr :: FormatArgs ( format_args:: parse (
687
+ expr,
688
+ & s,
689
+ fmt_snippet,
690
+ args,
691
+ is_direct_literal,
692
+ ) ) ,
693
+ syntax_ptr,
694
+ ) ) ;
695
+ }
696
+ }
697
+ }
698
+
699
+ self . alloc_expr (
700
+ Expr :: FormatArgs ( FormatArgs {
701
+ template_expr : expr,
702
+ template : Default :: default ( ) ,
703
+ arguments : args. finish ( ) ,
704
+ } ) ,
705
+ syntax_ptr,
706
+ )
707
+ }
661
708
} )
662
709
}
663
710
0 commit comments