@@ -77,14 +77,14 @@ pub struct FormatArguments {
7777 arguments : Vec < FormatArgument > ,
7878 num_unnamed_args : usize ,
7979 num_explicit_args : usize ,
80- names : FxHashMap < Symbol , usize > ,
80+ explicit_names : FxHashMap < Symbol , usize > ,
8181}
8282
8383impl FormatArguments {
8484 pub fn new ( ) -> Self {
8585 Self {
8686 arguments : Vec :: new ( ) ,
87- names : FxHashMap :: default ( ) ,
87+ explicit_names : FxHashMap :: default ( ) ,
8888 num_unnamed_args : 0 ,
8989 num_explicit_args : 0 ,
9090 }
@@ -93,13 +93,16 @@ impl FormatArguments {
9393 pub fn add ( & mut self , arg : FormatArgument ) -> usize {
9494 let index = self . arguments . len ( ) ;
9595 if let Some ( name) = arg. kind . ident ( ) {
96- self . names . insert ( name. name , index) ;
97- } else if self . names . is_empty ( ) {
96+ self . explicit_names . insert ( name. name , index) ;
97+ } else if let FormatArgumentKind :: Normal = arg. kind
98+ && self . explicit_names . is_empty ( )
99+ && self . arguments . len ( ) == self . num_unnamed_args
100+ {
98101 // Only count the unnamed args before the first named arg.
99102 // (Any later ones are errors.)
100103 self . num_unnamed_args += 1 ;
101104 }
102- if !matches ! ( arg. kind, FormatArgumentKind :: Captured ( .. ) ) {
105+ if !matches ! ( arg. kind, FormatArgumentKind :: Captured ) {
103106 // This is an explicit argument.
104107 // Make sure that all arguments so far are explicit.
105108 assert_eq ! (
@@ -113,8 +116,8 @@ impl FormatArguments {
113116 index
114117 }
115118
116- pub fn by_name ( & self , name : Symbol ) -> Option < ( usize , & FormatArgument ) > {
117- let i = * self . names . get ( & name) ?;
119+ pub fn by_explicit_name ( & self , name : Symbol ) -> Option < ( usize , & FormatArgument ) > {
120+ let i = * self . explicit_names . get ( & name) ?;
118121 Some ( ( i, & self . arguments [ i] ) )
119122 }
120123
@@ -156,15 +159,15 @@ pub enum FormatArgumentKind {
156159 /// `format_args(…, arg = 1)`
157160 Named ( Ident ) ,
158161 /// `format_args("… {arg} …")`
159- Captured ( Ident ) ,
162+ Captured ,
160163}
161164
162165impl FormatArgumentKind {
163166 pub fn ident ( & self ) -> Option < Ident > {
164167 match self {
165168 & Self :: Normal => None ,
166169 & Self :: Named ( id) => Some ( id) ,
167- & Self :: Captured ( id ) => Some ( id ) ,
170+ & Self :: Captured => None ,
168171 }
169172 }
170173}
0 commit comments