diagnostic.rs.html -- source
Description: Source to the Rust file `libproc_macro/diagnostic.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134

    // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    use Span;

    use rustc_errors as rustc;

    /// An enum representing a diagnostic level.
    #[unstable(feature = "proc_macro", issue = "38356")]
    #[derive(Copy, Clone, Debug)]
    pub enum Level {
        /// An error.
        Error,
        /// A warning.
        Warning,
        /// A note.
        Note,
        /// A help message.
        Help,
        #[doc(hidden)]
        __Nonexhaustive,
    }

    /// A structure representing a diagnostic message and associated children
    /// messages.
    #[unstable(feature = "proc_macro", issue = "38356")]
    #[derive(Clone, Debug)]
    pub struct Diagnostic {
        level: Level,
        message: String,
        span: Option<Span>,
        children: Vec<Diagnostic>
    }

    macro_rules! diagnostic_child_methods {
        ($spanned:ident, $regular:ident, $level:expr) => (
            /// Add a new child diagnostic message to `self` with the level
            /// identified by this methods name with the given `span` and `message`.
            #[unstable(feature = "proc_macro", issue = "38356")]
            pub fn $spanned<T: Into<String>>(mut self, span: Span, message: T) -> Diagnostic {
                self.children.push(Diagnostic::spanned(span, $level, message));
                self
            }

            /// Add a new child diagnostic message to `self` with the level
            /// identified by this method's name with the given `message`.
            #[unstable(feature = "proc_macro", issue = "38356")]
            pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
                self.children.push(Diagnostic::new($level, message));
                self
            }
        )
    }

    impl Diagnostic {
        /// Create a new diagnostic with the given `level` and `message`.
        #[unstable(feature = "proc_macro", issue = "38356")]
        pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
            Diagnostic {
                level: level,
                message: message.into(),
                span: None,
                children: vec![]
            }
        }

        /// Create a new diagnostic with the given `level` and `message` pointing to
        /// the given `span`.
        #[unstable(feature = "proc_macro", issue = "38356")]
        pub fn spanned<T: Into<String>>(span: Span, level: Level, message: T) -> Diagnostic {
            Diagnostic {
                level: level,
                message: message.into(),
                span: Some(span),
                children: vec![]
            }
        }

        diagnostic_child_methods!(span_error, error, Level::Error);
        diagnostic_child_methods!(span_warning, warning, Level::Warning);
        diagnostic_child_methods!(span_note, note, Level::Note);
        diagnostic_child_methods!(span_help, help, Level::Help);

        /// Returns the diagnostic `level` for `self`.
        #[unstable(feature = "proc_macro", issue = "38356")]
        pub fn level(&self) -> Level {
            self.level
        }

        /// Emit the diagnostic.
        #[unstable(feature = "proc_macro", issue = "38356")]
        pub fn emit(self) {
            ::__internal::with_sess(move |(sess, _)| {
                let handler = &sess.span_diagnostic;
                let level = __internal::level_to_internal_level(self.level);
                let mut diag = rustc::DiagnosticBuilder::new(handler, level, &*self.message);

                if let Some(span) = self.span {
                    diag.set_span(span.0);
                }

                for child in self.children {
                    let span = child.span.map(|s| s.0);
                    let level = __internal::level_to_internal_level(child.level);
                    diag.sub(level, &*child.message, span);
                }

                diag.emit();
            });
        }
    }

    #[unstable(feature = "proc_macro_internals", issue = "27812")]
    #[doc(hidden)]
    pub mod __internal {
        use super::{Level, rustc};

        pub fn level_to_internal_level(level: Level) -> rustc::Level {
            match level {
                Level::Error => rustc::Level::Error,
                Level::Warning => rustc::Level::Warning,
                Level::Note => rustc::Level::Note,
                Level::Help => rustc::Level::Help,
                Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive")
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
↑
  Move up in search results
↓
  Move down in search results
↹
  Switch tab
⏎
  Go to active search result
+ / -
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)