Skip to content

Commit 2fae59a

Browse files
authored
Rollup merge of rust-lang#144864 - Muscraft:no-source-fixes, r=jieyouxu
No source fixes This PR started as a fix for a rendering bug that [got noticed in rust-lang#143661](rust-lang#143661 (comment)), but turned into a fix for any rendering bugs related to files with no source. - Don't add an end column separator after a file with no source - Add column separator before secondary messages with no source - Render continuation between no source labels Before ``` error[E0423]: expected function, tuple struct or tuple variant, found struct `std::collections::HashMap` ╭▸ $DIR/multi-suggestion.rs:17:13 │ LL │ let _ = std::collections::HashMap(); │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━ ╭▸ $SRC_DIR/std/src/collections/hash/map.rs:LL:COL │ ╰ note: `std::collections::HashMap` defined here ╰╴ note: constructor is not visible here due to private fields ╭▸ $SRC_DIR/alloc/src/boxed.rs:LL:COL │ ╰ note: private field │ ╰ note: private field ``` After ``` error[E0423]: expected function, tuple struct or tuple variant, found struct `std::collections::HashMap` ╭▸ $DIR/multi-suggestion.rs:17:13 │ LL │ let _ = std::collections::HashMap(); │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━ ╰╴ ╭▸ $SRC_DIR/std/src/collections/hash/map.rs:LL:COL │ ╰ note: `std::collections::HashMap` defined here note: constructor is not visible here due to private fields ╭▸ $SRC_DIR/alloc/src/boxed.rs:LL:COL │ ├ note: private field │ ╰ note: private field ``` Note: This PR also makes it so `rustc` and `annotate-snippets` match in these cases
2 parents a65ed63 + 2bd47d2 commit 2fae59a

File tree

40 files changed

+128
-43
lines changed

40 files changed

+128
-43
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ impl HumanEmitter {
14621462
max_line_num_len: usize,
14631463
is_secondary: bool,
14641464
is_cont: bool,
1465-
) -> io::Result<()> {
1465+
) -> io::Result<CodeWindowStatus> {
14661466
let mut buffer = StyledBuffer::new();
14671467

14681468
if !msp.has_primary_spans() && !msp.has_span_labels() && is_secondary && !self.short_message
@@ -1575,12 +1575,14 @@ impl HumanEmitter {
15751575
}
15761576
let mut annotated_files = FileWithAnnotatedLines::collect_annotations(self, args, msp);
15771577
trace!("{annotated_files:#?}");
1578+
let mut code_window_status = CodeWindowStatus::Open;
15781579

15791580
// Make sure our primary file comes first
15801581
let primary_span = msp.primary_span().unwrap_or_default();
15811582
let (Some(sm), false) = (self.sm.as_ref(), primary_span.is_dummy()) else {
15821583
// If we don't have span information, emit and exit
1583-
return emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message);
1584+
return emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)
1585+
.map(|_| code_window_status);
15841586
};
15851587
let primary_lo = sm.lookup_char_pos(primary_span.lo());
15861588
if let Ok(pos) =
@@ -1589,6 +1591,9 @@ impl HumanEmitter {
15891591
annotated_files.swap(0, pos);
15901592
}
15911593

