From 0ac465924e6ae4380b25c38cbc14f425796fa2af Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 26 Jan 2018 15:33:05 +0000 Subject: [PATCH 1/3] Add line numbers and columns to error messages spanning multiple files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- src/librustc_errors/emitter.rs | 13 ++++++++++++- src/librustc_errors/snippet.rs | 3 ++- src/test/ui/cross-file-errors/main.rs | 16 ++++++++++++++++ src/test/ui/cross-file-errors/main.stderr | 11 +++++++++++ src/test/ui/cross-file-errors/underscore.rs | 20 ++++++++++++++++++++ src/tools/compiletest/src/runtest.rs | 2 +- 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/cross-file-errors/main.rs create mode 100644 src/test/ui/cross-file-errors/main.stderr create mode 100644 src/test/ui/cross-file-errors/underscore.rs diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 58f851aea3817..a9f228ca729b1 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -1013,8 +1013,19 @@ impl EmitterWriter { // Then, the secondary file indicator buffer.prepend(buffer_msg_line_offset + 1, "::: ", Style::LineNumber); + let loc = if let Some(first_line) = annotated_file.lines.first() { + let col = if let Some(first_annotation) = first_line.annotations.first() { + format!(":{}", first_annotation.start_col + 1) + } else { "".to_string() }; + format!("{}:{}{}", + annotated_file.file.name, + cm.doctest_offset_line(first_line.line_index), + col) + } else { + annotated_file.file.name.to_string() + }; buffer.append(buffer_msg_line_offset + 1, - &annotated_file.file.name.to_string(), + &loc, Style::LineAndColumn); for _ in 0..max_line_num_len { buffer.prepend(buffer_msg_line_offset + 1, " ", Style::NoStyle); diff --git a/src/librustc_errors/snippet.rs b/src/librustc_errors/snippet.rs index c2f4701999ea9..6035f33c822ce 100644 --- a/src/librustc_errors/snippet.rs +++ b/src/librustc_errors/snippet.rs @@ -27,7 +27,8 @@ pub struct FileInfo { /// The "primary file", if any, gets a `-->` marker instead of /// `>>>`, and has a line-number/column printed and not just a - /// filename. It appears first in the listing. It is known to + /// filename (other files are not guaranteed to have line numbers + /// or columns). It appears first in the listing. It is known to /// contain at least one primary span, though primary spans (which /// are designated with `^^^`) may also occur in other files. primary_span: Option, diff --git a/src/test/ui/cross-file-errors/main.rs b/src/test/ui/cross-file-errors/main.rs new file mode 100644 index 0000000000000..8eae79a21a983 --- /dev/null +++ b/src/test/ui/cross-file-errors/main.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[macro_use] +mod underscore; + +fn main() { + underscore!(); +} diff --git a/src/test/ui/cross-file-errors/main.stderr b/src/test/ui/cross-file-errors/main.stderr new file mode 100644 index 0000000000000..a1cdae10edfcd --- /dev/null +++ b/src/test/ui/cross-file-errors/main.stderr @@ -0,0 +1,11 @@ +error: expected expression, found `_` + --> $DIR/underscore.rs:18:9 + | +18 | _ + | ^ + | + ::: $DIR/main.rs:15:5 + | +15 | underscore!(); + | -------------- in this macro invocation + diff --git a/src/test/ui/cross-file-errors/underscore.rs b/src/test/ui/cross-file-errors/underscore.rs new file mode 100644 index 0000000000000..312b3b8f4ddd5 --- /dev/null +++ b/src/test/ui/cross-file-errors/underscore.rs @@ -0,0 +1,20 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// We want this file only so we can test cross-file error +// messages, but we don't want it in an external crate. +// ignore-test +#![crate_type = "lib"] + +macro_rules! underscore { + () => ( + _ + ) +} diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index bf5fc00428df2..abf62a060b83b 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1402,7 +1402,7 @@ impl<'test> TestCx<'test> { } /// For each `aux-build: foo/bar` annotation, we check to find the - /// file in a `aux` directory relative to the test itself. + /// file in a `auxiliary` directory relative to the test itself. fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths { let test_ab = self.testpaths .file From aa6cc6e1898067c4311e260960bca776f3e02715 Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 26 Jan 2018 16:56:37 +0000 Subject: [PATCH 2/3] Fix test in macro_backtrace --- src/test/ui/macro_backtrace/main.stderr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/ui/macro_backtrace/main.stderr b/src/test/ui/macro_backtrace/main.stderr index 5990f71b3ca0a..48138ee711b3f 100644 --- a/src/test/ui/macro_backtrace/main.stderr +++ b/src/test/ui/macro_backtrace/main.stderr @@ -22,7 +22,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found 27 | ping!(); | -------- in this macro invocation | - ::: + ::: :1:1 | 1 | ( ) => { pong ! ( ) ; } | ------------------------- @@ -42,7 +42,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found 28 | deep!(); | -------- in this macro invocation (#1) | - ::: + ::: :1:1 | 1 | ( ) => { foo ! ( ) ; } | ------------------------ @@ -50,7 +50,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found | | in this macro invocation (#2) | in this expansion of `deep!` (#1) | - ::: + ::: :1:1 | 1 | ( ) => { bar ! ( ) ; } | ------------------------ @@ -58,7 +58,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found | | in this macro invocation (#3) | in this expansion of `foo!` (#2) | - ::: + ::: :1:1 | 1 | ( ) => { ping ! ( ) ; } | ------------------------- @@ -66,7 +66,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found | | in this macro invocation (#4) | in this expansion of `bar!` (#3) | - ::: + ::: :1:1 | 1 | ( ) => { pong ! ( ) ; } | ------------------------- From a21b7b3b162932011b83f4703fb87030c216e962 Mon Sep 17 00:00:00 2001 From: varkor Date: Sat, 27 Jan 2018 15:16:42 +0000 Subject: [PATCH 3/3] Improve formatting of else block --- src/librustc_errors/emitter.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index a9f228ca729b1..63c4ae5561c92 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -1016,7 +1016,9 @@ impl EmitterWriter { let loc = if let Some(first_line) = annotated_file.lines.first() { let col = if let Some(first_annotation) = first_line.annotations.first() { format!(":{}", first_annotation.start_col + 1) - } else { "".to_string() }; + } else { + "".to_string() + }; format!("{}:{}{}", annotated_file.file.name, cm.doctest_offset_line(first_line.line_index),