Skip to content

Commit 427f3d3

Browse files
authored
Rollup merge of #114018 - Enselic:multi-annotation, r=b-naber
Make `--error-format human-annotate-rs` handle multiple files Closes #64205 which is E-help-wanted
2 parents e3bf088 + 504acf8 commit 427f3d3

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ impl AnnotateSnippetEmitterWriter {
157157
{
158158
annotated_files.swap(0, pos);
159159
}
160-
// owned: line source, line index, annotations
161-
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
162-
let filename = source_map.filename_for_diagnostics(&primary_lo.file.name);
163-
let origin = filename.to_string_lossy();
160+
// owned: file name, line source, line index, annotations
161+
type Owned = (String, String, usize, Vec<crate::snippet::Annotation>);
164162
let annotated_files: Vec<Owned> = annotated_files
165163
.into_iter()
166164
.flat_map(|annotated_file| {
@@ -169,7 +167,15 @@ impl AnnotateSnippetEmitterWriter {
169167
.lines
170168
.into_iter()
171169
.map(|line| {
172-
(source_string(file.clone(), &line), line.line_index, line.annotations)
170+
// Ensure the source file is present before we try
171+
// to load a string from it.
172+
source_map.ensure_source_file_source_present(file.clone());
173+
(
174+
format!("{}", source_map.filename_for_diagnostics(&file.name)),
175+
source_string(file.clone(), &line),
176+
line.line_index,
177+
line.annotations,
178+
)
173179
})
174180
.collect::<Vec<Owned>>()
175181
})
@@ -192,11 +198,11 @@ impl AnnotateSnippetEmitterWriter {
192198
},
193199
slices: annotated_files
194200
.iter()
195-
.map(|(source, line_index, annotations)| {
201+
.map(|(file_name, source, line_index, annotations)| {
196202
Slice {
197203
source,
198204
line_start: *line_index,
199-
origin: Some(&origin),
205+
origin: Some(&file_name),
200206
// FIXME(#59346): Not really sure when `fold` should be true or false
201207
fold: false,
202208
annotations: annotations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub struct WithPrivateMethod;
2+
3+
impl WithPrivateMethod {
4+
/// Private to get an error involving two files
5+
fn private_method(&self) {}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:other_file.rs
2+
// compile-flags: --error-format human-annotate-rs -Z unstable-options
3+
4+
extern crate other_file;
5+
6+
fn main() {
7+
other_file::WithPrivateMethod.private_method();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0624]: method `private_method` is private
2+
--> $DIR/multiple-files.rs:7:35
3+
|
4+
LL | other_file::WithPrivateMethod.private_method();
5+
| ^^^^^^^^^^^^^^ private method
6+
|
7+
::: $DIR/auxiliary/other_file.rs:5:5
8+
|
9+
LL | fn private_method(&self) {}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^ private method defined here
11+
|

0 commit comments

Comments
 (0)