about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-11-03 12:15:55 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2022-11-03 15:58:03 +1100
commit84ca2c3bab370ee58ebd23050e9286e1d9e664b9 (patch)
treed9c80dff82a56e38a50ee9d3a8df651fd6b9d08b /compiler
parentf32e6781b2932aed55342ad8a4a7f1023acb30b4 (diff)
downloadrust-84ca2c3bab370ee58ebd23050e9286e1d9e664b9.tar.gz
rust-84ca2c3bab370ee58ebd23050e9286e1d9e664b9.zip
Clarify range calculations.
There is some subtlety here.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_lexer/src/unescape.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/compiler/rustc_lexer/src/unescape.rs b/compiler/rustc_lexer/src/unescape.rs
index a6752c82bd3..dc2fd359e27 100644
--- a/compiler/rustc_lexer/src/unescape.rs
+++ b/compiler/rustc_lexer/src/unescape.rs
@@ -275,11 +275,13 @@ where
     F: FnMut(Range<usize>, Result<char, EscapeError>),
 {
     debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
-    let initial_len = src.len();
     let mut chars = src.chars();
-    while let Some(c) = chars.next() {
-        let start = initial_len - chars.as_str().len() - c.len_utf8();
 
+    // The `start` and `end` computation here is complicated because
+    // `skip_ascii_whitespace` makes us to skip over chars without counting
+    // them in the range computation.
+    while let Some(c) = chars.next() {
+        let start = src.len() - chars.as_str().len() - c.len_utf8();
         let result = match c {
             '\\' => {
                 match chars.clone().next() {
@@ -300,7 +302,7 @@ where
             '\r' => Err(EscapeError::BareCarriageReturn),
             _ => ascii_check(c, mode),
         };
-        let end = initial_len - chars.as_str().len();
+        let end = src.len() - chars.as_str().len();
         callback(start..end, result);
     }
 
@@ -340,19 +342,19 @@ where
     F: FnMut(Range<usize>, Result<char, EscapeError>),
 {
     debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
-    let initial_len = src.len();
-
     let mut chars = src.chars();
-    while let Some(c) = chars.next() {
-        let start = initial_len - chars.as_str().len() - c.len_utf8();
 
+    // The `start` and `end` computation here matches the one in
+    // `unescape_str_or_byte_str` for consistency, even though this function
+    // doesn't have to worry about skipping any chars.
+    while let Some(c) = chars.next() {
+        let start = src.len() - chars.as_str().len() - c.len_utf8();
         let result = match c {
             '\r' => Err(EscapeError::BareCarriageReturnInRawString),
             c if mode.is_bytes() && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
             c => Ok(c),
         };
-        let end = initial_len - chars.as_str().len();
-
+        let end = src.len() - chars.as_str().len();
         callback(start..end, result);
     }
 }