Skip to content

Commit

Permalink
Make trimming logic work on more than one span at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 28, 2025
1 parent ad69b6a commit bf334c2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 29 deletions.
37 changes: 25 additions & 12 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,16 @@ impl HumanEmitter {
// | x_span
// <EMPTY LINE>
//
let mut overlap = vec![false; annotations.len()];
let mut annotations_position = vec![];
let mut line_len: usize = 0;
let mut p = 0;
for (i, annotation) in annotations.iter().enumerate() {
for (j, next) in annotations.iter().enumerate() {
if overlaps(next, annotation, 0) && j > i {
overlap[i] = true;
overlap[j] = true;
}
if overlaps(next, annotation, 0) // This label overlaps with another one and both
&& annotation.has_label() // take space (they have text and are not
&& j > i // multiline lines).
Expand Down Expand Up @@ -1269,13 +1274,17 @@ impl HumanEmitter {
// We look for individual *long* spans, and we trim the *middle*, so that we render
// LL | ...= [0, 0, 0, ..., 0, 0];
// | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
for &(pos, annotation) in &annotations_position {
for (i, (_pos, annotation)) in annotations_position.iter().enumerate() {
// Skip cases where multiple spans overlap each other.
if overlap[i] {
continue;
};
let AnnotationType::Singleline = annotation.annotation_type else { continue };
let width = annotation.end_col.display - annotation.start_col.display;
if pos == 0 && width > margin.column_width && width > 10 {
if width > margin.column_width * 2 && width > 10 {
// If the terminal is *too* small, we keep at least a tiny bit of the span for
// display.
let pad = max(margin.column_width / 2, 5);
let pad = max(margin.column_width / 3, 5);
// Code line
buffer.replace(
line_offset,
Expand Down Expand Up @@ -1800,15 +1809,7 @@ impl HumanEmitter {
width_offset + annotated_file.multiline_depth + 1
};

let column_width = if let Some(width) = self.diagnostic_width {
width.saturating_sub(code_offset)
} else if self.ui_testing || cfg!(miri) {
DEFAULT_COLUMN_WIDTH
} else {
termize::dimensions()
.map(|(w, _)| w.saturating_sub(code_offset))
.unwrap_or(DEFAULT_COLUMN_WIDTH)
};
let column_width = self.column_width(code_offset);

let margin = Margin::new(
whitespace_margin,
Expand Down Expand Up @@ -1965,6 +1966,18 @@ impl HumanEmitter {
Ok(())
}

fn column_width(&self, code_offset: usize) -> usize {
if let Some(width) = self.diagnostic_width {
width.saturating_sub(code_offset)
} else if self.ui_testing || cfg!(miri) {
DEFAULT_COLUMN_WIDTH
} else {
termize::dimensions()
.map(|(w, _)| w.saturating_sub(code_offset))
.unwrap_or(DEFAULT_COLUMN_WIDTH)
}
}

fn emit_suggestion_default(
&mut self,
span: &MultiSpan,
Expand Down
19 changes: 14 additions & 5 deletions tests/ui/diagnostic-width/long-span.long.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
╭▸ $DIR/long-span.rs:7:5056
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];
│ ┬───────────────────────────…────────────────────── ━ ────────────────────────────…────────────────────── [{integer}; 1680]
│ │
╰╴ [{integer}; 1680]

error[E0308]: mismatched types
╭▸ $DIR/long-span.rs:7:15
╭▸ $DIR/long-span.rs:9:15
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];
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━…━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, , 0, 0, 0, 0, 0, 0, 0];
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.
19 changes: 14 additions & 5 deletions tests/ui/diagnostic-width/long-span.longest.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
--> $DIR/long-span.rs:7:5056
|
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];
| -----------------------------------------...----------------------------------- ^ -----------------------------------------...----------------------------------- [{integer}; 1680]
| |
| [{integer}; 1680]

error[E0308]: mismatched types
--> $DIR/long-span.rs:7:15
--> $DIR/long-span.rs:9:15
|
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];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`
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];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

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

0 comments on commit bf334c2

Please sign in to comment.