1
- use std:: ops:: Range ;
2
-
3
1
use diagnostics:: make_unclosed_delims_error;
4
2
use rustc_ast:: ast:: { self , AttrStyle } ;
5
3
use rustc_ast:: token:: { self , CommentKind , Delimiter , IdentIsRaw , Token , TokenKind } ;
@@ -8,7 +6,7 @@ use rustc_ast::util::unicode::contains_text_flow_control_chars;
8
6
use rustc_errors:: codes:: * ;
9
7
use rustc_errors:: { Applicability , Diag , DiagCtxtHandle , StashKey } ;
10
8
use rustc_lexer:: { Base , Cursor , DocStyle , LiteralKind , RawStrError } ;
11
- use rustc_literal_escaper:: { EscapeError , Mode , unescape_mixed , unescape_unicode } ;
9
+ use rustc_literal_escaper:: { EscapeError , Mode , unescape_for_errors } ;
12
10
use rustc_session:: lint:: BuiltinLintDiag ;
13
11
use rustc_session:: lint:: builtin:: {
14
12
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX , RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX ,
@@ -525,7 +523,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
525
523
}
526
524
err. emit ( )
527
525
}
528
- self . cook_unicode ( token:: Char , Mode :: Char , start, end, 1 , 1 ) // ' '
526
+ self . cook_quoted ( token:: Char , Mode :: Char , start, end, 1 , 1 ) // ' '
529
527
}
530
528
rustc_lexer:: LiteralKind :: Byte { terminated } => {
531
529
if !terminated {
@@ -537,7 +535,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
537
535
. with_code ( E0763 )
538
536
. emit ( )
539
537
}
540
- self . cook_unicode ( token:: Byte , Mode :: Byte , start, end, 2 , 1 ) // b' '
538
+ self . cook_quoted ( token:: Byte , Mode :: Byte , start, end, 2 , 1 ) // b' '
541
539
}
542
540
rustc_lexer:: LiteralKind :: Str { terminated } => {
543
541
if !terminated {
@@ -549,7 +547,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
549
547
. with_code ( E0765 )
550
548
. emit ( )
551
549
}
552
- self . cook_unicode ( token:: Str , Mode :: Str , start, end, 1 , 1 ) // " "
550
+ self . cook_quoted ( token:: Str , Mode :: Str , start, end, 1 , 1 ) // " "
553
551
}
554
552
rustc_lexer:: LiteralKind :: ByteStr { terminated } => {
555
553
if !terminated {
@@ -561,7 +559,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
561
559
. with_code ( E0766 )
562
560
. emit ( )
563
561
}
564
- self . cook_unicode ( token:: ByteStr , Mode :: ByteStr , start, end, 2 , 1 ) // b" "
562
+ self . cook_quoted ( token:: ByteStr , Mode :: ByteStr , start, end, 2 , 1 ) // b" "
565
563
}
566
564
rustc_lexer:: LiteralKind :: CStr { terminated } => {
567
565
if !terminated {
@@ -573,13 +571,13 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
573
571
. with_code ( E0767 )
574
572
. emit ( )
575
573
}
576
- self . cook_mixed ( token:: CStr , Mode :: CStr , start, end, 2 , 1 ) // c" "
574
+ self . cook_quoted ( token:: CStr , Mode :: CStr , start, end, 2 , 1 ) // c" "
577
575
}
578
576
rustc_lexer:: LiteralKind :: RawStr { n_hashes } => {
579
577
if let Some ( n_hashes) = n_hashes {
580
578
let n = u32:: from ( n_hashes) ;
581
579
let kind = token:: StrRaw ( n_hashes) ;
582
- self . cook_unicode ( kind, Mode :: RawStr , start, end, 2 + n, 1 + n) // r##" "##
580
+ self . cook_quoted ( kind, Mode :: RawStr , start, end, 2 + n, 1 + n) // r##" "##
583
581
} else {
584
582
self . report_raw_str_error ( start, 1 ) ;
585
583
}
@@ -588,7 +586,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
588
586
if let Some ( n_hashes) = n_hashes {
589
587
let n = u32:: from ( n_hashes) ;
590
588
let kind = token:: ByteStrRaw ( n_hashes) ;
591
- self . cook_unicode ( kind, Mode :: RawByteStr , start, end, 3 + n, 1 + n) // br##" "##
589
+ self . cook_quoted ( kind, Mode :: RawByteStr , start, end, 3 + n, 1 + n) // br##" "##
592
590
} else {
593
591
self . report_raw_str_error ( start, 2 ) ;
594
592
}
@@ -597,7 +595,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
597
595
if let Some ( n_hashes) = n_hashes {
598
596
let n = u32:: from ( n_hashes) ;
599
597
let kind = token:: CStrRaw ( n_hashes) ;
600
- self . cook_unicode ( kind, Mode :: RawCStr , start, end, 3 + n, 1 + n) // cr##" "##
598
+ self . cook_quoted ( kind, Mode :: RawCStr , start, end, 3 + n, 1 + n) // cr##" "##
601
599
} else {
602
600
self . report_raw_str_error ( start, 2 ) ;
603
601
}
@@ -914,40 +912,36 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
914
912
self . dcx ( ) . emit_fatal ( errors:: TooManyHashes { span : self . mk_sp ( start, self . pos ) , num } ) ;
915
913
}
916
914
917
- fn cook_common (
915
+ fn cook_quoted (
918
916
& self ,
919
917
mut kind : token:: LitKind ,
920
918
mode : Mode ,
921
919
start : BytePos ,
922
920
end : BytePos ,
923
921
prefix_len : u32 ,
924
922
postfix_len : u32 ,
925
- unescape : fn ( & str , Mode , & mut dyn FnMut ( Range < usize > , Result < ( ) , EscapeError > ) ) ,
926
923
) -> ( token:: LitKind , Symbol ) {
927
924
let content_start = start + BytePos ( prefix_len) ;
928
925
let content_end = end - BytePos ( postfix_len) ;
929
926
let lit_content = self . str_from_to ( content_start, content_end) ;
930
- unescape ( lit_content, mode, & mut |range, result| {
931
- // Here we only check for errors. The actual unescaping is done later.
932
- if let Err ( err) = result {
933
- let span_with_quotes = self . mk_sp ( start, end) ;
934
- let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
935
- let lo = content_start + BytePos ( start) ;
936
- let hi = lo + BytePos ( end - start) ;
937
- let span = self . mk_sp ( lo, hi) ;
938
- let is_fatal = err. is_fatal ( ) ;
939
- if let Some ( guar) = emit_unescape_error (
940
- self . dcx ( ) ,
941
- lit_content,
942
- span_with_quotes,
943
- span,
944
- mode,
945
- range,
946
- err,
947
- ) {
948
- assert ! ( is_fatal) ;
949
- kind = token:: Err ( guar) ;
950
- }
927
+ unescape_for_errors ( lit_content, mode, |range, err| {
928
+ let span_with_quotes = self . mk_sp ( start, end) ;
929
+ let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
930
+ let lo = content_start + BytePos ( start) ;
931
+ let hi = lo + BytePos ( end - start) ;
932
+ let span = self . mk_sp ( lo, hi) ;
933
+ let is_fatal = err. is_fatal ( ) ;
934
+ if let Some ( guar) = emit_unescape_error (
935
+ self . dcx ( ) ,
936
+ lit_content,
937
+ span_with_quotes,
938
+ span,
939
+ mode,
940
+ range,
941
+ err,
942
+ ) {
943
+ assert ! ( is_fatal) ;
944
+ kind = token:: Err ( guar) ;
951
945
}
952
946
} ) ;
953
947
@@ -960,34 +954,6 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
960
954
} ;
961
955
( kind, sym)
962
956
}
963
-
964
- fn cook_unicode (
965
- & self ,
966
- kind : token:: LitKind ,
967
- mode : Mode ,
968
- start : BytePos ,
969
- end : BytePos ,
970
- prefix_len : u32 ,
971
- postfix_len : u32 ,
972
- ) -> ( token:: LitKind , Symbol ) {
973
- self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
974
- unescape_unicode ( src, mode, & mut |span, result| callback ( span, result. map ( drop) ) )
975
- } )
976
- }
977
-
978
- fn cook_mixed (
979
- & self ,
980
- kind : token:: LitKind ,
981
- mode : Mode ,
982
- start : BytePos ,
983
- end : BytePos ,
984
- prefix_len : u32 ,
985
- postfix_len : u32 ,
986
- ) -> ( token:: LitKind , Symbol ) {
987
- self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
988
- unescape_mixed ( src, mode, & mut |span, result| callback ( span, result. map ( drop) ) )
989
- } )
990
- }
991
957
}
992
958
993
959
pub fn nfc_normalize ( string : & str ) -> Symbol {
0 commit comments