Skip to main content

clock_dbm/
error.rs

1//! The crate-wide error type.
2
3use creusot_std::prelude::{Clone, DeepModel, PartialEq, *};
4
5/// An invalid argument or an unrepresentable bound in a zone operation.
6///
7/// **An inconsistent zone is not an error:** it is returned as `Ok` with
8/// `Dbm::is_empty` equal to `true`. [`Error::Range`] instead indicates
9/// that exact finite arithmetic required a constant outside the supported
10/// range. Do not interpret `Range` as an empty zone or silently clamp it.
11///
12/// Argument validation precedes emptiness checks, so invalid arguments
13/// are rejected even for empty input zones.
14#[derive(Clone, Copy, PartialEq, Eq, Debug, DeepModel)]
15#[non_exhaustive]
16pub enum Error {
17    /// Exact arithmetic would leave `[MIN_CONSTANT, MAX_CONSTANT]`.
18    /// This is **not** emptiness and **not** "unsatisfiable".
19    Range,
20    /// More than `MAX_VARIABLES` clocks requested.
21    TooManyVariables {
22        /// The number of actual clocks requested.
23        requested: usize,
24    },
25    /// Two operands (DBM/DBM, DBM/MaxBounds) disagree on the number of variables.
26    DimensionMismatch {
27        /// The number of variables of the receiver.
28        expected: usize,
29        /// The number of variables of the other operand.
30        found: usize,
31    },
32    /// A clock index ≥ dimension of the DBM it is used with.
33    ClockOutOfRange {
34        /// The offending clock index.
35        clock: usize,
36        /// The number of actual clocks of the DBM.
37        variables: usize,
38    },
39    /// Reset to a negative value (clocks are nonnegative).
40    NegativeConstant {
41        /// The rejected reset value.
42        value: i32,
43    },
44    /// A `MaxBounds` ceiling is negative or unrepresentable.
45    InvalidMaxBounds {
46        /// The (1-based) actual clock whose ceiling is invalid.
47        variable: usize,
48        /// The rejected ceiling.
49        value: i32,
50    },
51}
52
53// Message formatting only; it computes no bound and is not part of the
54// verified kernel (see verification/PROOF_STATUS.md).
55#[cfg(not(creusot))]
56impl core::fmt::Display for Error {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        match *self {
59            Error::Range => f.write_str("bound constant outside the representable range"),
60            Error::TooManyVariables { requested } => {
61                write!(f, "too many clock variables requested: {requested}")
62            }
63            Error::DimensionMismatch { expected, found } => {
64                write!(
65                    f,
66                    "dimension mismatch: expected {expected} variables, found {found}"
67                )
68            }
69            Error::ClockOutOfRange { clock, variables } => {
70                write!(
71                    f,
72                    "clock index {clock} out of range for {variables} variables"
73                )
74            }
75            Error::NegativeConstant { value } => {
76                write!(f, "negative reset value {value}")
77            }
78            Error::InvalidMaxBounds { variable, value } => {
79                write!(f, "invalid maximal constant {value} for clock x{variable}")
80            }
81        }
82    }
83}
84
85#[cfg(not(creusot))]
86impl std::error::Error for Error {}