Skip to content

Commit 59554a2

Browse files
committed
Avoid rendering empty annotations
1 parent 2e2a479 commit 59554a2

Some content is hidden

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

43 files changed

+94
-195
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,51 +1408,58 @@ impl EmitterWriter {
14081408
if !sm.ensure_source_file_source_present(annotated_file.file.clone()) {
14091409
if !self.short_message {
14101410
// We'll just print an unannotated message.
1411-
for (annotation_id, line) in annotated_file.lines.into_iter().enumerate() {
1411+
for (annotation_id, line) in annotated_file.lines.iter().enumerate() {
14121412
let mut annotations = line.annotations.clone();
14131413
annotations.sort_by_key(|a| Reverse(a.start_col));
14141414
let mut line_idx = buffer.num_lines();
1415-
buffer.append(
1416-
line_idx,
1417-
&format!(
1418-
"{}:{}:{}",
1419-
sm.filename_for_diagnostics(&annotated_file.file.name),
1420-
sm.doctest_offset_line(&annotated_file.file.name, line.line_index),
1421-
annotations[0].start_col + 1,
1422-
),
1423-
Style::LineAndColumn,
1424-
);
1425-
if annotation_id == 0 {
1426-
buffer.prepend(line_idx, "--> ", Style::LineNumber);
1415+
1416+
let labels: Vec<_> = annotations
1417+
.iter()
1418+
.filter_map(|a| Some((a.label.as_ref()?, a.is_primary)))
1419+
.filter(|(l, _)| !l.is_empty())
1420+
.collect();
1421+
1422+
if annotation_id == 0 || !labels.is_empty() {
1423+
buffer.append(
1424+
line_idx,
1425+
&format!(
1426+
"{}:{}:{}",
1427+
sm.filename_for_diagnostics(&annotated_file.file.name),
1428+
sm.doctest_offset_line(
1429+
&annotated_file.file.name,
1430+
line.line_index
1431+
),
1432+
annotations[0].start_col + 1,
1433+
),
1434+
Style::LineAndColumn,
1435+
);
1436+
if annotation_id == 0 {
1437+
buffer.prepend(line_idx, "--> ", Style::LineNumber);
1438+
} else {
1439+
buffer.prepend(line_idx, "::: ", Style::LineNumber);
1440+
}
14271441
for _ in 0..max_line_num_len {
14281442
buffer.prepend(line_idx, " ", Style::NoStyle);
14291443
}
14301444
line_idx += 1;
1431-
};
1432-
for (i, annotation) in annotations.into_iter().enumerate() {
1433-
if let Some(label) = &annotation.label {
1434-
if !label.is_empty() {
1435-
let style = if annotation.is_primary {
1436-
Style::LabelPrimary
1437-
} else {
1438-
Style::LabelSecondary
1439-
};
1440-
if annotation_id == 0 {
1441-
buffer.prepend(line_idx, " |", Style::LineNumber);
1442-
for _ in 0..max_line_num_len {
1443-
buffer.prepend(line_idx, " ", Style::NoStyle);
1444-
}
1445-
line_idx += 1;
1446-
buffer.append(line_idx + i, " = note: ", style);
1447-
for _ in 0..max_line_num_len {
1448-
buffer.prepend(line_idx, " ", Style::NoStyle);
1449-
}
1450-
} else {
1451-
buffer.append(line_idx + i, ": ", style);
1452-
}
1453-
buffer.append(line_idx + i, label, style);
1454-
}
1445+
}
1446+
for (label, is_primary) in labels.into_iter() {
1447+
let style = if is_primary {
1448+
Style::LabelPrimary
1449+
} else {
1450+
Style::LabelSecondary
1451+
};
1452+
buffer.prepend(line_idx, " |", Style::LineNumber);
1453+
for _ in 0..max_line_num_len {
1454+
buffer.prepend(line_idx, " ", Style::NoStyle);
1455+
}
1456+
line_idx += 1;
1457+
buffer.append(line_idx, " = note: ", style);
1458+
for _ in 0..max_line_num_len {
1459+
buffer.prepend(line_idx, " ", Style::NoStyle);
14551460
}
1461+
buffer.append(line_idx, label, style);
1462+
line_idx += 1;
14561463
}
14571464
}
14581465
}

src/test/ui/closures/closure-expected.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ LL | let y = x.or_else(4);
1010
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
1111
note: required by a bound in `Option::<T>::or_else`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL
13-
$SRC_DIR/core/src/option.rs:LL:COL
14-
$SRC_DIR/core/src/option.rs:LL:COL
15-
$SRC_DIR/core/src/option.rs:LL:COL
1613

1714
error: aborting due to previous error
1815

src/test/ui/closures/closure-move-sync.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ LL | let t = thread::spawn(|| {
1919
| ^^
2020
note: required by a bound in `spawn`
2121
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
22-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
23-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
24-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
25-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
2622

2723
error[E0277]: `Sender<()>` cannot be shared between threads safely
2824
--> $DIR/closure-move-sync.rs:18:19
@@ -41,10 +37,6 @@ LL | thread::spawn(|| tx.send(()).unwrap());
4137
| ^^
4238
note: required by a bound in `spawn`
4339
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
44-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
45-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
46-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
47-
$SRC_DIR/std/src/thread/mod.rs:LL:COL
4840

4941
error: aborting due to 2 previous errors
5042

src/test/ui/closures/coerce-unsafe-to-closure.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
1010
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `Option::<T>::map`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL
13-
$SRC_DIR/core/src/option.rs:LL:COL
14-
$SRC_DIR/core/src/option.rs:LL:COL
15-
$SRC_DIR/core/src/option.rs:LL:COL
1613

1714
error: aborting due to previous error
1815

src/test/ui/error-codes/E0004-2.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ LL | match x { }
66
|
77
note: `Option<i32>` defined here
88
--> $SRC_DIR/core/src/option.rs:LL:COL
9-
$SRC_DIR/core/src/option.rs:LL:COL: not covered
10-
$SRC_DIR/core/src/option.rs:LL:COL: not covered
9+
::: $SRC_DIR/core/src/option.rs:LL:COL
10+
|
11+
= note: not covered
12+
::: $SRC_DIR/core/src/option.rs:LL:COL
13+
|
14+
= note: not covered
1115
= note: the matched value is of type `Option<i32>`
1216
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
1317
|

src/test/ui/error-codes/E0005.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ LL | let Some(y) = x;
88
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
99
note: `Option<i32>` defined here
1010
--> $SRC_DIR/core/src/option.rs:LL:COL
11-
$SRC_DIR/core/src/option.rs:LL:COL: not covered
11+
::: $SRC_DIR/core/src/option.rs:LL:COL
12+
|
13+
= note: not covered
1214
= note: the matched value is of type `Option<i32>`
1315
help: you might want to use `if let` to ignore the variant that isn't matched
1416
|

src/test/ui/error-codes/E0297.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ LL | for Some(x) in xs {}
66
|
77
note: `Option<i32>` defined here
88
--> $SRC_DIR/core/src/option.rs:LL:COL
9-
$SRC_DIR/core/src/option.rs:LL:COL: not covered
9+
::: $SRC_DIR/core/src/option.rs:LL:COL
10+
|
11+
= note: not covered
1012
= note: the matched value is of type `Option<i32>`
1113

1214
error: aborting due to previous error

src/test/ui/expr/malformed_closure/ruby_style_closure.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ LL | | });
2222
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<_>`
2323
note: required by a bound in `Option::<T>::and_then`
2424
--> $SRC_DIR/core/src/option.rs:LL:COL
25-
$SRC_DIR/core/src/option.rs:LL:COL
26-
$SRC_DIR/core/src/option.rs:LL:COL
27-
$SRC_DIR/core/src/option.rs:LL:COL
2825

2926
error: aborting due to 2 previous errors
3027

src/test/ui/feature-gates/feature-gate-exhaustive-patterns.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ LL | let Ok(_x) = foo();
88
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
99
note: `Result<u32, !>` defined here
1010
--> $SRC_DIR/core/src/result.rs:LL:COL
11-
$SRC_DIR/core/src/result.rs:LL:COL: not covered
11+
::: $SRC_DIR/core/src/result.rs:LL:COL
12+
|
13+
= note: not covered
1214
= note: the matched value is of type `Result<u32, !>`
1315
help: you might want to use `if let` to ignore the variant that isn't matched
1416
|

src/test/ui/inference/issue-71732.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ LL | .get(&"key".into())
1212
where T: ?Sized;
1313
note: required by a bound in `HashMap::<K, V, S>::get`
1414
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
15-
$SRC_DIR/std/src/collections/hash/map.rs:LL:COL
16-
$SRC_DIR/std/src/collections/hash/map.rs:LL:COL
17-
$SRC_DIR/std/src/collections/hash/map.rs:LL:COL
1815
help: consider specifying the generic argument
1916
|
2017
LL | .get::<Q>(&"key".into())

0 commit comments

Comments
 (0)