@@ -321,33 +321,29 @@ impl<'a> StringReader<'a> {
321321 ( pos - self . source_file . start_pos ) . to_usize ( )
322322 }
323323
324- /// Calls `f` with a string slice of the source text spanning from `start`
325- /// up to but excluding `self.pos`, meaning the slice does not include
326- /// the character `self.ch`.
327- fn with_str_from < T , F > ( & self , start : BytePos , f : F ) -> T
328- where F : FnOnce ( & str ) -> T
324+ /// Slice of the source text from `start` up to but excluding `self.pos`,
325+ /// meaning the slice does not include the character `self.ch`.
326+ fn str_from ( & self , start : BytePos ) -> & str
329327 {
330- self . with_str_from_to ( start, self . pos , f )
328+ self . str_from_to ( start, self . pos )
331329 }
332330
333331 /// Creates a Name from a given offset to the current offset.
334332 fn name_from ( & self , start : BytePos ) -> ast:: Name {
335333 debug ! ( "taking an ident from {:?} to {:?}" , start, self . pos) ;
336- self . with_str_from ( start , Symbol :: intern)
334+ Symbol :: intern ( self . str_from ( start ) )
337335 }
338336
339337 /// As name_from, with an explicit endpoint.
340338 fn name_from_to ( & self , start : BytePos , end : BytePos ) -> ast:: Name {
341339 debug ! ( "taking an ident from {:?} to {:?}" , start, end) ;
342- self . with_str_from_to ( start, end, Symbol :: intern )
340+ Symbol :: intern ( self . str_from_to ( start, end) )
343341 }
344342
345- /// Calls `f` with a string slice of the source text spanning from `start`
346- /// up to but excluding `end`.
347- fn with_str_from_to < T , F > ( & self , start : BytePos , end : BytePos , f : F ) -> T
348- where F : FnOnce ( & str ) -> T
343+ /// Slice of the source text spanning from `start` up to but excluding `end`.
344+ fn str_from_to ( & self , start : BytePos , end : BytePos ) -> & str
349345 {
350- f ( & self . src [ self . src_index ( start) ..self . src_index ( end) ] )
346+ & self . src [ self . src_index ( start) ..self . src_index ( end) ]
351347 }
352348
353349 /// Converts CRLF to LF in the given string, raising an error on bare CR.
@@ -456,8 +452,8 @@ impl<'a> StringReader<'a> {
456452 self . bump ( ) ;
457453 }
458454
459- self . with_str_from ( start, |string| {
460- if string == "_" {
455+ match self . str_from ( start) {
456+ "_" => {
461457 self . sess . span_diagnostic
462458 . struct_span_warn ( self . mk_sp ( start, self . pos ) ,
463459 "underscore literal suffix is not allowed" )
@@ -468,10 +464,9 @@ impl<'a> StringReader<'a> {
468464 <https://github.com/rust-lang/rust/issues/42326>")
469465 . emit ( ) ;
470466 None
471- } else {
472- Some ( Symbol :: intern ( string) )
473467 }
474- } )
468+ name => Some ( Symbol :: intern ( name) )
469+ }
475470 }
476471
477472 /// PRECONDITION: self.ch is not whitespace
@@ -513,9 +508,7 @@ impl<'a> StringReader<'a> {
513508 }
514509
515510 let kind = if doc_comment {
516- self . with_str_from ( start_bpos, |string| {
517- token:: DocComment ( Symbol :: intern ( string) )
518- } )
511+ token:: DocComment ( self . name_from ( start_bpos) )
519512 } else {
520513 token:: Comment
521514 } ;
@@ -615,23 +608,22 @@ impl<'a> StringReader<'a> {
615608 self . bump ( ) ;
616609 }
617610
618- self . with_str_from ( start_bpos, |string| {
619- // but comments with only "*"s between two "/"s are not
620- let kind = if is_block_doc_comment ( string) {
621- let string = if has_cr {
622- self . translate_crlf ( start_bpos,
623- string,
624- "bare CR not allowed in block doc-comment" )
625- } else {
626- string. into ( )
627- } ;
628- token:: DocComment ( Symbol :: intern ( & string[ ..] ) )
611+ let string = self . str_from ( start_bpos) ;
612+ // but comments with only "*"s between two "/"s are not
613+ let kind = if is_block_doc_comment ( string) {
614+ let string = if has_cr {
615+ self . translate_crlf ( start_bpos,
616+ string,
617+ "bare CR not allowed in block doc-comment" )
629618 } else {
630- token :: Comment
619+ string . into ( )
631620 } ;
621+ token:: DocComment ( Symbol :: intern ( & string[ ..] ) )
622+ } else {
623+ token:: Comment
624+ } ;
632625
633- Some ( Token :: new ( kind, self . mk_sp ( start_bpos, self . pos ) ) )
634- } )
626+ Some ( Token :: new ( kind, self . mk_sp ( start_bpos, self . pos ) ) )
635627 }
636628
637629 /// Scan through any digits (base `scan_radix`) or underscores,
@@ -838,20 +830,17 @@ impl<'a> StringReader<'a> {
838830 self . bump ( ) ;
839831 }
840832
841- return Ok ( self . with_str_from ( start, |string| {
842- // FIXME: perform NFKC normalization here. (Issue #2253)
843- let name = ast:: Name :: intern ( string) ;
844-
845- if is_raw_ident {
846- let span = self . mk_sp ( raw_start, self . pos ) ;
847- if !name. can_be_raw ( ) {
848- self . err_span ( span, & format ! ( "`{}` cannot be a raw identifier" , name) ) ;
849- }
850- self . sess . raw_identifier_spans . borrow_mut ( ) . push ( span) ;
833+ // FIXME: perform NFKC normalization here. (Issue #2253)
834+ let name = self . name_from ( start) ;
835+ if is_raw_ident {
836+ let span = self . mk_sp ( raw_start, self . pos ) ;
837+ if !name. can_be_raw ( ) {
838+ self . err_span ( span, & format ! ( "`{}` cannot be a raw identifier" , name) ) ;
851839 }
840+ self . sess . raw_identifier_spans . borrow_mut ( ) . push ( span) ;
841+ }
852842
853- token:: Ident ( name, is_raw_ident)
854- } ) ) ;
843+ return Ok ( token:: Ident ( name, is_raw_ident) ) ;
855844 }
856845 }
857846
@@ -1300,101 +1289,95 @@ impl<'a> StringReader<'a> {
13001289 }
13011290
13021291 fn validate_char_escape ( & self , start_with_quote : BytePos ) {
1303- self . with_str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) , |lit| {
1304- if let Err ( ( off, err) ) = unescape:: unescape_char ( lit) {
1292+ let lit = self . str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) ) ;
1293+ if let Err ( ( off, err) ) = unescape:: unescape_char ( lit) {
1294+ emit_unescape_error (
1295+ & self . sess . span_diagnostic ,
1296+ lit,
1297+ self . mk_sp ( start_with_quote, self . pos ) ,
1298+ unescape:: Mode :: Char ,
1299+ 0 ..off,
1300+ err,
1301+ )
1302+ }
1303+ }
1304+
1305+ fn validate_byte_escape ( & self , start_with_quote : BytePos ) {
1306+ let lit = self . str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) ) ;
1307+ if let Err ( ( off, err) ) = unescape:: unescape_byte ( lit) {
1308+ emit_unescape_error (
1309+ & self . sess . span_diagnostic ,
1310+ lit,
1311+ self . mk_sp ( start_with_quote, self . pos ) ,
1312+ unescape:: Mode :: Byte ,
1313+ 0 ..off,
1314+ err,
1315+ )
1316+ }
1317+ }
1318+
1319+ fn validate_str_escape ( & self , start_with_quote : BytePos ) {
1320+ let lit = self . str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) ) ;
1321+ unescape:: unescape_str ( lit, & mut |range, c| {
1322+ if let Err ( err) = c {
13051323 emit_unescape_error (
13061324 & self . sess . span_diagnostic ,
13071325 lit,
13081326 self . mk_sp ( start_with_quote, self . pos ) ,
1309- unescape:: Mode :: Char ,
1310- 0 ..off ,
1327+ unescape:: Mode :: Str ,
1328+ range ,
13111329 err,
13121330 )
13131331 }
1314- } ) ;
1332+ } )
13151333 }
13161334
1317- fn validate_byte_escape ( & self , start_with_quote : BytePos ) {
1318- self . with_str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) , |lit| {
1319- if let Err ( ( off, err) ) = unescape:: unescape_byte ( lit) {
1335+ fn validate_raw_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
1336+ let lit = self . str_from_to ( content_start, content_end) ;
1337+ unescape:: unescape_raw_str ( lit, & mut |range, c| {
1338+ if let Err ( err) = c {
13201339 emit_unescape_error (
13211340 & self . sess . span_diagnostic ,
13221341 lit,
1323- self . mk_sp ( start_with_quote , self . pos ) ,
1324- unescape:: Mode :: Byte ,
1325- 0 ..off ,
1342+ self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
1343+ unescape:: Mode :: Str ,
1344+ range ,
13261345 err,
13271346 )
13281347 }
1329- } ) ;
1330- }
1331-
1332- fn validate_str_escape ( & self , start_with_quote : BytePos ) {
1333- self . with_str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) , |lit| {
1334- unescape:: unescape_str ( lit, & mut |range, c| {
1335- if let Err ( err) = c {
1336- emit_unescape_error (
1337- & self . sess . span_diagnostic ,
1338- lit,
1339- self . mk_sp ( start_with_quote, self . pos ) ,
1340- unescape:: Mode :: Str ,
1341- range,
1342- err,
1343- )
1344- }
1345- } )
1346- } ) ;
1347- }
1348-
1349- fn validate_raw_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
1350- self . with_str_from_to ( content_start, content_end, |lit : & str | {
1351- unescape:: unescape_raw_str ( lit, & mut |range, c| {
1352- if let Err ( err) = c {
1353- emit_unescape_error (
1354- & self . sess . span_diagnostic ,
1355- lit,
1356- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
1357- unescape:: Mode :: Str ,
1358- range,
1359- err,
1360- )
1361- }
1362- } )
1363- } ) ;
1348+ } )
13641349 }
13651350
13661351 fn validate_raw_byte_str_escape ( & self , content_start : BytePos , content_end : BytePos ) {
1367- self . with_str_from_to ( content_start, content_end, |lit : & str | {
1368- unescape:: unescape_raw_byte_str ( lit, & mut |range, c| {
1369- if let Err ( err) = c {
1370- emit_unescape_error (
1371- & self . sess . span_diagnostic ,
1372- lit,
1373- self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
1374- unescape:: Mode :: ByteStr ,
1375- range,
1376- err,
1377- )
1378- }
1379- } )
1380- } ) ;
1352+ let lit = self . str_from_to ( content_start, content_end) ;
1353+ unescape:: unescape_raw_byte_str ( lit, & mut |range, c| {
1354+ if let Err ( err) = c {
1355+ emit_unescape_error (
1356+ & self . sess . span_diagnostic ,
1357+ lit,
1358+ self . mk_sp ( content_start - BytePos ( 1 ) , content_end + BytePos ( 1 ) ) ,
1359+ unescape:: Mode :: ByteStr ,
1360+ range,
1361+ err,
1362+ )
1363+ }
1364+ } )
13811365 }
13821366
13831367 fn validate_byte_str_escape ( & self , start_with_quote : BytePos ) {
1384- self . with_str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) , |lit| {
1385- unescape:: unescape_byte_str ( lit, & mut |range, c| {
1386- if let Err ( err) = c {
1387- emit_unescape_error (
1388- & self . sess . span_diagnostic ,
1389- lit,
1390- self . mk_sp ( start_with_quote, self . pos ) ,
1391- unescape:: Mode :: ByteStr ,
1392- range,
1393- err,
1394- )
1395- }
1396- } )
1397- } ) ;
1368+ let lit = self . str_from_to ( start_with_quote + BytePos ( 1 ) , self . pos - BytePos ( 1 ) ) ;
1369+ unescape:: unescape_byte_str ( lit, & mut |range, c| {
1370+ if let Err ( err) = c {
1371+ emit_unescape_error (
1372+ & self . sess . span_diagnostic ,
1373+ lit,
1374+ self . mk_sp ( start_with_quote, self . pos ) ,
1375+ unescape:: Mode :: ByteStr ,
1376+ range,
1377+ err,
1378+ )
1379+ }
1380+ } )
13981381 }
13991382}
14001383
0 commit comments