@@ -5,7 +5,7 @@ use hir_expand::name::Name;
55use rustc_dependencies:: parse_format as parse;
66use syntax:: {
77 ast:: { self , IsString } ,
8- AstToken , SmolStr , TextRange ,
8+ SmolStr , TextRange , TextSize ,
99} ;
1010
1111use crate :: hir:: ExprId ;
@@ -170,15 +170,18 @@ pub(crate) fn parse(
170170 mut args : FormatArgumentsCollector ,
171171 is_direct_literal : bool ,
172172 mut synth : impl FnMut ( Name ) -> ExprId ,
173+ mut record_usage : impl FnMut ( Name , Option < TextRange > ) ,
173174) -> FormatArgs {
174- let text = s. text ( ) ;
175+ let text = s. text_without_quotes ( ) ;
175176 let str_style = match s. quote_offsets ( ) {
176177 Some ( offsets) => {
177178 let raw = u32:: from ( offsets. quotes . 0 . len ( ) ) - 1 ;
178- ( raw != 0 ) . then_some ( raw as usize )
179+ // subtract 1 for the `r` prefix
180+ ( raw != 0 ) . then ( || raw as usize - 1 )
179181 }
180182 None => None ,
181183 } ;
184+
182185 let mut parser =
183186 parse:: Parser :: new ( text, str_style, fmt_snippet, false , parse:: ParseMode :: Format ) ;
184187
@@ -199,6 +202,7 @@ pub(crate) fn parse(
199202 let to_span = |inner_span : parse:: InnerSpan | {
200203 is_source_literal. then ( || {
201204 TextRange :: new ( inner_span. start . try_into ( ) . unwrap ( ) , inner_span. end . try_into ( ) . unwrap ( ) )
205+ - TextSize :: from ( str_style. map ( |it| it + 1 ) . unwrap_or ( 0 ) as u32 + 1 )
202206 } )
203207 } ;
204208
@@ -230,9 +234,10 @@ pub(crate) fn parse(
230234 Err ( index)
231235 }
232236 }
233- ArgRef :: Name ( name, _span ) => {
237+ ArgRef :: Name ( name, span ) => {
234238 let name = Name :: new_text_dont_use ( SmolStr :: new ( name) ) ;
235239 if let Some ( ( index, _) ) = args. by_name ( & name) {
240+ record_usage ( name, span) ;
236241 // Name found in `args`, so we resolve it to its index.
237242 if index < args. explicit_args ( ) . len ( ) {
238243 // Mark it as used, if it was an explicit argument.
@@ -246,6 +251,7 @@ pub(crate) fn parse(
246251 // disabled (see RFC #2795)
247252 // FIXME: Diagnose
248253 }
254+ record_usage ( name. clone ( ) , span) ;
249255 Ok ( args. add ( FormatArgument {
250256 kind : FormatArgumentKind :: Captured ( name. clone ( ) ) ,
251257 // FIXME: This is problematic, we might want to synthesize a dummy
0 commit comments