@@ -89,6 +89,28 @@ pub use need_type_info::TypeAnnotationNeeded;
8989
9090pub mod nice_region_error;
9191
92+ /// Makes a valid string literal from a string by escaping special characters (" and \),
93+ /// unless they are already escaped.
94+ fn escape_literal ( s : & str ) -> String {
95+ let mut escaped = String :: with_capacity ( s. len ( ) ) ;
96+ let mut chrs = s. chars ( ) . peekable ( ) ;
97+ while let Some ( first) = chrs. next ( ) {
98+ match ( first, chrs. peek ( ) ) {
99+ ( '\\' , Some ( & delim @ '"' ) | Some ( & delim @ '\'' ) ) => {
100+ escaped. push ( '\\' ) ;
101+ escaped. push ( delim) ;
102+ chrs. next ( ) ;
103+ }
104+ ( '"' | '\'' , _) => {
105+ escaped. push ( '\\' ) ;
106+ escaped. push ( first)
107+ }
108+ ( c, _) => escaped. push ( c) ,
109+ } ;
110+ }
111+ escaped
112+ }
113+
92114/// A helper for building type related errors. The `typeck_results`
93115/// field is only populated during an in-progress typeck.
94116/// Get an instance by calling `InferCtxt::err` or `FnCtxt::infer_err`.
@@ -1904,25 +1926,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19041926 terr : TypeError < ' tcx > ,
19051927 ) -> Vec < Error0308Subdiags > {
19061928 use crate :: traits:: ObligationCauseCode :: MatchExpressionArm ;
1907- fn escape_literal ( s : & str ) -> String {
1908- let mut escaped = String :: with_capacity ( s. len ( ) ) ;
1909- let mut chrs = s. chars ( ) . peekable ( ) ;
1910- while let Some ( first) = chrs. next ( ) {
1911- match ( first, chrs. peek ( ) ) {
1912- ( '\\' , Some ( & delim @ '"' ) | Some ( & delim @ '\'' ) ) => {
1913- escaped. push ( '\\' ) ;
1914- escaped. push ( delim) ;
1915- chrs. next ( ) ;
1916- }
1917- ( '"' | '\'' , _) => {
1918- escaped. push ( '\\' ) ;
1919- escaped. push ( first)
1920- }
1921- ( c, _) => escaped. push ( c) ,
1922- } ;
1923- }
1924- escaped
1925- }
19261929 let mut suggestions = Vec :: new ( ) ;
19271930 let span = trace. cause . span ( ) ;
19281931 if let Some ( ( expected, found) ) = trace. values . ty ( ) {
0 commit comments