Skip to content

Commit ac6eb65

Browse files
committed
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 | ^ ```
1 parent 89f273f commit ac6eb65

File tree

60 files changed

+135
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+135
-278
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4008,7 +4008,6 @@ dependencies = [
40084008
"termcolor",
40094009
"termize",
40104010
"tracing",
4011-
"unicode-width",
40124011
"windows",
40134012
]
40144013

compiler/rustc_errors/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ serde_json = "1.0.59"
2626
termcolor = "1.2.0"
2727
termize = "0.1.1"
2828
tracing = "0.1"
29-
unicode-width = "0.1.4"
3029
# tidy-alphabetical-end
3130

3231
[target.'cfg(windows)'.dependencies.windows]

compiler/rustc_errors/src/emitter.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! The output types are defined in `rustc_session::config::ErrorOutputType`.
99
1010
use rustc_span::source_map::SourceMap;
11-
use rustc_span::{FileLines, FileName, SourceFile, Span};
11+
use rustc_span::{char_width, FileLines, FileName, SourceFile, Span};
1212

1313
use crate::snippet::{
1414
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
@@ -2614,21 +2614,6 @@ fn normalize_whitespace(str: &str) -> String {
26142614
s
26152615
}
26162616

2617-
fn char_width(ch: char) -> usize {
2618-
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. For now,
2619-
// just accept that sometimes the code line will be longer than desired.
2620-
match ch {
2621-
'\t' => 4,
2622-
'\u{0000}' | '\u{0001}' | '\u{0002}' | '\u{0003}' | '\u{0004}' | '\u{0005}'
2623-
| '\u{0006}' | '\u{0007}' | '\u{0008}' | '\u{000B}' | '\u{000C}' | '\u{000D}'
2624-
| '\u{000E}' | '\u{000F}' | '\u{0010}' | '\u{0011}' | '\u{0012}' | '\u{0013}'
2625-
| '\u{0014}' | '\u{0015}' | '\u{0016}' | '\u{0017}' | '\u{0018}' | '\u{0019}'
2626-
| '\u{001A}' | '\u{001B}' | '\u{001C}' | '\u{001D}' | '\u{001E}' | '\u{001F}'
2627-
| '\u{007F}' => 1,
2628-
_ => unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1),
2629-
}
2630-
}
2631-
26322617
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
26332618
buffer.puts(line, col, "| ", Style::LineNumber);
26342619
}

compiler/rustc_metadata/src/rmeta/decoder.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17271727
source_len,
17281728
lines,
17291729
multibyte_chars,
1730-
non_narrow_chars,
17311730
normalized_pos,
17321731
stable_id,
17331732
..
@@ -1779,7 +1778,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17791778
self.cnum,
17801779
lines,
17811780
multibyte_chars,
1782-
non_narrow_chars,
17831781
normalized_pos,
17841782
source_file_index,
17851783
);

compiler/rustc_query_system/src/ich/impls_syntax.rs

-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
7373
source_len: _,
7474
lines: _,
7575
ref multibyte_chars,
76-
ref non_narrow_chars,
7776
ref normalized_pos,
7877
} = *self;
7978

@@ -98,11 +97,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
9897
char_pos.hash_stable(hcx, hasher);
9998
}
10099

101-
non_narrow_chars.len().hash_stable(hcx, hasher);
102-
for &char_pos in non_narrow_chars.iter() {
103-
char_pos.hash_stable(hcx, hasher);
104-
}
105-
106100
normalized_pos.len().hash_stable(hcx, hasher);
107101
for &char_pos in normalized_pos.iter() {
108102
char_pos.hash_stable(hcx, hasher);

compiler/rustc_span/src/analyze_source_file.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use unicode_width::UnicodeWidthChar;
32

43
#[cfg(test)]
54
mod tests;
@@ -9,15 +8,12 @@ mod tests;
98
///
109
/// This function will use an SSE2 enhanced implementation if hardware support
1110
/// is detected at runtime.
12-
pub fn analyze_source_file(
13-
src: &str,
14-
) -> (Vec<RelativeBytePos>, Vec<MultiByteChar>, Vec<NonNarrowChar>) {
11+
pub fn analyze_source_file(src: &str) -> (Vec<RelativeBytePos>, Vec<MultiByteChar>) {
1512
let mut lines = vec![RelativeBytePos::from_u32(0)];
1613
let mut multi_byte_chars = vec![];
17-
let mut non_narrow_chars = vec![];
1814

1915
// Calls the right implementation, depending on hardware support available.
20-
analyze_source_file_dispatch(src, &mut lines, &mut multi_byte_chars, &mut non_narrow_chars);
16+
analyze_source_file_dispatch(src, &mut lines, &mut multi_byte_chars);
2117

2218
// The code above optimistically registers a new line *after* each \n
2319
// it encounters. If that point is already outside the source_file, remove
@@ -30,7 +26,7 @@ pub fn analyze_source_file(
3026
}
3127
}
3228

33-
(lines, multi_byte_chars, non_narrow_chars)
29+
(lines, multi_byte_chars)
3430
}
3531

3632
cfg_match! {
@@ -39,11 +35,10 @@ cfg_match! {
3935
src: &str,
4036
lines: &mut Vec<RelativeBytePos>,
4137
multi_byte_chars: &mut Vec<MultiByteChar>,
42-
non_narrow_chars: &mut Vec<NonNarrowChar>,
4338
) {
4439
if is_x86_feature_detected!("sse2") {
4540
unsafe {
46-
analyze_source_file_sse2(src, lines, multi_byte_chars, non_narrow_chars);
41+
analyze_source_file_sse2(src, lines, multi_byte_chars);
4742
}
4843
} else {
4944
analyze_source_file_generic(
@@ -52,7 +47,6 @@ cfg_match! {
5247
RelativeBytePos::from_u32(0),
5348
lines,
5449
multi_byte_chars,
55-
non_narrow_chars,
5650
);
5751
}
5852
}
@@ -66,7 +60,6 @@ cfg_match! {
6660
src: &str,
6761
lines: &mut Vec<RelativeBytePos>,
6862
multi_byte_chars: &mut Vec<MultiByteChar>,
69-
non_narrow_chars: &mut Vec<NonNarrowChar>,
7063
) {
7164
#[cfg(target_arch = "x86")]
7265
use std::arch::x86::*;
@@ -159,7 +152,6 @@ cfg_match! {
159152
RelativeBytePos::from_usize(scan_start),
160153
lines,
161154
multi_byte_chars,
162-
non_narrow_chars,
163155
);
164156
}
165157

@@ -172,7 +164,6 @@ cfg_match! {
172164
RelativeBytePos::from_usize(tail_start),
173165
lines,
174166
multi_byte_chars,
175-
non_narrow_chars,
176167
);
177168
}
178169
}
@@ -183,15 +174,13 @@ cfg_match! {
183174
src: &str,
184175
lines: &mut Vec<RelativeBytePos>,
185176
multi_byte_chars: &mut Vec<MultiByteChar>,
186-
non_narrow_chars: &mut Vec<NonNarrowChar>,
187177
) {
188178
analyze_source_file_generic(
189179
src,
190180
src.len(),
191181
RelativeBytePos::from_u32(0),
192182
lines,
193183
multi_byte_chars,
194-
non_narrow_chars,
195184
);
196185
}
197186
}
@@ -205,7 +194,6 @@ fn analyze_source_file_generic(
205194
output_offset: RelativeBytePos,
206195
lines: &mut Vec<RelativeBytePos>,
207196
multi_byte_chars: &mut Vec<MultiByteChar>,
208-
non_narrow_chars: &mut Vec<NonNarrowChar>,
209197
) -> usize {
210198
assert!(src.len() >= scan_len);
211199
let mut i = 0;
@@ -227,16 +215,8 @@ fn analyze_source_file_generic(
227215

228216
let pos = RelativeBytePos::from_usize(i) + output_offset;
229217

230-
match byte {
231-
b'\n' => {
232-
lines.push(pos + RelativeBytePos(1));
233-
}
234-
b'\t' => {
235-
non_narrow_chars.push(NonNarrowChar::Tab(pos));
236-
}
237-
_ => {
238-
non_narrow_chars.push(NonNarrowChar::ZeroWidth(pos));
239-
}
218+
if let b'\n' = byte {
219+
lines.push(pos + RelativeBytePos(1));
240220
}
241221
} else if byte >= 127 {
242222
// The slow path:
@@ -252,14 +232,6 @@ fn analyze_source_file_generic(
252232
let mbc = MultiByteChar { pos, bytes: char_len as u8 };
253233
multi_byte_chars.push(mbc);
254234
}
255-
256-
// Assume control characters are zero width.
257-
// FIXME: How can we decide between `width` and `width_cjk`?
258-
let char_width = UnicodeWidthChar::width(c).unwrap_or(0);
259-
260-
if char_width != 1 {
261-
non_narrow_chars.push(NonNarrowChar::new(pos, char_width));
262-
}
263235
}
264236

265237
i += char_len;

compiler/rustc_span/src/analyze_source_file/tests.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ macro_rules! test {
44
(case: $test_name:ident,
55
text: $text:expr,
66
lines: $lines:expr,
7-
multi_byte_chars: $multi_byte_chars:expr,
8-
non_narrow_chars: $non_narrow_chars:expr,) => {
7+
multi_byte_chars: $multi_byte_chars:expr,) => {
98
#[test]
109
fn $test_name() {
11-
let (lines, multi_byte_chars, non_narrow_chars) = analyze_source_file($text);
10+
let (lines, multi_byte_chars) = analyze_source_file($text);
1211

1312
let expected_lines: Vec<RelativeBytePos> =
1413
$lines.into_iter().map(RelativeBytePos).collect();
@@ -21,13 +20,6 @@ macro_rules! test {
2120
.collect();
2221

2322
assert_eq!(multi_byte_chars, expected_mbcs);
24-
25-
let expected_nncs: Vec<NonNarrowChar> = $non_narrow_chars
26-
.into_iter()
27-
.map(|(pos, width)| NonNarrowChar::new(RelativeBytePos(pos), width))
28-
.collect();
29-
30-
assert_eq!(non_narrow_chars, expected_nncs);
3123
}
3224
};
3325
}
@@ -37,93 +29,81 @@ test!(
3729
text: "",
3830
lines: vec![],
3931
multi_byte_chars: vec![],
40-
non_narrow_chars: vec![],
4132
);
4233

4334
test!(
4435
case: newlines_short,
4536
text: "a\nc",
4637
lines: vec![0, 2],
4738
multi_byte_chars: vec![],
48-
non_narrow_chars: vec![],
4939
);
5040

5141
test!(
5242
case: newlines_long,
5343
text: "012345678\nabcdef012345678\na",
5444
lines: vec![0, 10, 26],
5545
multi_byte_chars: vec![],
56-
non_narrow_chars: vec![],
5746
);
5847

5948
test!(
6049
case: newline_and_multi_byte_char_in_same_chunk,
6150
text: "01234β789\nbcdef0123456789abcdef",
6251
lines: vec![0, 11],
6352
multi_byte_chars: vec![(5, 2)],
64-
non_narrow_chars: vec![],
6553
);
6654

6755
test!(
6856
case: newline_and_control_char_in_same_chunk,
6957
text: "01234\u{07}6789\nbcdef0123456789abcdef",
7058
lines: vec![0, 11],
7159
multi_byte_chars: vec![],
72-
non_narrow_chars: vec![(5, 0)],
7360
);
7461

7562
test!(
7663
case: multi_byte_char_short,
7764
text: "aβc",
7865
lines: vec![0],
7966
multi_byte_chars: vec![(1, 2)],
80-
non_narrow_chars: vec![],
8167
);
8268

8369
test!(
8470
case: multi_byte_char_long,
8571
text: "0123456789abcΔf012345β",
8672
lines: vec![0],
8773
multi_byte_chars: vec![(13, 2), (22, 2)],
88-
non_narrow_chars: vec![],
8974
);
9075

9176
test!(
9277
case: multi_byte_char_across_chunk_boundary,
9378
text: "0123456789abcdeΔ123456789abcdef01234",
9479
lines: vec![0],
9580
multi_byte_chars: vec![(15, 2)],
96-
non_narrow_chars: vec![],
9781
);
9882

9983
test!(
10084
case: multi_byte_char_across_chunk_boundary_tail,
10185
text: "0123456789abcdeΔ....",
10286
lines: vec![0],
10387
multi_byte_chars: vec![(15, 2)],
104-
non_narrow_chars: vec![],
10588
);
10689

10790
test!(
10891
case: non_narrow_short,
10992
text: "0\t2",
11093
lines: vec![0],
11194
multi_byte_chars: vec![],
112-
non_narrow_chars: vec![(1, 4)],
11395
);
11496

11597
test!(
11698
case: non_narrow_long,
11799
text: "01\t3456789abcdef01234567\u{07}9",
118100
lines: vec![0],
119101
multi_byte_chars: vec![],
120-
non_narrow_chars: vec![(2, 4), (24, 0)],
121102
);
122103

123104
test!(
124105
case: output_offset_all,
125106
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
126107
lines: vec![0, 7, 27],
127108
multi_byte_chars: vec![(13, 2), (29, 2)],
128-
non_narrow_chars: vec![(2, 4), (24, 0)],
129109
);

0 commit comments

Comments
 (0)