Skip to main content

ringkernel_ir/
error.rs

1//! IR error types.
2
3use thiserror::Error;
4
5use crate::{BlockId, IrType, ValueId};
6
7/// IR result type.
8pub type IrResult<T> = Result<T, IrError>;
9
10/// IR errors.
11#[derive(Debug, Error)]
12pub enum IrError {
13    /// Type mismatch.
14    #[error("Type mismatch: expected {expected}, got {actual}")]
15    TypeMismatch {
16        /// Expected type.
17        expected: IrType,
18        /// Actual type.
19        actual: IrType,
20    },
21
22    /// Undefined value reference.
23    #[error("Undefined value: {0}")]
24    UndefinedValue(ValueId),
25
26    /// Undefined block reference.
27    #[error("Undefined block: {0}")]
28    UndefinedBlock(BlockId),
29
30    /// Block not terminated.
31    #[error("Block {0} is not terminated")]
32    UnterminatedBlock(BlockId),
33
34    /// Invalid operation for type.
35    #[error("Invalid operation {op} for type {ty}")]
36    InvalidOperation {
37        /// Operation name.
38        op: String,
39        /// Type involved.
40        ty: IrType,
41    },
42
43    /// Invalid cast.
44    #[error("Cannot cast from {from} to {to}")]
45    InvalidCast {
46        /// Source type.
47        from: IrType,
48        /// Target type.
49        to: IrType,
50    },
51
52    /// Capability not supported.
53    #[error("Capability not supported: {0}")]
54    CapabilityNotSupported(String),
55
56    /// Invalid parameter count.
57    #[error("Expected {expected} parameters, got {actual}")]
58    ParameterCountMismatch {
59        /// Expected count.
60        expected: usize,
61        /// Actual count.
62        actual: usize,
63    },
64
65    /// Invalid vector size.
66    #[error("Invalid vector size: {0} (must be 2, 3, or 4)")]
67    InvalidVectorSize(u8),
68
69    /// Invalid array size.
70    #[error("Invalid array size: {0}")]
71    InvalidArraySize(usize),
72
73    /// Missing entry block.
74    #[error("Module has no entry block")]
75    MissingEntryBlock,
76
77    /// Duplicate definition.
78    #[error("Duplicate definition: {0}")]
79    DuplicateDefinition(String),
80
81    /// Invalid phi node.
82    #[error("Invalid phi node: {0}")]
83    InvalidPhi(String),
84
85    /// Control flow error.
86    #[error("Control flow error: {0}")]
87    ControlFlowError(String),
88
89    /// Validation error.
90    #[error("Validation error: {0}")]
91    ValidationError(String),
92
93    /// Internal error.
94    #[error("Internal error: {0}")]
95    Internal(String),
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn test_error_display() {
104        let err = IrError::TypeMismatch {
105            expected: IrType::I32,
106            actual: IrType::F32,
107        };
108        assert!(err.to_string().contains("i32"));
109        assert!(err.to_string().contains("f32"));
110    }
111
112    #[test]
113    fn test_undefined_value() {
114        let id = ValueId::new();
115        let err = IrError::UndefinedValue(id);
116        assert!(err.to_string().contains("Undefined value"));
117    }
118}