1
1
//! Parses `format_args` input.
2
2
3
+ use either:: Either ;
3
4
use hir_expand:: name:: Name ;
4
5
use intern:: Symbol ;
5
6
use rustc_parse_format as parse;
6
7
use span:: SyntaxContextId ;
7
8
use stdx:: TupleExt ;
8
9
use syntax:: {
9
10
ast:: { self , IsString } ,
10
- TextRange , TextSize ,
11
+ TextRange ,
11
12
} ;
12
13
13
14
use crate :: hir:: ExprId ;
@@ -33,7 +34,7 @@ pub enum FormatArgsPiece {
33
34
Placeholder ( FormatPlaceholder ) ,
34
35
}
35
36
36
- #[ derive( Copy , Debug , Clone , PartialEq , Eq ) ]
37
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
37
38
pub struct FormatPlaceholder {
38
39
/// Index into [`FormatArgs::arguments`].
39
40
pub argument : FormatArgPosition ,
@@ -45,11 +46,11 @@ pub struct FormatPlaceholder {
45
46
pub format_options : FormatOptions ,
46
47
}
47
48
48
- #[ derive( Copy , Debug , Clone , PartialEq , Eq ) ]
49
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
49
50
pub struct FormatArgPosition {
50
51
/// Which argument this position refers to (Ok),
51
52
/// or would've referred to if it existed (Err).
52
- pub index : Result < usize , usize > ,
53
+ pub index : Result < usize , Either < usize , Name > > ,
53
54
/// What kind of position this is. See [`FormatArgPositionKind`].
54
55
pub kind : FormatArgPositionKind ,
55
56
/// The span of the name or number.
@@ -88,7 +89,7 @@ pub enum FormatTrait {
88
89
UpperHex ,
89
90
}
90
91
91
- #[ derive( Copy , Clone , Default , Debug , PartialEq , Eq ) ]
92
+ #[ derive( Clone , Default , Debug , PartialEq , Eq ) ]
92
93
pub struct FormatOptions {
93
94
/// The width. E.g. `{:5}` or `{:width$}`.
94
95
pub width : Option < FormatCount > ,
@@ -133,7 +134,7 @@ pub enum FormatAlignment {
133
134
Center ,
134
135
}
135
136
136
- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
137
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
137
138
pub enum FormatCount {
138
139
/// `{:5}` or `{:.5}`
139
140
Literal ( usize ) ,
@@ -173,7 +174,7 @@ pub(crate) fn parse(
173
174
fmt_snippet : Option < String > ,
174
175
mut args : FormatArgumentsCollector ,
175
176
is_direct_literal : bool ,
176
- mut synth : impl FnMut ( Name ) -> ExprId ,
177
+ mut synth : impl FnMut ( Name , Option < TextRange > ) -> ExprId ,
177
178
mut record_usage : impl FnMut ( Name , Option < TextRange > ) ,
178
179
call_ctx : SyntaxContextId ,
179
180
) -> FormatArgs {
@@ -192,7 +193,6 @@ pub(crate) fn parse(
192
193
}
193
194
None => None ,
194
195
} ;
195
-
196
196
let mut parser =
197
197
parse:: Parser :: new ( & text, str_style, fmt_snippet, false , parse:: ParseMode :: Format ) ;
198
198
@@ -217,7 +217,6 @@ pub(crate) fn parse(
217
217
let to_span = |inner_span : parse:: InnerSpan | {
218
218
is_source_literal. then ( || {
219
219
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 )
221
220
} )
222
221
} ;
223
222
@@ -245,8 +244,8 @@ pub(crate) fn parse(
245
244
Ok ( index)
246
245
} else {
247
246
// 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) )
250
249
}
251
250
}
252
251
ArgRef :: Name ( name, span) => {
@@ -265,14 +264,17 @@ pub(crate) fn parse(
265
264
// For the moment capturing variables from format strings expanded from macros is
266
265
// disabled (see RFC #2795)
267
266
// 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
+ } ) )
268
277
}
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
- } ) )
276
278
}
277
279
}
278
280
} ;
0 commit comments