toasty_core/stmt/
expr_error.rs

1use super::Expr;
2
3/// An expression representing an unreachable branch.
4///
5/// `Expr::Error` marks code paths that should never execute at runtime. The
6/// primary use is the else branch of `ExprMatch` on enum discriminants: all
7/// valid discriminants are covered by explicit arms, so the else branch is
8/// semantically unreachable. If it IS reached (e.g., due to data corruption or
9/// a schema mismatch), evaluation fails with the contained message.
10///
11/// # Simplifier semantics
12///
13/// Because Error is unreachable, simplification rules treat it as an opaque
14/// value — no special propagation is needed. Existing rules handle it
15/// naturally:
16///
17/// - `false AND (Error == x)` → `false` (short-circuit on `false`)
18/// - `Record([disc, Error]) == Record([I64(1), "alice"])` decomposes into
19///   `disc == I64(1) AND Error == "alice"`, and if `disc == I64(1)`
20///   contradicts a guard like `disc != I64(1)`, the whole AND folds to
21///   `false`.
22///
23/// In all well-formed cases, the guard constraints around Error cause the
24/// branch to be pruned without requiring Error-specific rules.
25///
26/// # Type inference
27///
28/// `Expr::Error` infers as `Type::Unknown`. `TypeUnion::insert` skips
29/// `Unknown`, so an Error branch doesn't widen inferred type unions.
30#[derive(Debug, Clone, PartialEq)]
31pub struct ExprError {
32    /// The error message to surface if this expression is evaluated.
33    pub message: String,
34}
35
36impl Expr {
37    pub fn error(message: impl Into<String>) -> Self {
38        ExprError {
39            message: message.into(),
40        }
41        .into()
42    }
43}
44
45impl From<ExprError> for Expr {
46    fn from(value: ExprError) -> Self {
47        Self::Error(value)
48    }
49}