about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-30 17:12:56 +0000
committerGitHub <noreply@github.com>2021-01-30 17:12:56 +0000
commit286d90de2d213b467a092e702edf8b0706c7c1b2 (patch)
tree462c0d7ff8cf26f551c0137cbc4332219da0cf10
parentf408ff50130eae0eb56e7f9668e9df39f7baa6dd (diff)
parent6c2ce5515064011b2fde064ff18f178465383bce (diff)
downloadrust-286d90de2d213b467a092e702edf8b0706c7c1b2.tar.gz
rust-286d90de2d213b467a092e702edf8b0706c7c1b2.zip
Merge #7500
7500: Fix ast::String::value not properly escaping in some cases r=Veykril a=Veykril

Fixes #7496
bors r+


Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
-rw-r--r--crates/syntax/src/ast/token_ext.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 5e07ec7d159..044e3e5e849 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -173,7 +173,7 @@ impl ast::String {
             buf.capacity() == 0,
         ) {
             (Ok(c), false) => buf.push(c),
-            (Ok(c), true) if Some(c) == text_iter.next() => (),
+            (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
             (Ok(c), true) => {
                 buf.reserve_exact(text.len());
                 buf.push_str(&text[..char_range.start]);
@@ -659,7 +659,7 @@ impl Radix {
 
 #[cfg(test)]
 mod tests {
-    use crate::ast::{make, FloatNumber, IntNumber};
+    use crate::ast::{self, make, FloatNumber, IntNumber};
 
     fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
         assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
@@ -692,4 +692,21 @@ mod tests {
         check_int_suffix("0o11u32", "u32");
         check_int_suffix("0xffu32", "u32");
     }
+
+    fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
+        assert_eq!(
+            ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
+                .value()
+                .as_deref(),
+            expected.into()
+        );
+    }
+
+    #[test]
+    fn test_string_escape() {
+        check_string_value(r"foobar", "foobar");
+        check_string_value(r"\foobar", None);
+        check_string_value(r"\nfoobar", "\nfoobar");
+        check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
+    }
 }