Skip to content

Commit b1e7cbe

Browse files
committed
Ensure source file present when calculating max line number
In the case where there was a non-dummy span but there was no local file present we would include the line number in the max line number calculation but the sub diagnostic would not be emitted.
1 parent 218a96c commit b1e7cbe

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler/rustc_errors/src/emitter.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1131,16 +1131,23 @@ impl EmitterWriter {
11311131
None => return 0,
11321132
};
11331133

1134+
let will_be_emitted = |span: &Span| {
1135+
!span.is_dummy() && {
1136+
let file = sm.lookup_source_file(span.hi());
1137+
sm.ensure_source_file_source_present(file)
1138+
}
1139+
};
1140+
11341141
let mut max = 0;
11351142
for primary_span in msp.primary_spans() {
1136-
if !primary_span.is_dummy() {
1143+
if will_be_emitted(primary_span) {
11371144
let hi = sm.lookup_char_pos(primary_span.hi());
11381145
max = (hi.line).max(max);
11391146
}
11401147
}
11411148
if !self.short_message {
11421149
for span_label in msp.span_labels() {
1143-
if !span_label.span.is_dummy() {
1150+
if will_be_emitted(&span_label.span) {
11441151
let hi = sm.lookup_char_pos(span_label.span.hi());
11451152
max = (hi.line).max(max);
11461153
}

src/test/ui/issues/issue-71363.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-fail
2+
// compile-flags: -Z simulate-remapped-rust-src-base=/rustc/xyz -Z ui-testing=no
3+
4+
#[derive(Debug)]
5+
struct Test;
6+
impl std::error::Error for Test {}
7+
//~^ ERROR `Test` doesn't implement `std::fmt::Display`
8+
9+
fn main() {}

src/test/ui/issues/issue-71363.stderr

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: `Test` doesn't implement `std::fmt::Display`
2+
--> $DIR/issue-71363.rs:6:6
3+
|
4+
6 | impl std::error::Error for Test {}
5+
| ^^^^^^^^^^^^^^^^^ `Test` cannot be formatted with the default formatter
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `Test`
8+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
9+
note: required by a bound in `std::error::Error`
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)