Skip to content

Commit a04c47a

Browse files
committed
Make trimming logic work on more than one span at a time
1 parent c75da0e commit a04c47a

File tree

6 files changed

+80
-29
lines changed

6 files changed

+80
-29
lines changed

compiler/rustc_errors/src/emitter.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -882,11 +882,16 @@ impl HumanEmitter {
882882
// | x_span
883883
// <EMPTY LINE>
884884
//
885+
let mut overlap = vec![false; annotations.len()];
885886
let mut annotations_position = vec![];
886887
let mut line_len: usize = 0;
887888
let mut p = 0;
888889
for (i, annotation) in annotations.iter().enumerate() {
889890
for (j, next) in annotations.iter().enumerate() {
891+
if overlaps(next, annotation, 0) && j > i {
892+
overlap[i] = true;
893+
overlap[j] = true;
894+
}
890895
if overlaps(next, annotation, 0) // This label overlaps with another one and both
891896
&& annotation.has_label() // take space (they have text and are not
892897
&& j > i // multiline lines).
@@ -1269,13 +1274,17 @@ impl HumanEmitter {
12691274
// We look for individual *long* spans, and we trim the *middle*, so that we render
12701275
// LL | ...= [0, 0, 0, ..., 0, 0];
12711276
// | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
1272-
for &(pos, annotation) in &annotations_position {
1277+
for (i, (_pos, annotation)) in annotations_position.iter().enumerate() {
1278+
// Skip cases where multiple spans overlap each other.
1279+
if overlap[i] {
1280+
continue;
1281+
};
12731282
let AnnotationType::Singleline = annotation.annotation_type else { continue };
12741283
let width = annotation.end_col.display - annotation.start_col.display;
1275-
if pos == 0 && width > margin.column_width && width > 10 {
1284+
if width > margin.column_width * 2 && width > 10 {
12761285
// If the terminal is *too* small, we keep at least a tiny bit of the span for
12771286
// display.
1278-
let pad = max(margin.column_width / 2, 5);
1287+
let pad = max(margin.column_width / 3, 5);
12791288
// Code line
12801289
buffer.replace(
12811290
line_offset,
@@ -1800,15 +1809,7 @@ impl HumanEmitter {
18001809
width_offset + annotated_file.multiline_depth + 1
18011810
};
18021811

1803-
let column_width = if let Some(width) = self.diagnostic_width {
1804-
width.saturating_sub(code_offset)
1805-
} else if self.ui_testing || cfg!(miri) {
1806-
DEFAULT_COLUMN_WIDTH
1807-
} else {
1808-
termize::dimensions()
1809-
.map(|(w, _)| w.saturating_sub(code_offset))
1810-
.unwrap_or(DEFAULT_COLUMN_WIDTH)
1811-
};
1812+
let column_width = self.column_width(code_offset);
18121813

18131814
let margin = Margin::new(
18141815
whitespace_margin,
@@ -1965,6 +1966,18 @@ impl HumanEmitter {
19651966
Ok(())
19661967
}
19671968

1969+
fn column_width(&self, code_offset: usize) -> usize {
1970+
if let Some(width) = self.diagnostic_width {
1971+
width.saturating_sub(code_offset)
1972+
} else if self.ui_testing || cfg!(miri) {
1973+
DEFAULT_COLUMN_WIDTH
1974+
} else {
1975+
termize::dimensions()
1976+
.map(|(w, _)| w.saturating_sub(code_offset))
1977+
.unwrap_or(DEFAULT_COLUMN_WIDTH)
1978+
}
1979+
}
1980+
19681981
fn emit_suggestion_default(
19691982
&mut self,
19701983
span: &MultiSpan,
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
2+
╭▸ $DIR/long-span.rs:7:5056
3+
4+
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, …, 0, 0, 0, 0, 0, 0, 0] + [0, 0, 0, 0, 0, 0, 0, 0, 0, …, 0, 0, 0, 0, 0, 0, 0];
5+
│ ┬───────────────────────────…────────────────────── ━ ────────────────────────────…────────────────────── [{integer}; 1680]
6+
│ │
7+
╰╴ [{integer}; 1680]
8+
19
error[E0308]: mismatched types
2-
╭▸ $DIR/long-span.rs:7:15
10+
╭▸ $DIR/long-span.rs:9:15
311
4-
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
5-
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━…━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`
12+
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, , 0, 0, 0, 0, 0, 0, 0];
13+
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`
614

7-
error: aborting due to 1 previous error
15+
error: aborting due to 2 previous errors
816

9-
For more information about this error, try `rustc --explain E0308`.
17+
Some errors have detailed explanations: E0308, E0369.
18+
For more information about an error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
2+
--> $DIR/long-span.rs:7:5056
3+
|
4+
LL | ... = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
5+
| -----------------------------------------...----------------------------------- ^ -----------------------------------------...----------------------------------- [{integer}; 1680]
6+
| |
7+
| [{integer}; 1680]
8+
19
error[E0308]: mismatched types
2-
--> $DIR/long-span.rs:7:15
10+
--> $DIR/long-span.rs:9:15
311
|
4-
LL | ... = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`
12+
LL | ... = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`
614

7-
error: aborting due to 1 previous error
15+
error: aborting due to 2 previous errors
816

9-
For more information about this error, try `rustc --explain E0308`.
17+
Some errors have detailed explanations: E0308, E0369.
18+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)