rune/diagnostics/
runtime_warning.rs

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
use core::fmt;

use crate::Hash;

/// Runtime Warning diagnostic emitted during the execution of the VM. Warning diagnostics indicates
/// an recoverable issues.
#[derive(Debug)]
pub struct RuntimeWarningDiagnostic {
    /// The instruction pointer of the vm where the warning happened.
    pub(crate) ip: usize,
    /// The kind of the warning.
    pub(crate) kind: RuntimeWarningDiagnosticKind,
}

impl RuntimeWarningDiagnostic {
    /// The instruction pointer of the vm where the warning happened.
    pub fn ip(&self) -> usize {
        self.ip
    }

    /// The kind of the warning.
    #[cfg(feature = "emit")]
    #[allow(unused)]
    pub(crate) fn kind(&self) -> &RuntimeWarningDiagnosticKind {
        &self.kind
    }

    #[cfg(test)]
    #[allow(unused)]
    pub(crate) fn into_kind(self) -> RuntimeWarningDiagnosticKind {
        self.kind
    }
}

impl fmt::Display for RuntimeWarningDiagnostic {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.kind, f)
    }
}

impl core::error::Error for RuntimeWarningDiagnostic {
    #[inline]
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        None
    }
}

/// The kind of a [RuntimeWarningDiagnostic].
#[derive(Debug)]
#[allow(missing_docs)]
#[non_exhaustive]
pub(crate) enum RuntimeWarningDiagnosticKind {
    UsedDeprecated {
        /// The hash which produced the deprecation
        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
        hash: Hash,
    },
}

impl fmt::Display for RuntimeWarningDiagnosticKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RuntimeWarningDiagnosticKind::UsedDeprecated { .. } => {
                write!(f, "Used deprecated function")
            }
        }
    }
}