Skip to main content

rustc_errors/
annotate_snippet_emitter_writer.rs

1//! Emit diagnostics using the `annotate-snippets` library
2//!
3//! This is the equivalent of `./emitter.rs` but making use of the
4//! [`annotate-snippets`][annotate_snippets] library instead of building the output ourselves.
5//!
6//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/
7
8use std::borrow::Cow;
9use std::fmt::Debug;
10use std::io;
11use std::io::Write;
12use std::sync::Arc;
13
14use annotate_snippets::renderer::DEFAULT_TERM_WIDTH;
15use annotate_snippets::{AnnotationKind, Group, Origin, Padding, Patch, Renderer, Snippet};
16use anstream::ColorChoice;
17use derive_setters::Setters;
18use rustc_data_structures::sync::IntoDynSyncSend;
19use rustc_error_messages::{DiagArgMap, SpanLabel};
20use rustc_lint_defs::pluralize;
21use rustc_span::source_map::SourceMap;
22use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
23use tracing::debug;
24
25use crate::emitter::{
26    ConfusionType, Destination, MAX_SUGGESTIONS, OutputTheme, detect_confusion_type, is_different,
27    normalize_whitespace, should_show_source_code,
28};
29use crate::formatting::{format_diag_message, format_diag_messages};
30use crate::{
31    CodeSuggestion, DiagInner, DiagMessage, Emitter, ErrCode, Level, MultiSpan, Style, Subdiag,
32    SuggestionStyle, TerminalUrl,
33};
34
35/// Generates diagnostics using annotate-snippet
36#[derive(impl AnnotateSnippetEmitter {
    #[must_use]
    pub fn sm(mut self, value: Option<Arc<SourceMap>>) -> Self {
        self.sm = value;
        self
    }
    #[must_use]
    pub fn short_message(mut self, value: bool) -> Self {
        self.short_message = value;
        self
    }
    #[must_use]
    pub fn ui_testing(mut self, value: bool) -> Self {
        self.ui_testing = value;
        self
    }
    #[must_use]
    pub fn ignored_directories_in_source_blocks(mut self, value: Vec<String>)
        -> Self {
        self.ignored_directories_in_source_blocks = value;
        self
    }
    #[must_use]
    pub fn diagnostic_width(mut self, value: Option<usize>) -> Self {
        self.diagnostic_width = value;
        self
    }
    #[must_use]
    pub fn macro_backtrace(mut self, value: bool) -> Self {
        self.macro_backtrace = value;
        self
    }
    #[must_use]
    pub fn track_diagnostics(mut self, value: bool) -> Self {
        self.track_diagnostics = value;
        self
    }
    #[must_use]
    pub fn terminal_url(mut self, value: TerminalUrl) -> Self {
        self.terminal_url = value;
        self
    }
    #[must_use]
    pub fn theme(mut self, value: OutputTheme) -> Self {
        self.theme = value;
        self
    }
}Setters)]
37pub struct AnnotateSnippetEmitter {
38    #[setters(skip)]
39    dst: IntoDynSyncSend<Destination>,
40    sm: Option<Arc<SourceMap>>,
41    short_message: bool,
42    ui_testing: bool,
43    ignored_directories_in_source_blocks: Vec<String>,
44    diagnostic_width: Option<usize>,
45
46    macro_backtrace: bool,
47    track_diagnostics: bool,
48    terminal_url: TerminalUrl,
49    theme: OutputTheme,
50}
51
52impl Debug for AnnotateSnippetEmitter {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.debug_struct("AnnotateSnippetEmitter")
55            .field("short_message", &self.short_message)
56            .field("ui_testing", &self.ui_testing)
57            .field(
58                "ignored_directories_in_source_blocks",
59                &self.ignored_directories_in_source_blocks,
60            )
61            .field("diagnostic_width", &self.diagnostic_width)
62            .field("macro_backtrace", &self.macro_backtrace)
63            .field("track_diagnostics", &self.track_diagnostics)
64            .field("terminal_url", &self.terminal_url)
65            .field("theme", &self.theme)
66            .finish()
67    }
68}
69
70impl Emitter for AnnotateSnippetEmitter {
71    /// The entry point for the diagnostics generation
72    fn emit_diagnostic(&mut self, mut diag: DiagInner) {
73        if self.track_diagnostics && diag.span.has_primary_spans() && !diag.span.is_dummy() {
74            diag.children.insert(0, diag.emitted_at_sub_diag());
75        }
76
77        let mut suggestions = diag.suggestions.unwrap_tag();
78        self.primary_span_formatted(&mut diag.span, &mut suggestions, &diag.args);
79
80        self.fix_multispans_in_extern_macros_and_render_macro_backtrace(
81            &mut diag.span,
82            &mut diag.children,
83            &diag.level,
84            self.macro_backtrace,
85        );
86
87        self.emit_messages_default(
88            &diag.level,
89            &diag.messages,
90            &diag.args,
91            &diag.code,
92            &diag.span,
93            &diag.children,
94            suggestions,
95        );
96    }
97
98    fn source_map(&self) -> Option<&SourceMap> {
99        self.sm.as_deref()
100    }
101
102    fn should_show_explain(&self) -> bool {
103        !self.short_message
104    }
105
106    fn supports_color(&self) -> bool {
107        false
108    }
109}
110
111fn annotation_level_for_level(level: Level) -> annotate_snippets::level::Level<'static> {
112    match level {
113        Level::Bug | Level::DelayedBug => {
114            annotate_snippets::Level::ERROR.with_name("error: internal compiler error")
115        }
116        Level::Fatal | Level::Error => annotate_snippets::level::ERROR,
117        Level::ForceWarning | Level::Warning => annotate_snippets::Level::WARNING,
118        Level::Note | Level::OnceNote => annotate_snippets::Level::NOTE,
119        Level::Help | Level::OnceHelp => annotate_snippets::Level::HELP,
120        Level::FailureNote => annotate_snippets::Level::NOTE.no_name(),
121        Level::Allow => { ::core::panicking::panic_fmt(format_args!("Should not call with Allow")); }panic!("Should not call with Allow"),
122        Level::Expect => { ::core::panicking::panic_fmt(format_args!("Should not call with Expect")); }panic!("Should not call with Expect"),
123    }
124}
125
126impl AnnotateSnippetEmitter {
127    pub fn new(dst: Destination) -> Self {
128        Self {
129            dst: IntoDynSyncSend(dst),
130            sm: None,
131            short_message: false,
132            ui_testing: false,
133            ignored_directories_in_source_blocks: Vec::new(),
134            diagnostic_width: None,
135            macro_backtrace: false,
136            track_diagnostics: false,
137            terminal_url: TerminalUrl::No,
138            theme: OutputTheme::Ascii,
139        }
140    }
141
142    fn emit_messages_default(
143        &mut self,
144        level: &Level,
145        msgs: &[(DiagMessage, Style)],
146        args: &DiagArgMap,
147        code: &Option<ErrCode>,
148        msp: &MultiSpan,
149        children: &[Subdiag],
150        suggestions: Vec<CodeSuggestion>,
151    ) {
152        let renderer = self.renderer();
153        let annotation_level = annotation_level_for_level(*level);
154
155        // If at least one portion of the message is styled, we need to
156        // "pre-style" the message
157        let mut title = if msgs.iter().any(|(_, style)| style != &crate::Style::NoStyle) {
158            annotation_level
159                .clone()
160                .secondary_title(Cow::Owned(self.pre_style_msgs(msgs, *level, args)))
161        } else {
162            annotation_level.clone().primary_title(format_diag_messages(msgs, args))
163        };
164
165        if let Some(c) = code {
166            title = title.id(c.to_string());
167            if let TerminalUrl::Yes = self.terminal_url {
168                title = title.id_url(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("https://doc.rust-lang.org/error_codes/{0}.html",
                c))
    })format!("https://doc.rust-lang.org/error_codes/{c}.html"));
169            }
170        }
171
172        let mut report = ::alloc::vec::Vec::new()vec![];
173        let mut group = Group::with_title(title);
174
175        // If we don't have span information, emit and exit
176        let Some(sm) = self.sm.as_ref() else {
177            group = group.elements(children.iter().map(|c| {
178                let msg = format_diag_messages(&c.messages, args).to_string();
179                let level = annotation_level_for_level(c.level);
180                level.message(msg)
181            }));
182
183            report.push(group);
184            if let Err(e) = emit_to_destination(
185                renderer.render(&report),
186                level,
187                &mut self.dst,
188                self.short_message,
189            ) {
190                {
    ::core::panicking::panic_fmt(format_args!("failed to emit error: {0}",
            e));
};panic!("failed to emit error: {e}");
191            }
192            return;
193        };
194
195        let mut file_ann = collect_annotations(args, msp, sm);
196
197        // Make sure our primary file comes first
198        let primary_span = msp.primary_span().unwrap_or_default();
199        if !primary_span.is_dummy() {
200            let primary_lo = sm.lookup_char_pos(primary_span.lo());
201            if let Ok(pos) = file_ann.binary_search_by(|(f, _)| f.name.cmp(&primary_lo.file.name)) {
202                file_ann.swap(0, pos);
203            }
204
205            let file_ann_len = file_ann.len();
206            for (file_idx, (file, annotations)) in file_ann.into_iter().enumerate() {
207                if should_show_source_code(&self.ignored_directories_in_source_blocks, sm, &file) {
208                    if let Some(snippet) = self.annotated_snippet(annotations, &file.name, sm) {
209                        group = group.element(snippet);
210                    }
211                // we can't annotate anything if the source is unavailable.
212                } else if !self.short_message {
213                    // We'll just print unannotated messages
214                    group = self.unannotated_messages(
215                        annotations,
216                        &file.name,
217                        sm,
218                        file_idx,
219                        &mut report,
220                        group,
221                        &annotation_level,
222                    );
223                    // If this is the last annotation for a file, and
224                    // this is the last file, and the first child is a
225                    // "secondary" message, we need to add padding
226                    // ╭▸ /rustc/FAKE_PREFIX/library/core/src/clone.rs:236:13
227                    // │
228                    // ├ note: the late bound lifetime parameter
229                    // │ (<- It adds *this*)
230                    // ╰ warning: this was previously accepted
231                    if let Some(c) = children.first()
232                        && (!c.span.has_primary_spans() && !c.span.has_span_labels())
233                        && file_idx == file_ann_len - 1
234                    {
235                        group = group.element(Padding);
236                    }
237                }
238            }
239        }
240
241        for c in children {
242            let level = annotation_level_for_level(c.level);
243
244            // If at least one portion of the message is styled, we need to
245            // "pre-style" the message
246            let msg = if c.messages.iter().any(|(_, style)| style != &crate::Style::NoStyle) {
247                Cow::Owned(self.pre_style_msgs(&c.messages, c.level, args))
248            } else {
249                format_diag_messages(&c.messages, args)
250            };
251
252            // This is a secondary message with no span info
253            if !c.span.has_primary_spans() && !c.span.has_span_labels() {
254                group = group.element(level.clone().message(msg));
255                continue;
256            }
257
258            report.push(std::mem::replace(
259                &mut group,
260                Group::with_title(level.clone().secondary_title(msg)),
261            ));
262
263            let mut file_ann = collect_annotations(args, &c.span, sm);
264            let primary_span = c.span.primary_span().unwrap_or_default();
265            if !primary_span.is_dummy() {
266                let primary_lo = sm.lookup_char_pos(primary_span.lo());
267                if let Ok(pos) =
268                    file_ann.binary_search_by(|(f, _)| f.name.cmp(&primary_lo.file.name))
269                {
270                    file_ann.swap(0, pos);
271                }
272            }
273
274            for (file_idx, (file, annotations)) in file_ann.into_iter().enumerate() {
275                if should_show_source_code(&self.ignored_directories_in_source_blocks, sm, &file) {
276                    if let Some(snippet) = self.annotated_snippet(annotations, &file.name, sm) {
277                        group = group.element(snippet);
278                    }
279                // we can't annotate anything if the source is unavailable.
280                } else if !self.short_message {
281                    // We'll just print unannotated messages
282                    group = self.unannotated_messages(
283                        annotations,
284                        &file.name,
285                        sm,
286                        file_idx,
287                        &mut report,
288                        group,
289                        &level,
290                    );
291                }
292            }
293        }
294
295        for suggestion in suggestions {
296            match suggestion.style {
297                SuggestionStyle::CompletelyHidden => {
298                    // do not display this suggestion, it is meant only for tools
299                }
300                SuggestionStyle::HideCodeAlways => {
301                    let msg = format_diag_messages(
302                        &[(suggestion.msg.to_owned(), Style::HeaderMsg)],
303                        args,
304                    );
305                    group = group.element(annotate_snippets::Level::HELP.message(msg));
306                }
307                SuggestionStyle::HideCodeInline
308                | SuggestionStyle::ShowCode
309                | SuggestionStyle::ShowAlways => {
310                    let substitutions = suggestion
311                        .substitutions
312                        .into_iter()
313                        .filter(|subst| {
314                            // Suggestions coming from macros can have malformed spans. This is a heavy
315                            // handed approach to avoid ICEs by ignoring the suggestion outright.
316                            let invalid =
317                                subst.parts.iter().any(|item| sm.is_valid_span(item.span).is_err());
318                            if invalid {
319                                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs:319",
                        "rustc_errors::annotate_snippet_emitter_writer",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs"),
                        ::tracing_core::__macro_support::Option::Some(319u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_errors::annotate_snippet_emitter_writer"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("suggestion contains an invalid span: {0:?}",
                                                    subst) as &dyn Value))])
            });
    } else { ; }
};debug!("suggestion contains an invalid span: {:?}", subst);
320                            }
321                            !invalid
322                        })
323                        .filter_map(|mut subst| {
324                            // Assumption: all spans are in the same file, and all spans
325                            // are disjoint. Sort in ascending order.
326                            subst.parts.sort_by_key(|part| part.span.lo());
327                            // Verify the assumption that all spans are disjoint
328                            if true {
    match (&subst.parts.array_windows().find(|[a, b]|
                        a.span.overlaps(b.span)), &None) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("all spans must be disjoint")));
            }
        }
    };
};debug_assert_eq!(
329                                subst.parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
330                                None,
331                                "all spans must be disjoint",
332                            );
333
334                            let lo = subst.parts.iter().map(|part| part.span.lo()).min()?;
335                            let lo_file = sm.lookup_source_file(lo);
336                            let hi = subst.parts.iter().map(|part| part.span.hi()).max()?;
337                            let hi_file = sm.lookup_source_file(hi);
338
339                            // The different spans might belong to different contexts, if so ignore suggestion.
340                            if lo_file.stable_id != hi_file.stable_id {
341                                return None;
342                            }
343
344                            // We can't splice anything if the source is unavailable.
345                            if !sm.ensure_source_file_source_present(&lo_file) {
346                                return None;
347                            }
348
349                            // Account for cases where we are suggesting the same code that's already
350                            // there. This shouldn't happen often, but in some cases for multipart
351                            // suggestions it's much easier to handle it here than in the origin.
352                            subst.parts.retain(|p| is_different(sm, &p.snippet, p.span));
353
354                            if subst.parts.is_empty() { None } else { Some(subst) }
355                        })
356                        .collect::<Vec<_>>();
357
358                    if substitutions.is_empty() {
359                        continue;
360                    }
361                    let mut msg = format_diag_message(&suggestion.msg, args).to_string();
362
363                    let lo = substitutions
364                        .iter()
365                        .find_map(|sub| sub.parts.first().map(|p| p.span.lo()))
366                        .unwrap();
367                    let file = sm.lookup_source_file(lo);
368
369                    let filename =
370                        sm.filename_for_diagnostics(&file.name).to_string_lossy().to_string();
371
372                    let other_suggestions = substitutions.len().saturating_sub(MAX_SUGGESTIONS);
373
374                    let subs = substitutions
375                        .into_iter()
376                        .take(MAX_SUGGESTIONS)
377                        .filter_map(|sub| {
378                            let mut confusion_type = ConfusionType::None;
379                            for part in &sub.parts {
380                                let part_confusion =
381                                    detect_confusion_type(sm, &part.snippet, part.span);
382                                confusion_type = confusion_type.combine(part_confusion);
383                            }
384
385                            if !#[allow(non_exhaustive_omitted_patterns)] match confusion_type {
    ConfusionType::None => true,
    _ => false,
}matches!(confusion_type, ConfusionType::None) {
386                                msg.push_str(confusion_type.label_text());
387                            }
388
389                            let mut parts = sub
390                                .parts
391                                .into_iter()
392                                .filter_map(|p| {
393                                    if is_different(sm, &p.snippet, p.span) {
394                                        Some((p.span, p.snippet))
395                                    } else {
396                                        None
397                                    }
398                                })
399                                .collect::<Vec<_>>();
400
401                            if parts.is_empty() {
402                                None
403                            } else {
404                                let spans = parts.iter().map(|(span, _)| *span).collect::<Vec<_>>();
405                                // The suggestion adds an entire line of code, ending on a newline, so we'll also
406                                // print the *following* line, to provide context of what we're advising people to
407                                // do. Otherwise you would only see contextless code that can be confused for
408                                // already existing code, despite the colors and UI elements.
409                                // We special case `#[derive(_)]\n` and other attribute suggestions, because those
410                                // are the ones where context is most useful.
411                                let fold = if let [(p, snippet)] = &mut parts[..]
412                                    && snippet.trim().starts_with("#[")
413                                    // This allows for spaces to come between the attribute and the newline
414                                    && snippet.trim().ends_with("]")
415                                    && snippet.ends_with('\n')
416                                    && p.hi() == p.lo()
417                                    && let Ok(b) = sm.span_to_prev_source(*p)
418                                    && let b = b.rsplit_once('\n').unwrap_or_else(|| ("", &b)).1
419                                    && b.trim().is_empty()
420                                {
421                                    // FIXME: This is a hack:
422                                    // The span for attribute suggestions often times points to the
423                                    // beginning of an item, disregarding leading whitespace. This
424                                    // causes the attribute to be properly indented, but leaves original
425                                    // item without indentation when rendered.
426                                    // This fixes that problem by adjusting the span to point to the start
427                                    // of the whitespace, and adds the whitespace to the replacement.
428                                    //
429                                    // Source: "    extern "custom" fn negate(a: i64) -> i64 {\n"
430                                    // Span: 4..4
431                                    // Replacement: "#[unsafe(naked)]\n"
432                                    //
433                                    // Before:
434                                    // help: convert this to an `#[unsafe(naked)]` function
435                                    //    |
436                                    // LL +     #[unsafe(naked)]
437                                    // LL | extern "custom" fn negate(a: i64) -> i64 {
438                                    //    |
439                                    //
440                                    // After
441                                    // help: convert this to an `#[unsafe(naked)]` function
442                                    //    |
443                                    // LL +     #[unsafe(naked)]
444                                    // LL |     extern "custom" fn negate(a: i64) -> i64 {
445                                    //    |
446                                    if !b.is_empty() && !snippet.ends_with(b) {
447                                        snippet.insert_str(0, b);
448                                        let offset = BytePos(b.len() as u32);
449                                        *p = p.with_lo(p.lo() - offset).shrink_to_lo();
450                                    }
451                                    false
452                                } else {
453                                    true
454                                };
455
456                                if let Some((bounding_span, source, line_offset)) =
457                                    shrink_file(spans.as_slice(), &file.name, sm)
458                                {
459                                    let adj_lo = bounding_span.lo().to_usize();
460                                    Some(
461                                        Snippet::source(source)
462                                            .line_start(line_offset)
463                                            .path(filename.clone())
464                                            .fold(fold)
465                                            .patches(parts.into_iter().map(
466                                                |(span, replacement)| {
467                                                    let lo =
468                                                        span.lo().to_usize().saturating_sub(adj_lo);
469                                                    let hi =
470                                                        span.hi().to_usize().saturating_sub(adj_lo);
471
472                                                    Patch::new(lo..hi, replacement)
473                                                },
474                                            )),
475                                    )
476                                } else {
477                                    None
478                                }
479                            }
480                        })
481                        .collect::<Vec<_>>();
482                    if !subs.is_empty() {
483                        report.push(std::mem::replace(
484                            &mut group,
485                            Group::with_title(annotate_snippets::Level::HELP.secondary_title(msg)),
486                        ));
487
488                        group = group.elements(subs);
489                        if other_suggestions > 0 {
490                            group = group.element(
491                                annotate_snippets::Level::NOTE.no_name().message(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("and {0} other candidate{1}",
                other_suggestions,
                if other_suggestions == 1 { "" } else { "s" }))
    })format!(
492                                    "and {} other candidate{}",
493                                    other_suggestions,
494                                    pluralize!(other_suggestions)
495                                )),
496                            );
497                        }
498                    }
499                }
500            }
501        }
502
503        if !group.is_empty() {
504            report.push(group);
505        }
506        if let Err(e) =
507            emit_to_destination(renderer.render(&report), level, &mut self.dst, self.short_message)
508        {
509            {
    ::core::panicking::panic_fmt(format_args!("failed to emit error: {0}",
            e));
};panic!("failed to emit error: {e}");
510        }
511    }
512
513    fn renderer(&self) -> Renderer {
514        let width = if let Some(width) = self.diagnostic_width {
515            width
516        } else if self.ui_testing || falsecfg!(miri) {
517            DEFAULT_TERM_WIDTH
518        } else {
519            termize::dimensions().map(|(w, _)| w).unwrap_or(DEFAULT_TERM_WIDTH)
520        };
521        let decor_style = match self.theme {
522            OutputTheme::Ascii => annotate_snippets::renderer::DecorStyle::Ascii,
523            OutputTheme::Unicode => annotate_snippets::renderer::DecorStyle::Unicode,
524        };
525
526        match self.dst.current_choice() {
527            ColorChoice::AlwaysAnsi | ColorChoice::Always | ColorChoice::Auto => Renderer::styled(),
528            ColorChoice::Never => Renderer::plain(),
529        }
530        .term_width(width)
531        .anonymized_line_numbers(self.ui_testing)
532        .decor_style(decor_style)
533        .short_message(self.short_message)
534    }
535
536    fn pre_style_msgs(
537        &self,
538        msgs: &[(DiagMessage, Style)],
539        level: Level,
540        args: &DiagArgMap,
541    ) -> String {
542        msgs.iter()
543            .filter_map(|(m, style)| {
544                let text = format_diag_message(m, args);
545                let style = style.anstyle(level);
546                if text.is_empty() { None } else { Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{0:#}", style, text))
    })format!("{style}{text}{style:#}")) }
547            })
548            .collect()
549    }
550
551    fn annotated_snippet<'a>(
552        &self,
553        annotations: Vec<Annotation>,
554        file_name: &FileName,
555        sm: &Arc<SourceMap>,
556    ) -> Option<Snippet<'a, annotate_snippets::Annotation<'a>>> {
557        let spans = annotations.iter().map(|a| a.span).collect::<Vec<_>>();
558        if let Some((bounding_span, source, offset_line)) = shrink_file(&spans, file_name, sm) {
559            let adj_lo = bounding_span.lo().to_usize();
560            let filename = sm.filename_for_diagnostics(file_name).to_string_lossy().to_string();
561            Some(Snippet::source(source).line_start(offset_line).path(filename).annotations(
562                annotations.into_iter().map(move |a| {
563                    let lo = a.span.lo().to_usize().saturating_sub(adj_lo);
564                    let hi = a.span.hi().to_usize().saturating_sub(adj_lo);
565                    let ann = a.kind.span(lo..hi);
566                    if let Some(label) = a.label { ann.label(label) } else { ann }
567                }),
568            ))
569        } else {
570            None
571        }
572    }
573
574    fn unannotated_messages<'a>(
575        &self,
576        annotations: Vec<Annotation>,
577        file_name: &FileName,
578        sm: &Arc<SourceMap>,
579        file_idx: usize,
580        report: &mut Vec<Group<'a>>,
581        mut group: Group<'a>,
582        level: &annotate_snippets::level::Level<'static>,
583    ) -> Group<'a> {
584        let filename = sm.filename_for_diagnostics(file_name).to_string_lossy().to_string();
585        let mut line_tracker = ::alloc::vec::Vec::new()vec![];
586        for (i, a) in annotations.into_iter().enumerate() {
587            let lo = sm.lookup_char_pos(a.span.lo());
588            let hi = sm.lookup_char_pos(a.span.hi());
589            if i == 0 || (a.label.is_some()) {
590                // Render each new file after the first in its own Group
591                //    ╭▸ $DIR/deriving-meta-unknown-trait.rs:1:10
592                //    │
593                // LL │ #[derive(Eqr)]
594                //    │          ━━━
595                //    ╰╴ (<- It makes it so *this* will get printed)
596                //    ╭▸ $SRC_DIR/core/src/option.rs:594:0
597                //    ⸬  $SRC_DIR/core/src/option.rs:602:4
598                //    │
599                //    ╰ note: not covered
600                if i == 0 && file_idx != 0 {
601                    report.push(std::mem::replace(&mut group, Group::with_level(level.clone())));
602                }
603
604                if !line_tracker.contains(&lo.line) && (i == 0 || hi.line <= lo.line) {
605                    line_tracker.push(lo.line);
606                    // ╭▸ $SRC_DIR/core/src/option.rs:594:0 (<- It adds *this*)
607                    // ⸬  $SRC_DIR/core/src/option.rs:602:4
608                    // │
609                    // ╰ note: not covered
610                    group = group.element(
611                        Origin::path(filename.clone())
612                            .line(sm.doctest_offset_line(file_name, lo.line))
613                            .char_column(lo.col_display),
614                    );
615                }
616
617                if hi.line > lo.line
618                    && a.label.as_ref().is_some_and(|l| !l.is_empty())
619                    && !line_tracker.contains(&hi.line)
620                {
621                    line_tracker.push(hi.line);
622                    // ╭▸ $SRC_DIR/core/src/option.rs:594:0
623                    // ⸬  $SRC_DIR/core/src/option.rs:602:4 (<- It adds *this*)
624                    // │
625                    // ╰ note: not covered
626                    group = group.element(
627                        Origin::path(filename.clone())
628                            .line(sm.doctest_offset_line(file_name, hi.line))
629                            .char_column(hi.col_display),
630                    );
631                }
632
633                if let Some(label) = a.label
634                    && !label.is_empty()
635                {
636                    // ╭▸ $SRC_DIR/core/src/option.rs:594:0
637                    // ⸬  $SRC_DIR/core/src/option.rs:602:4
638                    // │ (<- It adds *this*)
639                    // ╰ note: not covered (<- and *this*)
640                    group = group
641                        .element(Padding)
642                        .element(annotate_snippets::Level::NOTE.message(label));
643                }
644            }
645        }
646        group
647    }
648}
649
650fn emit_to_destination(
651    rendered: String,
652    lvl: &Level,
653    dst: &mut Destination,
654    short_message: bool,
655) -> io::Result<()> {
656    use crate::lock;
657    let _buffer_lock = lock::acquire_global_lock("rustc_errors");
658    dst.write_fmt(format_args!("{0}\n", rendered))writeln!(dst, "{rendered}")?;
659    if !short_message && !lvl.is_failure_note() {
660        dst.write_fmt(format_args!("\n"))writeln!(dst)?;
661    }
662    dst.flush()?;
663    Ok(())
664}
665
666#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Annotation {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "Annotation",
            "kind", &self.kind, "span", &self.span, "label", &&self.label)
    }
}Debug)]
667struct Annotation {
668    kind: AnnotationKind,
669    span: Span,
670    label: Option<String>,
671}
672
673fn collect_annotations(
674    args: &DiagArgMap,
675    msp: &MultiSpan,
676    sm: &Arc<SourceMap>,
677) -> Vec<(Arc<SourceFile>, Vec<Annotation>)> {
678    let mut output: Vec<(Arc<SourceFile>, Vec<Annotation>)> = ::alloc::vec::Vec::new()vec![];
679
680    for SpanLabel { span, is_primary, label } in msp.span_labels() {
681        // If we don't have a useful span, pick the primary span if that exists.
682        // Worst case we'll just print an error at the top of the main file.
683        let span = match (span.is_dummy(), msp.primary_span()) {
684            (_, None) | (false, _) => span,
685            (true, Some(span)) => span,
686        };
687        let file = sm.lookup_source_file(span.lo());
688
689        let kind = if is_primary { AnnotationKind::Primary } else { AnnotationKind::Context };
690
691        let label = label.as_ref().map(|m| normalize_whitespace(&format_diag_message(m, args)));
692
693        let ann = Annotation { kind, span, label };
694        if sm.is_valid_span(ann.span).is_ok() {
695            // Look through each of our files for the one we're adding to. We
696            // use each files `stable_id` to avoid issues with file name
697            // collisions when multiple versions of the same crate are present
698            // in the dependency graph
699            if let Some((_, annotations)) =
700                output.iter_mut().find(|(f, _)| f.stable_id == file.stable_id)
701            {
702                annotations.push(ann);
703            } else {
704                output.push((file, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ann]))vec![ann]));
705            }
706        }
707    }
708
709    // Sort annotations within each file by line number
710    for (_, ann) in output.iter_mut() {
711        ann.sort_by_key(|a| {
712            let lo = sm.lookup_char_pos(a.span.lo());
713            lo.line
714        });
715    }
716    output
717}
718
719fn shrink_file(
720    spans: &[Span],
721    file_name: &FileName,
722    sm: &Arc<SourceMap>,
723) -> Option<(Span, String, usize)> {
724    let lo_byte = spans.iter().map(|s| s.lo()).min()?;
725    let lo_loc = sm.lookup_char_pos(lo_byte);
726
727    let hi_byte = spans.iter().map(|s| s.hi()).max()?;
728    let hi_loc = sm.lookup_char_pos(hi_byte);
729
730    if lo_loc.file.stable_id != hi_loc.file.stable_id {
731        // this may happen when spans cross file boundaries due to macro expansion.
732        return None;
733    }
734
735    let lo = lo_loc.file.line_bounds(lo_loc.line.saturating_sub(1)).start;
736    let hi = hi_loc.file.line_bounds(hi_loc.line.saturating_sub(1)).end;
737
738    let bounding_span = Span::with_root_ctxt(lo, hi);
739    let source = sm.span_to_snippet(bounding_span).ok()?;
740    let offset_line = sm.doctest_offset_line(file_name, lo_loc.line);
741
742    Some((bounding_span, source, offset_line))
743}