1- use std:: ops:: Range ;
2-
31use diagnostics:: make_unclosed_delims_error;
42use rustc_ast:: ast:: { self , AttrStyle } ;
53use rustc_ast:: token:: { self , CommentKind , Delimiter , IdentIsRaw , Token , TokenKind } ;
@@ -10,7 +8,7 @@ use rustc_errors::{Applicability, Diag, DiagCtxtHandle, StashKey};
108use rustc_lexer:: {
119 Base , Cursor , DocStyle , FrontmatterAllowed , LiteralKind , RawStrError , is_whitespace,
1210} ;
13- use rustc_literal_escaper:: { EscapeError , Mode , unescape_mixed , unescape_unicode } ;
11+ use rustc_literal_escaper:: { EscapeError , Mode , check_for_errors } ;
1412use rustc_session:: lint:: BuiltinLintDiag ;
1513use rustc_session:: lint:: builtin:: {
1614 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX , RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX ,
@@ -702,7 +700,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
702700 }
703701 err. emit ( )
704702 }
705- self . cook_unicode ( token:: Char , Mode :: Char , start, end, 1 , 1 ) // ' '
703+ self . cook_quoted ( token:: Char , Mode :: Char , start, end, 1 , 1 ) // ' '
706704 }
707705 rustc_lexer:: LiteralKind :: Byte { terminated } => {
708706 if !terminated {
@@ -714,7 +712,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
714712 . with_code ( E0763 )
715713 . emit ( )
716714 }
717- self . cook_unicode ( token:: Byte , Mode :: Byte , start, end, 2 , 1 ) // b' '
715+ self . cook_quoted ( token:: Byte , Mode :: Byte , start, end, 2 , 1 ) // b' '
718716 }
719717 rustc_lexer:: LiteralKind :: Str { terminated } => {
720718 if !terminated {
@@ -726,7 +724,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
726724 . with_code ( E0765 )
727725 . emit ( )
728726 }
729- self . cook_unicode ( token:: Str , Mode :: Str , start, end, 1 , 1 ) // " "
727+ self . cook_quoted ( token:: Str , Mode :: Str , start, end, 1 , 1 ) // " "
730728 }
731729 rustc_lexer:: LiteralKind :: ByteStr { terminated } => {
732730 if !terminated {
@@ -738,7 +736,8 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
738736 . with_code ( E0766 )
739737 . emit ( )
740738 }
741- self . cook_unicode ( token:: ByteStr , Mode :: ByteStr , start, end, 2 , 1 ) // b" "
739+ self . cook_quoted ( token:: ByteStr , Mode :: ByteStr , start, end, 2 , 1 )
740+ // b" "
742741 }
743742 rustc_lexer:: LiteralKind :: CStr { terminated } => {
744743 if !terminated {
@@ -750,13 +749,14 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
750749 . with_code ( E0767 )
751750 . emit ( )
752751 }
753- self . cook_mixed ( token:: CStr , Mode :: CStr , start, end, 2 , 1 ) // c" "
752+ self . cook_quoted ( token:: CStr , Mode :: CStr , start, end, 2 , 1 ) // c" "
754753 }
755754 rustc_lexer:: LiteralKind :: RawStr { n_hashes } => {
756755 if let Some ( n_hashes) = n_hashes {
757756 let n = u32:: from ( n_hashes) ;
758757 let kind = token:: StrRaw ( n_hashes) ;
759- self . cook_unicode ( kind, Mode :: RawStr , start, end, 2 + n, 1 + n) // r##" "##
758+ self . cook_quoted ( kind, Mode :: RawStr , start, end, 2 + n, 1 + n)
759+ // r##" "##
760760 } else {
761761 self . report_raw_str_error ( start, 1 ) ;
762762 }
@@ -765,7 +765,8 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
765765 if let Some ( n_hashes) = n_hashes {
766766 let n = u32:: from ( n_hashes) ;
767767 let kind = token:: ByteStrRaw ( n_hashes) ;
768- self . cook_unicode ( kind, Mode :: RawByteStr , start, end, 3 + n, 1 + n) // br##" "##
768+ self . cook_quoted ( kind, Mode :: RawByteStr , start, end, 3 + n, 1 + n)
769+ // br##" "##
769770 } else {
770771 self . report_raw_str_error ( start, 2 ) ;
771772 }
@@ -774,7 +775,8 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
774775 if let Some ( n_hashes) = n_hashes {
775776 let n = u32:: from ( n_hashes) ;
776777 let kind = token:: CStrRaw ( n_hashes) ;
777- self . cook_unicode ( kind, Mode :: RawCStr , start, end, 3 + n, 1 + n) // cr##" "##
778+ self . cook_quoted ( kind, Mode :: RawCStr , start, end, 3 + n, 1 + n)
779+ // cr##" "##
778780 } else {
779781 self . report_raw_str_error ( start, 2 ) ;
780782 }
@@ -1091,40 +1093,36 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
10911093 self . dcx ( ) . emit_fatal ( errors:: TooManyHashes { span : self . mk_sp ( start, self . pos ) , num } ) ;
10921094 }
10931095
1094- fn cook_common (
1096+ fn cook_quoted (
10951097 & self ,
10961098 mut kind : token:: LitKind ,
10971099 mode : Mode ,
10981100 start : BytePos ,
10991101 end : BytePos ,
11001102 prefix_len : u32 ,
11011103 postfix_len : u32 ,
1102- unescape : fn ( & str , Mode , & mut dyn FnMut ( Range < usize > , Result < ( ) , EscapeError > ) ) ,
11031104 ) -> ( token:: LitKind , Symbol ) {
11041105 let content_start = start + BytePos ( prefix_len) ;
11051106 let content_end = end - BytePos ( postfix_len) ;
11061107 let lit_content = self . str_from_to ( content_start, content_end) ;
1107- unescape ( lit_content, mode, & mut |range, result| {
1108- // Here we only check for errors. The actual unescaping is done later.
1109- if let Err ( err) = result {
1110- let span_with_quotes = self . mk_sp ( start, end) ;
1111- let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
1112- let lo = content_start + BytePos ( start) ;
1113- let hi = lo + BytePos ( end - start) ;
1114- let span = self . mk_sp ( lo, hi) ;
1115- let is_fatal = err. is_fatal ( ) ;
1116- if let Some ( guar) = emit_unescape_error (
1117- self . dcx ( ) ,
1118- lit_content,
1119- span_with_quotes,
1120- span,
1121- mode,
1122- range,
1123- err,
1124- ) {
1125- assert ! ( is_fatal) ;
1126- kind = token:: Err ( guar) ;
1127- }
1108+ check_for_errors ( lit_content, mode, |range, err| {
1109+ let span_with_quotes = self . mk_sp ( start, end) ;
1110+ let ( start, end) = ( range. start as u32 , range. end as u32 ) ;
1111+ let lo = content_start + BytePos ( start) ;
1112+ let hi = lo + BytePos ( end - start) ;
1113+ let span = self . mk_sp ( lo, hi) ;
1114+ let is_fatal = err. is_fatal ( ) ;
1115+ if let Some ( guar) = emit_unescape_error (
1116+ self . dcx ( ) ,
1117+ lit_content,
1118+ span_with_quotes,
1119+ span,
1120+ mode,
1121+ range,
1122+ err,
1123+ ) {
1124+ assert ! ( is_fatal) ;
1125+ kind = token:: Err ( guar) ;
11281126 }
11291127 } ) ;
11301128
@@ -1137,34 +1135,6 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
11371135 } ;
11381136 ( kind, sym)
11391137 }
1140-
1141- fn cook_unicode (
1142- & self ,
1143- kind : token:: LitKind ,
1144- mode : Mode ,
1145- start : BytePos ,
1146- end : BytePos ,
1147- prefix_len : u32 ,
1148- postfix_len : u32 ,
1149- ) -> ( token:: LitKind , Symbol ) {
1150- self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
1151- unescape_unicode ( src, mode, & mut |span, result| callback ( span, result. map ( drop) ) )
1152- } )
1153- }
1154-
1155- fn cook_mixed (
1156- & self ,
1157- kind : token:: LitKind ,
1158- mode : Mode ,
1159- start : BytePos ,
1160- end : BytePos ,
1161- prefix_len : u32 ,
1162- postfix_len : u32 ,
1163- ) -> ( token:: LitKind , Symbol ) {
1164- self . cook_common ( kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
1165- unescape_mixed ( src, mode, & mut |span, result| callback ( span, result. map ( drop) ) )
1166- } )
1167- }
11681138}
11691139
11701140pub fn nfc_normalize ( string : & str ) -> Symbol {
0 commit comments