diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-12-12 15:25:04 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-12-13 10:06:08 +0000 |
| commit | 59554a2b54c3510b062825801064541a4caa662d (patch) | |
| tree | 761ab5e9a5860b7d87dd9022326a9c5dc01f4094 /compiler/rustc_errors/src | |
| parent | 2e2a4797a26b0effe15815b403be4ba752cc7a0b (diff) | |
| download | rust-59554a2b54c3510b062825801064541a4caa662d.tar.gz rust-59554a2b54c3510b062825801064541a4caa662d.zip | |
Avoid rendering empty annotations
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 268f17f86fe..c62e358e804 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1408,51 +1408,58 @@ impl EmitterWriter { if !sm.ensure_source_file_source_present(annotated_file.file.clone()) { if !self.short_message { // We'll just print an unannotated message. - for (annotation_id, line) in annotated_file.lines.into_iter().enumerate() { + for (annotation_id, line) in annotated_file.lines.iter().enumerate() { let mut annotations = line.annotations.clone(); annotations.sort_by_key(|a| Reverse(a.start_col)); let mut line_idx = buffer.num_lines(); - buffer.append( - line_idx, - &format!( - "{}:{}:{}", - sm.filename_for_diagnostics(&annotated_file.file.name), - sm.doctest_offset_line(&annotated_file.file.name, line.line_index), - annotations[0].start_col + 1, - ), - Style::LineAndColumn, - ); - if annotation_id == 0 { - buffer.prepend(line_idx, "--> ", Style::LineNumber); + + let labels: Vec<_> = annotations + .iter() + .filter_map(|a| Some((a.label.as_ref()?, a.is_primary))) + .filter(|(l, _)| !l.is_empty()) + .collect(); + + if annotation_id == 0 || !labels.is_empty() { + buffer.append( + line_idx, + &format!( + "{}:{}:{}", + sm.filename_for_diagnostics(&annotated_file.file.name), + sm.doctest_offset_line( + &annotated_file.file.name, + line.line_index + ), + annotations[0].start_col + 1, + ), + Style::LineAndColumn, + ); + if annotation_id == 0 { + buffer.prepend(line_idx, "--> ", Style::LineNumber); + } else { + buffer.prepend(line_idx, "::: ", Style::LineNumber); + } for _ in 0..max_line_num_len { buffer.prepend(line_idx, " ", Style::NoStyle); } line_idx += 1; - }; - for (i, annotation) in annotations.into_iter().enumerate() { - if let Some(label) = &annotation.label { - if !label.is_empty() { - let style = if annotation.is_primary { - Style::LabelPrimary - } else { - Style::LabelSecondary - }; - if annotation_id == 0 { - buffer.prepend(line_idx, " |", Style::LineNumber); - for _ in 0..max_line_num_len { - buffer.prepend(line_idx, " ", Style::NoStyle); - } - line_idx += 1; - buffer.append(line_idx + i, " = note: ", style); - for _ in 0..max_line_num_len { - buffer.prepend(line_idx, " ", Style::NoStyle); - } - } else { - buffer.append(line_idx + i, ": ", style); - } - buffer.append(line_idx + i, label, style); - } + } + for (label, is_primary) in labels.into_iter() { + let style = if is_primary { + Style::LabelPrimary + } else { + Style::LabelSecondary + }; + buffer.prepend(line_idx, " |", Style::LineNumber); + for _ in 0..max_line_num_len { + buffer.prepend(line_idx, " ", Style::NoStyle); + } + line_idx += 1; + buffer.append(line_idx, " = note: ", style); + for _ in 0..max_line_num_len { + buffer.prepend(line_idx, " ", Style::NoStyle); } + buffer.append(line_idx, label, style); + line_idx += 1; } } } |
