about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-07-09 17:00:19 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-07-18 20:08:38 +0000
commit2d7795dfb942d49d447837ef43db89216de15696 (patch)
treef1ef42e40392a1a0e1b7114662492507309e526e /compiler/rustc_errors/src
parent89f273f40dafb693139496ed6f914872b6533fa6 (diff)
downloadrust-2d7795dfb942d49d447837ef43db89216de15696.tar.gz
rust-2d7795dfb942d49d447837ef43db89216de15696.zip
Be more accurate about calculating `display_col` from a `BytePos`
No longer track "zero-width" chars in `SourceMap`, read directly from the line when calculating the `display_col` of a `BytePos`. Move `char_width` to `rustc_span` and use it from the emitter.

This change allows the following to properly align in terminals (depending on the font, the replaced control codepoints are rendered as 1 or 2 width, on my terminal they are rendered as 1, on VSCode text they are rendered as 2):

```
error: this file contains an unclosed delimiter
  --> $DIR/issue-68629.rs:5:17
   |
LL | ␜␟ts␀![{i
   |       -- unclosed delimiter
   |       |
   |       unclosed delimiter
LL | ␀␀  fn rݻoa>rݻm
   |                ^
```
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/emitter.rs17
1 files changed, 1 insertions, 16 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 95e1b5348b7..16fa0ff7a2d 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -8,7 +8,7 @@
 //! The output types are defined in `rustc_session::config::ErrorOutputType`.
 
 use rustc_span::source_map::SourceMap;
-use rustc_span::{FileLines, FileName, SourceFile, Span};
+use rustc_span::{char_width, FileLines, FileName, SourceFile, Span};
 
 use crate::snippet::{
     Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
@@ -2614,21 +2614,6 @@ fn normalize_whitespace(str: &str) -> String {
     s
 }
 
-fn char_width(ch: char) -> usize {
-    // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. For now,
-    // just accept that sometimes the code line will be longer than desired.
-    match ch {
-        '\t' => 4,
-        '\u{0000}' | '\u{0001}' | '\u{0002}' | '\u{0003}' | '\u{0004}' | '\u{0005}'
-        | '\u{0006}' | '\u{0007}' | '\u{0008}' | '\u{000B}' | '\u{000C}' | '\u{000D}'
-        | '\u{000E}' | '\u{000F}' | '\u{0010}' | '\u{0011}' | '\u{0012}' | '\u{0013}'
-        | '\u{0014}' | '\u{0015}' | '\u{0016}' | '\u{0017}' | '\u{0018}' | '\u{0019}'
-        | '\u{001A}' | '\u{001B}' | '\u{001C}' | '\u{001D}' | '\u{001E}' | '\u{001F}'
-        | '\u{007F}' => 1,
-        _ => unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1),
-    }
-}
-
 fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
     buffer.puts(line, col, "| ", Style::LineNumber);
 }