]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_errors/src/lib.rs
wip
[rust.git] / compiler / rustc_errors / src / lib.rs
index ec29d8016ddf4461478fc748f0065598d4ced5ec..ab3f094863269abba4a84858948444e34c20232f 100644 (file)
@@ -14,7 +14,7 @@
 
 pub use emitter::ColorConfig;
 
-use tracing::debug;
+use tracing::{debug, info};
 use Level::*;
 
 use emitter::{is_case_difference, Emitter, EmitterWriter};
@@ -283,6 +283,9 @@ fn push_trailing(
                 let mut buf = String::new();
 
                 let mut line_highlight = vec![];
+                // We need to keep track of the difference between the existing code and the added
+                // or deleted code in order to point at the correct column *after* substitution.
+                let mut acc = 0;
                 for part in &substitution.parts {
                     let cur_lo = sm.lookup_char_pos(part.span.lo());
                     if prev_hi.line == cur_lo.line {
@@ -290,6 +293,7 @@ fn push_trailing(
                             push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
                         while count > 0 {
                             highlights.push(std::mem::take(&mut line_highlight));
+                            acc = 0;
                             count -= 1;
                         }
                     } else {
@@ -297,6 +301,7 @@ fn push_trailing(
                         let mut count = push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
                         while count > 0 {
                             highlights.push(std::mem::take(&mut line_highlight));
+                            acc = 0;
                             count -= 1;
                         }
                         // push lines between the previous and current span (if any)
@@ -305,6 +310,7 @@ fn push_trailing(
                                 buf.push_str(line.as_ref());
                                 buf.push('\n');
                                 highlights.push(std::mem::take(&mut line_highlight));
+                                acc = 0;
                             }
                         }
                         if let Some(cur_line) = sf.get_line(cur_lo.line - 1) {
@@ -316,18 +322,22 @@ fn push_trailing(
                         }
                     }
                     // Add a whole line highlight per line in the snippet.
+                    let len = part.snippet.split('\n').next().unwrap_or(&part.snippet).len();
                     line_highlight.push(SubstitutionHighlight {
-                        start: cur_lo.col.0,
-                        end: cur_lo.col.0
-                            + part.snippet.split('\n').next().unwrap_or(&part.snippet).len(),
+                        start: (cur_lo.col.0 as isize + acc) as usize,
+                        end: (cur_lo.col.0 as isize + acc + len as isize) as usize,
                     });
+                    buf.push_str(&part.snippet);
+                    prev_hi = sm.lookup_char_pos(part.span.hi());
+                    if prev_hi.line == cur_lo.line {
+                        acc += len as isize - (prev_hi.col.0 - cur_lo.col.0) as isize;
+                    }
+                    prev_line = sf.get_line(prev_hi.line - 1);
                     for line in part.snippet.split('\n').skip(1) {
+                        acc = 0;
                         highlights.push(std::mem::take(&mut line_highlight));
                         line_highlight.push(SubstitutionHighlight { start: 0, end: line.len() });
                     }
-                    buf.push_str(&part.snippet);
-                    prev_hi = sm.lookup_char_pos(part.span.hi());
-                    prev_line = sf.get_line(prev_hi.line - 1);
                 }
                 highlights.push(std::mem::take(&mut line_highlight));
                 let only_capitalization = is_case_difference(sm, &buf, bounding_span);
@@ -339,6 +349,7 @@ fn push_trailing(
                 while buf.ends_with('\n') {
                     buf.pop();
                 }
+                info!(?buf, ?highlights);
                 Some((buf, substitution.parts, highlights, only_capitalization))
             })
             .collect()