@@ -321,33 +321,29 @@ impl<'a> StringReader<'a> {
321
321
( pos - self . source_file . start_pos ) . to_usize ( )
322
322
}
323
323
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
329
327
{
330
- self . with_str_from_to ( start, self . pos , f )
328
+ self . str_from_to ( start, self . pos )
331
329
}
332
330
333
331
/// Creates a Name from a given offset to the current offset.
334
332
fn name_from ( & self , start : BytePos ) -> ast:: Name {
335
333
debug ! ( "taking an ident from {:?} to {:?}" , start, self . pos) ;
336
- self . with_str_from ( start , Symbol :: intern)
334
+ Symbol :: intern ( self . str_from ( start ) )
337
335
}
338
336
339
337
/// As name_from, with an explicit endpoint.
340
338
fn name_from_to ( & self , start : BytePos , end : BytePos ) -> ast:: Name {
341
339
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) )
343
341
}
344
342
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
349
345
{
350
- f ( & self . src [ self . src_index ( start) ..self . src_index ( end) ] )
346
+ & self . src [ self . src_index ( start) ..self . src_index ( end) ]
351
347
}
352
348
353
349
/// Converts CRLF to LF in the given string, raising an error on bare CR.
@@ -456,8 +452,8 @@ impl<'a> StringReader<'a> {
456
452
self . bump ( ) ;
457
453
}
458
454
459
- self . with_str_from ( start, |string| {
460
- if string == "_" {
455
+ match self . str_from ( start) {
456
+ "_" => {
461
457
self . sess . span_diagnostic
462
458
. struct_span_warn ( self . mk_sp ( start, self . pos ) ,
463
459
"underscore literal suffix is not allowed" )
@@ -468,10 +464,9 @@ impl<'a> StringReader<'a> {
468
464
<https://github.com/rust-lang/rust/issues/42326>")
469
465
. emit ( ) ;
470
466
None
471
- } else {
472
- Some ( Symbol :: intern ( string) )
473
467
}
474
- } )
468
+ name => Some ( Symbol :: intern ( name) )
469
+ }
475
470
}
476
471
477
472
/// PRECONDITION: self.ch is not whitespace
@@ -513,9 +508,7 @@ impl<'a> StringReader<'a> {
513
508
}
514
509
515
510
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) )
519
512
} else {
520
513
token:: Comment
521
514
} ;
@@ -615,23 +608,22 @@ impl<'a> StringReader<'a> {
615
608
self . bump ( ) ;
616
609
}
617
610
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" )
629
618
} else {
630
- token :: Comment
619
+ string . into ( )
631
620
} ;
621
+ token:: DocComment ( Symbol :: intern ( & string[ ..] ) )
622
+ } else {
623
+ token:: Comment
624
+ } ;
632
625
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 ) ) )
635
627
}
636
628
637
629
/// Scan through any digits (base `scan_radix`) or underscores,
@@ -838,20 +830,17 @@ impl<'a> StringReader<'a> {
838
830
self . bump ( ) ;
839
831
}
840
832
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) ) ;
851
839
}
840
+ self . sess . raw_identifier_spans . borrow_mut ( ) . push ( span) ;
841
+ }
852
842
853
- token:: Ident ( name, is_raw_ident)
854
- } ) ) ;
843
+ return Ok ( token:: Ident ( name, is_raw_ident) ) ;
855
844
}
856
845
}
857
846
@@ -1300,101 +1289,95 @@ impl<'a> StringReader<'a> {
1300
1289
}
1301
1290
1302
1291
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 {
1305
1323
emit_unescape_error (
1306
1324
& self . sess . span_diagnostic ,
1307
1325
lit,
1308
1326
self . mk_sp ( start_with_quote, self . pos ) ,
1309
- unescape:: Mode :: Char ,
1310
- 0 ..off ,
1327
+ unescape:: Mode :: Str ,
1328
+ range ,
1311
1329
err,
1312
1330
)
1313
1331
}
1314
- } ) ;
1332
+ } )
1315
1333
}
1316
1334
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 {
1320
1339
emit_unescape_error (
1321
1340
& self . sess . span_diagnostic ,
1322
1341
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 ,
1326
1345
err,
1327
1346
)
1328
1347
}
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
+ } )
1364
1349
}
1365
1350
1366
1351
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
+ } )
1381
1365
}
1382
1366
1383
1367
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
+ } )
1398
1381
}
1399
1382
}
1400
1383
0 commit comments