11//! Parses `format_args` input.
22
3+ use either:: Either ;
34use hir_expand:: name:: Name ;
45use intern:: Symbol ;
56use rustc_parse_format as parse;
67use span:: SyntaxContextId ;
78use stdx:: TupleExt ;
89use syntax:: {
910 ast:: { self , IsString } ,
10- TextRange , TextSize ,
11+ TextRange ,
1112} ;
1213
1314use crate :: hir:: ExprId ;
@@ -33,7 +34,7 @@ pub enum FormatArgsPiece {
3334 Placeholder ( FormatPlaceholder ) ,
3435}
3536
36- #[ derive( Copy , Debug , Clone , PartialEq , Eq ) ]
37+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
3738pub struct FormatPlaceholder {
3839 /// Index into [`FormatArgs::arguments`].
3940 pub argument : FormatArgPosition ,
@@ -45,11 +46,11 @@ pub struct FormatPlaceholder {
4546 pub format_options : FormatOptions ,
4647}
4748
48- #[ derive( Copy , Debug , Clone , PartialEq , Eq ) ]
49+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
4950pub struct FormatArgPosition {
5051 /// Which argument this position refers to (Ok),
5152 /// or would've referred to if it existed (Err).
52- pub index : Result < usize , usize > ,
53+ pub index : Result < usize , Either < usize , Name > > ,
5354 /// What kind of position this is. See [`FormatArgPositionKind`].
5455 pub kind : FormatArgPositionKind ,
5556 /// The span of the name or number.
@@ -88,7 +89,7 @@ pub enum FormatTrait {
8889 UpperHex ,
8990}
9091
91- #[ derive( Copy , Clone , Default , Debug , PartialEq , Eq ) ]
92+ #[ derive( Clone , Default , Debug , PartialEq , Eq ) ]
9293pub struct FormatOptions {
9394 /// The width. E.g. `{:5}` or `{:width$}`.
9495 pub width : Option < FormatCount > ,
@@ -133,7 +134,7 @@ pub enum FormatAlignment {
133134 Center ,
134135}
135136
136- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
137+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
137138pub enum FormatCount {
138139 /// `{:5}` or `{:.5}`
139140 Literal ( usize ) ,
@@ -173,7 +174,7 @@ pub(crate) fn parse(
173174 fmt_snippet : Option < String > ,
174175 mut args : FormatArgumentsCollector ,
175176 is_direct_literal : bool ,
176- mut synth : impl FnMut ( Name ) -> ExprId ,
177+ mut synth : impl FnMut ( Name , Option < TextRange > ) -> ExprId ,
177178 mut record_usage : impl FnMut ( Name , Option < TextRange > ) ,
178179 call_ctx : SyntaxContextId ,
179180) -> FormatArgs {
@@ -192,7 +193,6 @@ pub(crate) fn parse(
192193 }
193194 None => None ,
194195 } ;
195-
196196 let mut parser =
197197 parse:: Parser :: new ( & text, str_style, fmt_snippet, false , parse:: ParseMode :: Format ) ;
198198
@@ -217,7 +217,6 @@ pub(crate) fn parse(
217217 let to_span = |inner_span : parse:: InnerSpan | {
218218 is_source_literal. then ( || {
219219 TextRange :: new ( inner_span. start . try_into ( ) . unwrap ( ) , inner_span. end . try_into ( ) . unwrap ( ) )
220- - TextSize :: from ( str_style. map ( |it| it + 1 ) . unwrap_or ( 0 ) as u32 + 1 )
221220 } )
222221 } ;
223222
@@ -245,8 +244,8 @@ pub(crate) fn parse(
245244 Ok ( index)
246245 } else {
247246 // Doesn't exist as an explicit argument.
248- invalid_refs. push ( ( index, span, used_as, kind) ) ;
249- Err ( index)
247+ invalid_refs. push ( ( Either :: Left ( index) , span, used_as, kind) ) ;
248+ Err ( Either :: Left ( index) )
250249 }
251250 }
252251 ArgRef :: Name ( name, span) => {
@@ -265,14 +264,17 @@ pub(crate) fn parse(
265264 // For the moment capturing variables from format strings expanded from macros is
266265 // disabled (see RFC #2795)
267266 // FIXME: Diagnose
267+ invalid_refs. push ( ( Either :: Right ( name. clone ( ) ) , span, used_as, kind) ) ;
268+ Err ( Either :: Right ( name) )
269+ } else {
270+ record_usage ( name. clone ( ) , span) ;
271+ Ok ( args. add ( FormatArgument {
272+ kind : FormatArgumentKind :: Captured ( name. clone ( ) ) ,
273+ // FIXME: This is problematic, we might want to synthesize a dummy
274+ // expression proper and/or desugar these.
275+ expr : synth ( name, span) ,
276+ } ) )
268277 }
269- record_usage ( name. clone ( ) , span) ;
270- Ok ( args. add ( FormatArgument {
271- kind : FormatArgumentKind :: Captured ( name. clone ( ) ) ,
272- // FIXME: This is problematic, we might want to synthesize a dummy
273- // expression proper and/or desugar these.
274- expr : synth ( name) ,
275- } ) )
276278 }
277279 }
278280 } ;
0 commit comments