about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIQuant <quant3234@gmail.com>2023-03-09 21:06:44 +0300
committerIQuant <quant3234@gmail.com>2023-04-04 18:50:07 +0300
commitaa33a6fca27f0b9183146e191ba565e37e7644eb (patch)
tree1f3d4cd6b56f5cff83bd77c19e2592283675c36a
parente813b6d6e1ef76a94a68f98316d76517d7ce0719 (diff)
downloadrust-aa33a6fca27f0b9183146e191ba565e37e7644eb.tar.gz
rust-aa33a6fca27f0b9183146e191ba565e37e7644eb.zip
Move and document escape_literal function
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/mod.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
index 4b7f4fe1aaa..212fea47437 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
@@ -89,6 +89,28 @@ pub use need_type_info::TypeAnnotationNeeded;
 
 pub mod nice_region_error;
 
+/// Makes a valid string literal from a string by escaping special characters (" and \),
+/// unless they are already escaped.
+fn escape_literal(s: &str) -> String {
+    let mut escaped = String::with_capacity(s.len());
+    let mut chrs = s.chars().peekable();
+    while let Some(first) = chrs.next() {
+        match (first, chrs.peek()) {
+            ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
+                escaped.push('\\');
+                escaped.push(delim);
+                chrs.next();
+            }
+            ('"' | '\'', _) => {
+                escaped.push('\\');
+                escaped.push(first)
+            }
+            (c, _) => escaped.push(c),
+        };
+    }
+    escaped
+}
+
 /// A helper for building type related errors. The `typeck_results`
 /// field is only populated during an in-progress typeck.
 /// Get an instance by calling `InferCtxt::err` or `FnCtxt::infer_err`.
@@ -1904,25 +1926,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
         terr: TypeError<'tcx>,
     ) -> Vec<Error0308Subdiags> {
         use crate::traits::ObligationCauseCode::MatchExpressionArm;
-        fn escape_literal(s: &str) -> String {
-            let mut escaped = String::with_capacity(s.len());
-            let mut chrs = s.chars().peekable();
-            while let Some(first) = chrs.next() {
-                match (first, chrs.peek()) {
-                    ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
-                        escaped.push('\\');
-                        escaped.push(delim);
-                        chrs.next();
-                    }
-                    ('"' | '\'', _) => {
-                        escaped.push('\\');
-                        escaped.push(first)
-                    }
-                    (c, _) => escaped.push(c),
-                };
-            }
-            escaped
-        }
         let mut suggestions = Vec::new();
         let span = trace.cause.span();
         if let Some((expected, found)) = trace.values.ty() {