Skip to content

Commit

Permalink
Rollup merge of #126882 - estebank:multiline-order, r=WaffleLapkin
Browse files Browse the repository at this point in the history
Special case when a code line only has multiline span starts

Minimize multline span overlap when there are multiple of them starting on the same line:

```
3 |       X0 Y0 Z0
  |  _____^  -  -
  | | _______|  |
  | || _________|
4 | |||   X1 Y1 Z1
5 | |||   X2 Y2 Z2
  | |||____^__-__- `Z` label
  | ||_____|__|
  | |______|  `Y` is a good letter too
  |        `X` is a good letter
```
  • Loading branch information
matthiaskrgr authored Jun 24, 2024
2 parents 8bfde60 + 284437d commit a80ee91
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
27 changes: 26 additions & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl HumanEmitter {
// <EMPTY LINE>
//
let mut annotations_position = vec![];
let mut line_len = 0;
let mut line_len: usize = 0;
let mut p = 0;
for (i, annotation) in annotations.iter().enumerate() {
for (j, next) in annotations.iter().enumerate() {
Expand Down Expand Up @@ -973,6 +973,31 @@ impl HumanEmitter {
return vec![];
}

if annotations_position
.iter()
.all(|(_, ann)| matches!(ann.annotation_type, AnnotationType::MultilineStart(_)))
&& let Some(max_pos) = annotations_position.iter().map(|(pos, _)| *pos).max()
{
// Special case the following, so that we minimize overlapping multiline spans.
//
// 3 │ X0 Y0 Z0
// │ ┏━━━━━┛ │ │ < We are writing these lines
// │ ┃┌───────┘ │ < by reverting the "depth" of
// │ ┃│┌─────────┘ < their multilne spans.
// 4 │ ┃││ X1 Y1 Z1
// 5 │ ┃││ X2 Y2 Z2
// │ ┃│└────╿──│──┘ `Z` label
// │ ┃└─────│──┤
// │ ┗━━━━━━┥ `Y` is a good letter too
// ╰╴ `X` is a good letter
for (pos, _) in &mut annotations_position {
*pos = max_pos - *pos;
}
// We know then that we don't need an additional line for the span label, saving us
// one line of vertical space.
line_len = line_len.saturating_sub(1);
}

// Write the column separator.
//
// After this we will have:
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_parse/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,8 @@ error: foo
--> test.rs:3:3
|
3 | X0 Y0
| ___^__-
| |___|
| ||
| ____^ -
| | ______|
4 | || X1 Y1
5 | || X2 Y2
| ||____^__- `Y` is a good letter too
Expand Down Expand Up @@ -361,9 +360,8 @@ error: foo
--> test.rs:3:3
|
3 | X0 Y0
| ___^__-
| |___|
| ||
| ____^ -
| | ______|
4 | || Y1 X1
| ||____-__^ `X` is a good letter
| |____|
Expand Down Expand Up @@ -445,10 +443,9 @@ error: foo
--> test.rs:3:3
|
3 | X0 Y0 Z0
| ___^__-__-
| |___|__|
| ||___|
| |||
| _____^ - -
| | _______| |
| || _________|
4 | ||| X1 Y1 Z1
5 | ||| X2 Y2 Z2
| |||____^__-__- `Z` label
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/async-await/async-is-unwindsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ error[E0277]: the type `&mut Context<'_>` may not be safely transferred across a
--> $DIR/async-is-unwindsafe.rs:12:5
|
LL | is_unwindsafe(async {
| _____^_____________-
| |_____|
| ||
| ______^ -
| | ___________________|
LL | ||
LL | || use std::ptr::null;
LL | || use std::task::{Context, RawWaker, RawWakerVTable, Waker};
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/lint/suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ warning: variable does not need to be mutable
--> $DIR/suggestions.rs:54:13
|
LL | let mut
| ______________^
| | _____________|
| ||
| _____________^
| |_____________|
LL | || b = 1;
| ||____________-^
| |_____________|
Expand Down

0 comments on commit a80ee91

Please sign in to comment.