From 504acf876035fa2b944c64d01f6fe66b30acf1e4 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Mon, 24 Jul 2023 14:40:44 +0200 Subject: [PATCH] Make `--error-format human-annotate-rs` handle multiple files --- .../src/annotate_snippet_emitter_writer.rs | 20 ++++++++++++------- .../annotate-snippet/auxiliary/other_file.rs | 6 ++++++ tests/ui/annotate-snippet/multiple-files.rs | 8 ++++++++ .../ui/annotate-snippet/multiple-files.stderr | 11 ++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 tests/ui/annotate-snippet/auxiliary/other_file.rs create mode 100644 tests/ui/annotate-snippet/multiple-files.rs create mode 100644 tests/ui/annotate-snippet/multiple-files.stderr diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index 9872b3bda1e..a88fba6dae6 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -157,10 +157,8 @@ fn emit_messages_default( { annotated_files.swap(0, pos); } - // owned: line source, line index, annotations - type Owned = (String, usize, Vec); - let filename = source_map.filename_for_diagnostics(&primary_lo.file.name); - let origin = filename.to_string_lossy(); + // owned: file name, line source, line index, annotations + type Owned = (String, String, usize, Vec); let annotated_files: Vec = annotated_files .into_iter() .flat_map(|annotated_file| { @@ -169,7 +167,15 @@ fn emit_messages_default( .lines .into_iter() .map(|line| { - (source_string(file.clone(), &line), line.line_index, line.annotations) + // Ensure the source file is present before we try + // to load a string from it. + source_map.ensure_source_file_source_present(file.clone()); + ( + format!("{}", source_map.filename_for_diagnostics(&file.name)), + source_string(file.clone(), &line), + line.line_index, + line.annotations, + ) }) .collect::>() }) @@ -192,11 +198,11 @@ fn emit_messages_default( }, slices: annotated_files .iter() - .map(|(source, line_index, annotations)| { + .map(|(file_name, source, line_index, annotations)| { Slice { source, line_start: *line_index, - origin: Some(&origin), + origin: Some(&file_name), // FIXME(#59346): Not really sure when `fold` should be true or false fold: false, annotations: annotations diff --git a/tests/ui/annotate-snippet/auxiliary/other_file.rs b/tests/ui/annotate-snippet/auxiliary/other_file.rs new file mode 100644 index 00000000000..6f5f412d086 --- /dev/null +++ b/tests/ui/annotate-snippet/auxiliary/other_file.rs @@ -0,0 +1,6 @@ +pub struct WithPrivateMethod; + +impl WithPrivateMethod { + /// Private to get an error involving two files + fn private_method(&self) {} +} diff --git a/tests/ui/annotate-snippet/multiple-files.rs b/tests/ui/annotate-snippet/multiple-files.rs new file mode 100644 index 00000000000..981cdbb10a9 --- /dev/null +++ b/tests/ui/annotate-snippet/multiple-files.rs @@ -0,0 +1,8 @@ +// aux-build:other_file.rs +// compile-flags: --error-format human-annotate-rs -Z unstable-options + +extern crate other_file; + +fn main() { + other_file::WithPrivateMethod.private_method(); +} diff --git a/tests/ui/annotate-snippet/multiple-files.stderr b/tests/ui/annotate-snippet/multiple-files.stderr new file mode 100644 index 00000000000..4236ec811d0 --- /dev/null +++ b/tests/ui/annotate-snippet/multiple-files.stderr @@ -0,0 +1,11 @@ +error[E0624]: method `private_method` is private + --> $DIR/multiple-files.rs:7:35 + | +LL | other_file::WithPrivateMethod.private_method(); + | ^^^^^^^^^^^^^^ private method + | + ::: $DIR/auxiliary/other_file.rs:5:5 + | +LL | fn private_method(&self) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ private method defined here + |