Skip to content

Commit 44b9641

Browse files
authored
Rollup merge of rust-lang#47780 - varkor:cross-file-errors-line-col, r=estebank
Add line numbers and columns to error messages spanning multiple files If an error message is emitted that spans several files, only the primary file currently has line and column data attached. This is useful information, even in files other than the one in which the error occurs. We can often work out which line and column the error corresponds to in other files — in this case it is helpful to add them (in the case of ambiguity, the first relevant line/column is picked, which is still helpful than none).
2 parents 4dbfc8d + a21b7b3 commit 44b9641

File tree

7 files changed

+69
-8
lines changed

7 files changed

+69
-8
lines changed

src/librustc_errors/emitter.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,21 @@ impl EmitterWriter {
10141014

10151015
// Then, the secondary file indicator
10161016
buffer.prepend(buffer_msg_line_offset + 1, "::: ", Style::LineNumber);
1017+
let loc = if let Some(first_line) = annotated_file.lines.first() {
1018+
let col = if let Some(first_annotation) = first_line.annotations.first() {
1019+
format!(":{}", first_annotation.start_col + 1)
1020+
} else {
1021+
"".to_string()
1022+
};
1023+
format!("{}:{}{}",
1024+
annotated_file.file.name,
1025+
cm.doctest_offset_line(first_line.line_index),
1026+
col)
1027+
} else {
1028+
annotated_file.file.name.to_string()
1029+
};
10171030
buffer.append(buffer_msg_line_offset + 1,
1018-
&annotated_file.file.name.to_string(),
1031+
&loc,
10191032
Style::LineAndColumn);
10201033
for _ in 0..max_line_num_len {
10211034
buffer.prepend(buffer_msg_line_offset + 1, " ", Style::NoStyle);

src/librustc_errors/snippet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ pub struct FileInfo {
2727

2828
/// The "primary file", if any, gets a `-->` marker instead of
2929
/// `>>>`, and has a line-number/column printed and not just a
30-
/// filename. It appears first in the listing. It is known to
30+
/// filename (other files are not guaranteed to have line numbers
31+
/// or columns). It appears first in the listing. It is known to
3132
/// contain at least one primary span, though primary spans (which
3233
/// are designated with `^^^`) may also occur in other files.
3334
primary_span: Option<Span>,

src/test/ui/cross-file-errors/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[macro_use]
12+
mod underscore;
13+
14+
fn main() {
15+
underscore!();
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: expected expression, found `_`
2+
--> $DIR/underscore.rs:18:9
3+
|
4+
18 | _
5+
| ^
6+
|
7+
::: $DIR/main.rs:15:5
8+
|
9+
15 | underscore!();
10+
| -------------- in this macro invocation
11+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// We want this file only so we can test cross-file error
12+
// messages, but we don't want it in an external crate.
13+
// ignore-test
14+
#![crate_type = "lib"]
15+
16+
macro_rules! underscore {
17+
() => (
18+
_
19+
)
20+
}

src/test/ui/macro_backtrace/main.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
2222
27 | ping!();
2323
| -------- in this macro invocation
2424
|
25-
::: <ping macros>
25+
::: <ping macros>:1:1
2626
|
2727
1 | ( ) => { pong ! ( ) ; }
2828
| -------------------------
@@ -42,31 +42,31 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
4242
28 | deep!();
4343
| -------- in this macro invocation (#1)
4444
|
45-
::: <deep macros>
45+
::: <deep macros>:1:1
4646
|
4747
1 | ( ) => { foo ! ( ) ; }
4848
| ------------------------
4949
| | |
5050
| | in this macro invocation (#2)
5151
| in this expansion of `deep!` (#1)
5252
|
53-
::: <foo macros>
53+
::: <foo macros>:1:1
5454
|
5555
1 | ( ) => { bar ! ( ) ; }
5656
| ------------------------
5757
| | |
5858
| | in this macro invocation (#3)
5959
| in this expansion of `foo!` (#2)
6060
|
61-
::: <bar macros>
61+
::: <bar macros>:1:1
6262
|
6363
1 | ( ) => { ping ! ( ) ; }
6464
| -------------------------
6565
| | |
6666
| | in this macro invocation (#4)
6767
| in this expansion of `bar!` (#3)
6868
|
69-
::: <ping macros>
69+
::: <ping macros>:1:1
7070
|
7171
1 | ( ) => { pong ! ( ) ; }
7272
| -------------------------

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ impl<'test> TestCx<'test> {
14021402
}
14031403

14041404
/// For each `aux-build: foo/bar` annotation, we check to find the
1405-
/// file in a `aux` directory relative to the test itself.
1405+
/// file in a `auxiliary` directory relative to the test itself.
14061406
fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
14071407
let test_ab = self.testpaths
14081408
.file

0 commit comments

Comments
 (0)