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
use super::Expr;
/// An expression representing an unreachable branch.
///
/// `Expr::Error` marks code paths that should never execute at runtime. The
/// primary use is the else branch of `ExprMatch` on enum discriminants: all
/// valid discriminants are covered by explicit arms, so the else branch is
/// semantically unreachable. If it IS reached (e.g., due to data corruption or
/// a schema mismatch), evaluation fails with the contained message.
///
/// # Simplifier semantics
///
/// Because Error is unreachable, simplification rules treat it as an opaque
/// value — no special propagation is needed. Existing rules handle it
/// naturally:
///
/// - `false AND (Error == x)` → `false` (short-circuit on `false`)
/// - `Record([disc, Error]) == Record([I64(1), "alice"])` decomposes into
/// `disc == I64(1) AND Error == "alice"`, and if `disc == I64(1)`
/// contradicts a guard like `disc != I64(1)`, the whole AND folds to
/// `false`.
///
/// In all well-formed cases, the guard constraints around Error cause the
/// branch to be pruned without requiring Error-specific rules.
///
/// # Type inference
///
/// `Expr::Error` infers as `Type::Unknown`. `TypeUnion::insert` skips
/// `Unknown`, so an Error branch doesn't widen inferred type unions.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprError {
/// The error message to surface if this expression is evaluated.
pub message: String,
}
impl Expr {
/// Creates an error expression with the given message.
///
/// If this expression is ever evaluated at runtime, it will fail with the
/// provided message.
pub fn error(message: impl Into<String>) -> Self {
ExprError {
message: message.into(),
}
.into()
}
}
impl From<ExprError> for Expr {
fn from(value: ExprError) -> Self {
Self::Error(value)
}
}