1594+
// An end column separator should be emitted when a file with with a
1595+
// source, is followed by one without a source
1596+
let mut col_sep_before_no_show_source = false;
15921597
let annotated_files_len = annotated_files.len();
15931598
// Print out the annotate source lines that correspond with the error
15941599
for (file_idx, annotated_file) in annotated_files.into_iter().enumerate() {
@@ -1599,6 +1604,26 @@ impl HumanEmitter {
15991604
&annotated_file.file,
16001605
) {
16011606
if !self.short_message {
1607+
// Add an end column separator when a file without a source
1608+
// comes after one with a source
1609+
// ╭▸ $DIR/deriving-meta-unknown-trait.rs:1:10
1610+
// │
1611+
// LL │ #[derive(Eqr)]
1612+
// │ ━━━
1613+
// ╰╴ (<- It prints *this* line)
1614+
// ╭▸ $SRC_DIR/core/src/cmp.rs:356:0
1615+
// │
1616+
// ╰╴note: similarly named derive macro `Eq` defined here
1617+
if col_sep_before_no_show_source {
1618+
let buffer_msg_line_offset = buffer.num_lines();
1619+
self.draw_col_separator_end(
1620+
&mut buffer,
1621+
buffer_msg_line_offset,
1622+
max_line_num_len + 1,
1623+
);
1624+
}
1625+
col_sep_before_no_show_source = false;
1626+
16021627
// We'll just print an unannotated message.
16031628
for (annotation_id, line) in annotated_file.lines.iter().enumerate() {
16041629
let mut annotations = line.annotations.clone();
@@ -1639,29 +1664,42 @@ impl HumanEmitter {
16391664
}
16401665
line_idx += 1;
16411666
}
1642-
for (label, is_primary) in labels.into_iter() {
1667+
if is_cont
1668+
&& file_idx == annotated_files_len - 1
1669+
&& annotation_id == annotated_file.lines.len() - 1
1670+
&& !labels.is_empty()
1671+
{
1672+
code_window_status = CodeWindowStatus::Closed;
1673+
}
1674+
let labels_len = labels.len();
1675+
for (label_idx, (label, is_primary)) in labels.into_iter().enumerate() {
16431676
let style = if is_primary {
16441677
Style::LabelPrimary
16451678
} else {
16461679
Style::LabelSecondary
16471680
};
1648-
let pipe = self.col_separator();
1649-
buffer.prepend(line_idx, &format!(" {pipe}"), Style::LineNumber);
1650-
for _ in 0..max_line_num_len {
1651-
buffer.prepend(line_idx, " ", Style::NoStyle);
1652-
}
1681+
self.draw_col_separator_no_space(
1682+
&mut buffer,
1683+
line_idx,
1684+
max_line_num_len + 1,
1685+
);
16531686
line_idx += 1;
1654-
let chr = self.note_separator();
1655-
buffer.append(line_idx, &format!(" {chr} note: "), style);
1656-
for _ in 0..max_line_num_len {
1657-
buffer.prepend(line_idx, " ", Style::NoStyle);
1658-
}
1687+
self.draw_note_separator(
1688+
&mut buffer,
1689+
line_idx,
1690+
max_line_num_len + 1,
1691+
label_idx != labels_len - 1,
1692+
);
1693+
buffer.append(line_idx, "note", Style::MainHeaderMsg);
1694+
buffer.append(line_idx, ": ", Style::NoStyle);
16591695
buffer.append(line_idx, label, style);
16601696
line_idx += 1;
16611697
}
16621698
}
16631699
}
16641700
continue;
1701+
} else {
1702+
col_sep_before_no_show_source = true;
16651703
}
16661704

16671705
// print out the span location and spacer before we print the annotated source
@@ -1976,7 +2014,7 @@ impl HumanEmitter {
19762014
// final step: take our styled buffer, render it, then output it
19772015
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
19782016

1979-
Ok(())
2017+
Ok(code_window_status)
19802018
}
19812019

19822020
fn column_width(&self, code_offset: usize) -> usize {
@@ -2491,7 +2529,7 @@ impl HumanEmitter {
24912529
!children.is_empty()
24922530
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden),
24932531
) {
2494-
Ok(()) => {
2532+
Ok(code_window_status) => {
24952533
if !children.is_empty()
24962534
|| suggestions.iter().any(|s| s.style != SuggestionStyle::CompletelyHidden)
24972535
{
@@ -2502,7 +2540,7 @@ impl HumanEmitter {
25022540
{
25032541
// We'll continue the vertical bar to point into the next note.
25042542
self.draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
2505-
} else {
2543+
} else if matches!(code_window_status, CodeWindowStatus::Open) {
25062544
// We'll close the vertical bar to visually end the code window.
25072545
self.draw_col_separator_end(&mut buffer, 0, max_line_num_len + 1);
25082546
}
@@ -2829,10 +2867,11 @@ impl HumanEmitter {
28292867
}
28302868
}
28312869

2832-
fn note_separator(&self) -> char {
2870+
fn note_separator(&self, is_cont: bool) -> &'static str {
28332871
match self.theme {
2834-
OutputTheme::Ascii => '=',
2835-
OutputTheme::Unicode => '╰',
2872+
OutputTheme::Ascii => "= ",
2873+
OutputTheme::Unicode if is_cont => "├ ",
2874+
OutputTheme::Unicode => "╰ ",
28362875
}
28372876
}
28382877

@@ -2945,11 +2984,7 @@ impl HumanEmitter {
29452984
col: usize,
29462985
is_cont: bool,
29472986
) {
2948-
let chr = match self.theme {
2949-
OutputTheme::Ascii => "= ",
2950-
OutputTheme::Unicode if is_cont => "├ ",
2951-
OutputTheme::Unicode => "╰ ",
2952-
};
2987+
let chr = self.note_separator(is_cont);
29532988
buffer.puts(line, col, chr, Style::LineNumber);
29542989
}
29552990

@@ -3050,6 +3085,12 @@ enum DisplaySuggestion {
30503085
Add,
30513086
}
30523087

3088+
#[derive(Clone, Copy, Debug)]
3089+
enum CodeWindowStatus {
3090+
Closed,
3091+
Open,
3092+
}
3093+
30533094
impl FileWithAnnotatedLines {
30543095
/// Preprocess all the annotations so that they are grouped by file and by line number
30553096
/// This helps us quickly iterate over the whole message (including secondary file spans)

tests/ui/async-await/in-trait/dont-project-to-specializable-projection.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ error[E0599]: no method named `poll` found for struct `Pin<&mut impl Future<Outp
2525
|
2626
LL | match fut.as_mut().poll(ctx) {
2727
| ^^^^ method not found in `Pin<&mut impl Future<Output = ()>>`
28+
|
2829
--> $SRC_DIR/core/src/future/future.rs:LL:COL
2930
|
3031
= note: the method is available for `Pin<&mut impl Future<Output = ()>>` here

tests/ui/c-variadic/issue-86053-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ error[E0412]: cannot find type `F` in this scope
5757
|
5858
LL | self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize ) {
5959
| ^
60+
|
6061
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
6162
|
6263
= note: similarly named trait `Fn` defined here
63-
|
6464
help: a trait with a similar name exists
6565
|
6666
LL | self , ... , self , self , ... ) where Fn : FnOnce ( & 'a & 'b usize ) {

tests/ui/closures/issue-78720.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ error[E0412]: cannot find type `F` in this scope
99
|
1010
LL | _func: F,
1111
| ^
12+
|
1213
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
1314
|
1415
= note: similarly named trait `Fn` defined here
15-
|
1616
help: a trait with a similar name exists
1717
|
1818
LL | _func: Fn,

tests/ui/closures/issue-90871.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ error[E0412]: cannot find type `n` in this scope
33
|
44
LL | type_ascribe!(2, n([u8; || 1]))
55
| ^ help: a trait with a similar name exists: `Fn`
6+
|
67
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
78
|
89
= note: similarly named trait `Fn` defined here

tests/ui/const-generics/generic_const_exprs/expected-type-of-closure-body-to-be-a-closure-or-coroutine-ice-113776.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ error[E0412]: cannot find type `F` in this scope
33
|
44
LL | let f: F = async { 1 };
55
| ^
6+
|
67
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
78
|
89
= note: similarly named trait `Fn` defined here
9-
|
1010
help: a trait with a similar name exists
1111
|
1212
LL | let f: Fn = async { 1 };

tests/ui/consts/issue-89088.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | const FOO: &A = &A::Field(Cow::Borrowed("foo"));
66
...
77
LL | FOO => todo!(),
88
| ^^^ constant of non-structural type
9+
|
910
--> $SRC_DIR/alloc/src/borrow.rs:LL:COL
1011
|
1112
= note: `Cow<'_, str>` must be annotated with `#[derive(PartialEq)]` to be usable in patterns

tests/ui/derives/deriving-meta-unknown-trait.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ error: cannot find derive macro `Eqr` in this scope
33
|
44
LL | #[derive(Eqr)]
55
| ^^^ help: a derive macro with a similar name exists: `Eq`
6+
|
67
--> $SRC_DIR/core/src/cmp.rs:LL:COL
78
|
89
= note: similarly named derive macro `Eq` defined here
@@ -12,6 +13,7 @@ error: cannot find derive macro `Eqr` in this scope
1213
|
1314
LL | #[derive(Eqr)]
1415
| ^^^ help: a derive macro with a similar name exists: `Eq`
16+
|
1517
--> $SRC_DIR/core/src/cmp.rs:LL:COL
1618
|
1719
= note: similarly named derive macro `Eq` defined here

tests/ui/did_you_mean/println-typo.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ error: cannot find macro `prinltn` in this scope
33
|
44
LL | prinltn!();
55
| ^^^^^^^ help: a macro with a similar name exists: `println`
6+
|
67
--> $SRC_DIR/std/src/macros.rs:LL:COL
78
|
89
= note: similarly named macro `println` defined here

tests/ui/impl-trait/call_method_without_import.no_import.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ error[E0599]: no method named `fmt` found for opaque type `impl Debug` in the cu
33
|
44
LL | x.fmt(f);
55
| ^^^ method not found in `impl Debug`
6+
|
67
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
78
|
89
= note: the method is available for `impl Debug` here

0 commit comments

Comments
 (0)