1use thiserror::Error;
4
5use crate::{BlockId, IrType, ValueId};
6
7pub type IrResult<T> = Result<T, IrError>;
9
10#[derive(Debug, Error)]
12pub enum IrError {
13 #[error("Type mismatch: expected {expected}, got {actual}")]
15 TypeMismatch {
16 expected: IrType,
18 actual: IrType,
20 },
21
22 #[error("Undefined value: {0}")]
24 UndefinedValue(ValueId),
25
26 #[error("Undefined block: {0}")]
28 UndefinedBlock(BlockId),
29
30 #[error("Block {0} is not terminated")]
32 UnterminatedBlock(BlockId),
33
34 #[error("Invalid operation {op} for type {ty}")]
36 InvalidOperation {
37 op: String,
39 ty: IrType,
41 },
42
43 #[error("Cannot cast from {from} to {to}")]
45 InvalidCast {
46 from: IrType,
48 to: IrType,
50 },
51
52 #[error("Capability not supported: {0}")]
54 CapabilityNotSupported(String),
55
56 #[error("Expected {expected} parameters, got {actual}")]
58 ParameterCountMismatch {
59 expected: usize,
61 actual: usize,
63 },
64
65 #[error("Invalid vector size: {0} (must be 2, 3, or 4)")]
67 InvalidVectorSize(u8),
68
69 #[error("Invalid array size: {0}")]
71 InvalidArraySize(usize),
72
73 #[error("Module has no entry block")]
75 MissingEntryBlock,
76
77 #[error("Duplicate definition: {0}")]
79 DuplicateDefinition(String),
80
81 #[error("Invalid phi node: {0}")]
83 InvalidPhi(String),
84
85 #[error("Control flow error: {0}")]
87 ControlFlowError(String),
88
89 #[error("Validation error: {0}")]
91 ValidationError(String),
92
93 #[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}