rustc_symbol_mangling/
errors.rs

1//! Errors emitted by symbol_mangling.
2
3use std::fmt;
4
5use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level};
6use rustc_span::Span;
7
8pub struct TestOutput {
9    pub span: Span,
10    pub kind: Kind,
11    pub content: String,
12}
13
14// This diagnostic doesn't need translation because (a) it doesn't contain any
15// natural language, and (b) it's only used in tests. So we construct it
16// manually and avoid the fluent machinery.
17impl<G: EmissionGuarantee> Diagnostic<'_, G> for TestOutput {
18    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
19        let TestOutput { span, kind, content } = self;
20
21        #[allow(rustc::untranslatable_diagnostic)]
22        Diag::new(dcx, level, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}({1})", kind, content))
    })format!("{kind}({content})")).with_span(span)
23    }
24}
25
26pub enum Kind {
27    SymbolName,
28    Demangling,
29    DemanglingAlt,
30    DefPath,
31}
32
33impl fmt::Display for Kind {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Kind::SymbolName => f.write_fmt(format_args!("symbol-name"))write!(f, "symbol-name"),
37            Kind::Demangling => f.write_fmt(format_args!("demangling"))write!(f, "demangling"),
38            Kind::DemanglingAlt => f.write_fmt(format_args!("demangling-alt"))write!(f, "demangling-alt"),
39            Kind::DefPath => f.write_fmt(format_args!("def-path"))write!(f, "def-path"),
40        }
41    }
42}