2019-06-05 14:13:56 -05:00
|
|
|
//! Emit diagnostics using the `annotate-snippets` library
|
|
|
|
//!
|
|
|
|
//! This is the equivalent of `./emitter.rs` but making use of the
|
|
|
|
//! [`annotate-snippets`][annotate_snippets] library instead of building the output ourselves.
|
|
|
|
//!
|
|
|
|
//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/
|
2019-05-31 15:01:27 -05:00
|
|
|
|
|
|
|
use crate::emitter::FileWithAnnotatedLines;
|
|
|
|
use crate::snippet::Line;
|
2022-08-10 11:30:47 -05:00
|
|
|
use crate::translation::Translate;
|
2022-03-26 02:27:43 -05:00
|
|
|
use crate::{
|
2022-04-12 03:34:40 -05:00
|
|
|
CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Emitter, FluentBundle,
|
|
|
|
LazyFallbackBundle, Level, MultiSpan, Style, SubDiagnostic,
|
2022-03-26 02:27:43 -05:00
|
|
|
};
|
2020-05-08 15:48:26 -05:00
|
|
|
use annotate_snippets::display_list::{DisplayList, FormatOptions};
|
2019-05-31 15:01:27 -05:00
|
|
|
use annotate_snippets::snippet::*;
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-03-26 02:27:43 -05:00
|
|
|
use rustc_error_messages::FluentArgs;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::source_map::SourceMap;
|
2022-03-23 21:03:04 -05:00
|
|
|
use rustc_span::SourceFile;
|
2019-05-31 15:01:27 -05:00
|
|
|
|
2019-06-05 14:13:56 -05:00
|
|
|
/// Generates diagnostics using annotate-snippet
|
|
|
|
pub struct AnnotateSnippetEmitterWriter {
|
2019-11-15 07:32:31 -06:00
|
|
|
source_map: Option<Lrc<SourceMap>>,
|
2022-03-28 03:36:20 -05:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 03:34:40 -05:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2022-03-26 02:27:43 -05:00
|
|
|
|
2019-05-31 15:01:27 -05:00
|
|
|
/// If true, hides the longer explanation text
|
|
|
|
short_message: bool,
|
2019-07-25 14:03:53 -05:00
|
|
|
/// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs.
|
2019-05-31 15:01:27 -05:00
|
|
|
ui_testing: bool,
|
2019-09-07 08:57:11 -05:00
|
|
|
|
2019-12-15 09:12:30 -06:00
|
|
|
macro_backtrace: bool,
|
2019-05-31 15:01:27 -05:00
|
|
|
}
|
|
|
|
|
2022-08-10 11:30:47 -05:00
|
|
|
impl Translate for AnnotateSnippetEmitterWriter {
|
|
|
|
fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
|
|
|
|
self.fluent_bundle.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fallback_fluent_bundle(&self) -> &FluentBundle {
|
|
|
|
&**self.fallback_bundle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:13:56 -05:00
|
|
|
impl Emitter for AnnotateSnippetEmitterWriter {
|
2019-05-31 15:01:27 -05:00
|
|
|
/// The entry point for the diagnostics generation
|
2019-10-15 01:12:55 -05:00
|
|
|
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
|
2022-03-26 02:27:43 -05:00
|
|
|
let fluent_args = self.to_fluent_args(diag.args());
|
|
|
|
|
2019-10-15 01:12:55 -05:00
|
|
|
let mut children = diag.children.clone();
|
2022-03-26 02:27:43 -05:00
|
|
|
let (mut primary_span, suggestions) = self.primary_span_formatted(&diag, &fluent_args);
|
2019-05-31 15:01:27 -05:00
|
|
|
|
2019-12-15 09:47:51 -06:00
|
|
|
self.fix_multispans_in_extern_macros_and_render_macro_backtrace(
|
2019-09-05 14:31:12 -05:00
|
|
|
&self.source_map,
|
|
|
|
&mut primary_span,
|
|
|
|
&mut children,
|
2019-10-15 01:12:55 -05:00
|
|
|
&diag.level,
|
2019-12-15 09:12:30 -06:00
|
|
|
self.macro_backtrace,
|
2019-09-07 08:57:11 -05:00
|
|
|
);
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2019-10-15 01:12:55 -05:00
|
|
|
self.emit_messages_default(
|
|
|
|
&diag.level,
|
2022-03-26 02:27:43 -05:00
|
|
|
&diag.message,
|
|
|
|
&fluent_args,
|
2019-10-15 01:12:55 -05:00
|
|
|
&diag.code,
|
2019-05-31 15:01:27 -05:00
|
|
|
&primary_span,
|
|
|
|
&children,
|
|
|
|
&suggestions,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-15 07:32:31 -06:00
|
|
|
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
|
2019-10-13 23:48:39 -05:00
|
|
|
self.source_map.as_ref()
|
|
|
|
}
|
|
|
|
|
2019-05-31 15:01:27 -05:00
|
|
|
fn should_show_explain(&self) -> bool {
|
|
|
|
!self.short_message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:48:26 -05:00
|
|
|
/// Provides the source string for the given `line` of `file`
|
|
|
|
fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
|
|
|
|
file.get_line(line.line_index - 1).map(|a| a.to_string()).unwrap_or_default()
|
2019-05-31 15:01:27 -05:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:48:26 -05:00
|
|
|
/// Maps `Diagnostic::Level` to `snippet::AnnotationType`
|
|
|
|
fn annotation_type_for_level(level: Level) -> AnnotationType {
|
|
|
|
match level {
|
2022-01-23 17:11:37 -06:00
|
|
|
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error { .. } => {
|
|
|
|
AnnotationType::Error
|
|
|
|
}
|
2022-06-05 05:33:45 -05:00
|
|
|
Level::Warning(_) => AnnotationType::Warning,
|
2022-03-20 14:02:18 -05:00
|
|
|
Level::Note | Level::OnceNote => AnnotationType::Note,
|
2020-05-08 15:48:26 -05:00
|
|
|
Level::Help => AnnotationType::Help,
|
2022-01-25 21:39:14 -06:00
|
|
|
// FIXME(#59346): Not sure how to map this level
|
|
|
|
Level::FailureNote => AnnotationType::Error,
|
2020-08-13 14:41:52 -05:00
|
|
|
Level::Allow => panic!("Should not call with Allow"),
|
2021-08-06 16:18:16 -05:00
|
|
|
Level::Expect(_) => panic!("Should not call with Expect"),
|
2019-05-31 15:01:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:13:56 -05:00
|
|
|
impl AnnotateSnippetEmitterWriter {
|
2019-05-31 15:01:27 -05:00
|
|
|
pub fn new(
|
2019-11-15 07:32:31 -06:00
|
|
|
source_map: Option<Lrc<SourceMap>>,
|
2022-03-28 03:36:20 -05:00
|
|
|
fluent_bundle: Option<Lrc<FluentBundle>>,
|
2022-04-12 03:34:40 -05:00
|
|
|
fallback_bundle: LazyFallbackBundle,
|
2019-09-07 08:57:11 -05:00
|
|
|
short_message: bool,
|
2019-12-15 09:12:30 -06:00
|
|
|
macro_backtrace: bool,
|
2019-05-31 15:01:27 -05:00
|
|
|
) -> Self {
|
2022-03-28 03:36:20 -05:00
|
|
|
Self {
|
|
|
|
source_map,
|
|
|
|
fluent_bundle,
|
|
|
|
fallback_bundle,
|
|
|
|
short_message,
|
|
|
|
ui_testing: false,
|
|
|
|
macro_backtrace,
|
|
|
|
}
|
2019-05-31 15:01:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows to modify `Self` to enable or disable the `ui_testing` flag.
|
|
|
|
///
|
|
|
|
/// If this is set to true, line numbers will be normalized as `LL` in the output.
|
|
|
|
pub fn ui_testing(mut self, ui_testing: bool) -> Self {
|
|
|
|
self.ui_testing = ui_testing;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-06-03 00:38:19 -05:00
|
|
|
fn emit_messages_default(
|
|
|
|
&mut self,
|
|
|
|
level: &Level,
|
2022-03-26 02:27:43 -05:00
|
|
|
messages: &[(DiagnosticMessage, Style)],
|
|
|
|
args: &FluentArgs<'_>,
|
2019-06-03 00:38:19 -05:00
|
|
|
code: &Option<DiagnosticId>,
|
|
|
|
msp: &MultiSpan,
|
2020-05-08 15:48:26 -05:00
|
|
|
_children: &[SubDiagnostic],
|
|
|
|
_suggestions: &[CodeSuggestion],
|
2019-05-31 15:01:27 -05:00
|
|
|
) {
|
2022-03-26 02:27:43 -05:00
|
|
|
let message = self.translate_messages(messages, args);
|
2020-05-08 15:48:26 -05:00
|
|
|
if let Some(source_map) = &self.source_map {
|
|
|
|
// Make sure our primary file comes first
|
|
|
|
let primary_lo = if let Some(ref primary_span) = msp.primary_span().as_ref() {
|
2020-05-15 21:57:40 -05:00
|
|
|
if primary_span.is_dummy() {
|
|
|
|
// FIXME(#59346): Not sure when this is the case and what
|
|
|
|
// should be done if it happens
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
source_map.lookup_char_pos(primary_span.lo())
|
|
|
|
}
|
2020-05-08 15:48:26 -05:00
|
|
|
} else {
|
|
|
|
// FIXME(#59346): Not sure when this is the case and what
|
|
|
|
// should be done if it happens
|
|
|
|
return;
|
|
|
|
};
|
2022-03-26 02:27:43 -05:00
|
|
|
let mut annotated_files = FileWithAnnotatedLines::collect_annotations(self, args, msp);
|
2020-05-15 21:57:40 -05:00
|
|
|
if let Ok(pos) =
|
|
|
|
annotated_files.binary_search_by(|x| x.file.name.cmp(&primary_lo.file.name))
|
|
|
|
{
|
|
|
|
annotated_files.swap(0, pos);
|
|
|
|
}
|
2020-05-08 15:48:26 -05:00
|
|
|
// owned: line source, line index, annotations
|
|
|
|
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
|
2021-08-26 05:46:01 -05:00
|
|
|
let filename = source_map.filename_for_diagnostics(&primary_lo.file.name);
|
2021-04-19 17:27:02 -05:00
|
|
|
let origin = filename.to_string_lossy();
|
2020-05-08 15:48:26 -05:00
|
|
|
let annotated_files: Vec<Owned> = annotated_files
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|annotated_file| {
|
|
|
|
let file = annotated_file.file;
|
|
|
|
annotated_file
|
|
|
|
.lines
|
|
|
|
.into_iter()
|
|
|
|
.map(|line| {
|
|
|
|
(source_string(file.clone(), &line), line.line_index, line.annotations)
|
|
|
|
})
|
|
|
|
.collect::<Vec<Owned>>()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let snippet = Snippet {
|
|
|
|
title: Some(Annotation {
|
|
|
|
label: Some(&message),
|
|
|
|
id: code.as_ref().map(|c| match c {
|
2021-06-04 07:37:20 -05:00
|
|
|
DiagnosticId::Error(val) | DiagnosticId::Lint { name: val, .. } => {
|
|
|
|
val.as_str()
|
|
|
|
}
|
2020-05-08 15:48:26 -05:00
|
|
|
}),
|
|
|
|
annotation_type: annotation_type_for_level(*level),
|
|
|
|
}),
|
|
|
|
footer: vec![],
|
2022-07-31 08:44:06 -05:00
|
|
|
opt: FormatOptions {
|
|
|
|
color: true,
|
|
|
|
anonymized_line_numbers: self.ui_testing,
|
|
|
|
margin: None,
|
|
|
|
},
|
2020-05-08 15:48:26 -05:00
|
|
|
slices: annotated_files
|
|
|
|
.iter()
|
|
|
|
.map(|(source, line_index, annotations)| {
|
|
|
|
Slice {
|
|
|
|
source,
|
|
|
|
line_start: *line_index,
|
|
|
|
origin: Some(&origin),
|
|
|
|
// FIXME(#59346): Not really sure when `fold` should be true or false
|
|
|
|
fold: false,
|
|
|
|
annotations: annotations
|
2020-06-09 08:57:08 -05:00
|
|
|
.iter()
|
2020-05-08 15:48:26 -05:00
|
|
|
.map(|annotation| SourceAnnotation {
|
|
|
|
range: (annotation.start_col, annotation.end_col),
|
2020-06-09 08:57:08 -05:00
|
|
|
label: annotation.label.as_deref().unwrap_or_default(),
|
2020-05-08 15:48:26 -05:00
|
|
|
annotation_type: annotation_type_for_level(*level),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
};
|
2019-06-01 01:29:12 -05:00
|
|
|
// FIXME(#59346): Figure out if we can _always_ print to stderr or not.
|
|
|
|
// `emitter.rs` has the `Destination` enum that lists various possible output
|
|
|
|
// destinations.
|
2020-05-08 15:48:26 -05:00
|
|
|
eprintln!("{}", DisplayList::from(snippet))
|
|
|
|
}
|
|
|
|
// FIXME(#59346): Is it ok to return None if there's no source_map?
|
2019-05-31 15:01:27 -05:00
|
|
|
}
|
|
|
|
}
|