Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ impl AnnotateSnippetEmitterWriter {
{
annotated_files.swap(0, pos);
}
// owned: line source, line index, annotations
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
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<crate::snippet::Annotation>);
let annotated_files: Vec<Owned> = annotated_files
.into_iter()
.flat_map(|annotated_file| {
Expand All @@ -169,7 +167,15 @@ impl AnnotateSnippetEmitterWriter {
.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::<Vec<Owned>>()
})
Expand All @@ -192,11 +198,11 @@ impl AnnotateSnippetEmitterWriter {
},
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
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/annotate-snippet/auxiliary/other_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub struct WithPrivateMethod;

impl WithPrivateMethod {
/// Private to get an error involving two files
fn private_method(&self) {}
}
8 changes: 8 additions & 0 deletions tests/ui/annotate-snippet/multiple-files.rs
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 11 additions & 0 deletions tests/ui/annotate-snippet/multiple-files.stderr
Original file line number Diff line number Diff line change
@@ -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
|