@@ -89,6 +89,28 @@ pub use need_type_info::TypeAnnotationNeeded;
89
89
90
90
pub mod nice_region_error;
91
91
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
+
92
114
/// A helper for building type related errors. The `typeck_results`
93
115
/// field is only populated during an in-progress typeck.
94
116
/// Get an instance by calling `InferCtxt::err` or `FnCtxt::infer_err`.
@@ -1904,25 +1926,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1904
1926
terr : TypeError < ' tcx > ,
1905
1927
) -> Vec < Error0308Subdiags > {
1906
1928
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
- }
1926
1929
let mut suggestions = Vec :: new ( ) ;
1927
1930
let span = trace. cause . span ( ) ;
1928
1931
if let Some ( ( expected, found) ) = trace. values . ty ( ) {
0 commit comments