Skip to main content

tenferro_runtime/program/
error.rs

1/// Errors reported while adding semantic-program structure.
2#[derive(Debug, thiserror::Error)]
3#[non_exhaustive]
4pub enum ProgramBuildError {
5    /// A value token was issued by another builder.
6    #[error("value does not belong to this semantic-program builder")]
7    ForeignValue,
8    /// An import root belongs to another semantic program.
9    #[error("import root does not belong to the source semantic program")]
10    ForeignImportRoot,
11    /// Import bindings were frozen for another semantic program.
12    #[error("import bindings do not belong to the source semantic program")]
13    ForeignBindings,
14    /// Structured control flow is reserved for the later region/block model.
15    #[error("semantic control-flow construct {construct:?} is not supported")]
16    UnsupportedControlFlow {
17        /// Frontend construct that could not be represented.
18        construct: &'static str,
19    },
20    /// A frozen source program failed import-time structural validation.
21    #[error("source semantic program is invalid for import: {source}")]
22    InvalidImport {
23        #[source]
24        source: ProgramStructuralError,
25    },
26    /// A binding target is a computed value rather than an external input.
27    #[error("tensor bindings may target only semantic-program inputs")]
28    BindingTargetNotInput,
29    /// A shape guard target is not produced by a semantic operation.
30    #[error("semantic shape guards may target only operation outputs")]
31    GuardTargetNotOperationOutput,
32    /// An external input already has a tensor binding.
33    #[error("semantic-program input already has a tensor binding")]
34    DuplicateBinding,
35    /// The builder cannot represent another value slot.
36    #[error("semantic-program value count exceeds the supported u32 range")]
37    TooManyValues,
38    /// An operation received the wrong number of SSA inputs.
39    #[error("semantic operation expects {expected} inputs, got {actual}")]
40    Arity {
41        /// Declared input count.
42        expected: usize,
43        /// Supplied input count.
44        actual: usize,
45    },
46    /// Semantic dtype or shape inference failed.
47    #[error("semantic operation metadata inference failed: {source}")]
48    Metadata {
49        /// Original typed runtime inference error.
50        #[source]
51        source: Box<crate::Error>,
52    },
53    /// Inference did not return the declared number of outputs.
54    #[error("semantic operation declares {expected} outputs, inferred {actual}")]
55    OutputMetadataCount {
56        /// Declared output count.
57        expected: usize,
58        /// Inferred metadata count.
59        actual: usize,
60    },
61    /// An extension did not explicitly declare its observable effects.
62    #[error("extension family {family:?} has no semantic effect declaration")]
63    UndeclaredExtensionEffects {
64        /// Stable extension family.
65        family: &'static str,
66    },
67    /// An extension did not explicitly declare output aliases.
68    #[error("extension family {family:?} has no semantic alias declaration")]
69    UndeclaredExtensionAliases {
70        /// Stable extension family.
71        family: &'static str,
72    },
73    /// An extension declared an invalid resource family.
74    #[error("extension family {family:?} declared an invalid effect resource: {source}")]
75    InvalidEffectResource {
76        /// Stable extension family.
77        family: &'static str,
78        /// Typed resource validation failure.
79        #[source]
80        source: EffectResourceError,
81    },
82    /// An alias referenced an input or output outside the operation arity.
83    #[error(
84        "semantic alias index is out of bounds: output {output}/{output_count}, input {input:?}/{input_count}"
85    )]
86    AliasOutOfBounds {
87        /// Referenced output.
88        output: usize,
89        /// Output arity.
90        output_count: usize,
91        /// Referenced input, when applicable.
92        input: Option<usize>,
93        /// Input arity.
94        input_count: usize,
95    },
96    /// Alias declarations did not cover every output exactly once.
97    #[error("semantic aliases must cover {expected} outputs exactly once, got {actual}")]
98    AliasCoverage {
99        /// Output arity.
100        expected: usize,
101        /// Number of distinct valid output declarations.
102        actual: usize,
103    },
104}
105
106/// Errors reported while querying an immutable semantic program.
107#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
108pub enum ProgramQueryError {
109    /// A value token belongs to another program or names no frozen value.
110    #[error("value does not belong to this semantic program")]
111    ForeignValue,
112}
113
114/// Internal structural validation failures detected during atomic freeze.
115#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
116#[non_exhaustive]
117pub enum ProgramStructuralError {
118    /// An operation references a value outside the frozen value table.
119    #[error("semantic operation references a value outside the program")]
120    InvalidValueReference,
121    /// An operation output is not strictly after all prior values.
122    #[error("semantic operation outputs violate SSA ordering")]
123    InvalidSsaOrder,
124}
125
126/// Tensor-binding validation failures detected during atomic freeze.
127#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
128#[non_exhaustive]
129pub enum ProgramBindingError {
130    /// A pending binding no longer names an external input.
131    #[error("tensor binding target is not a semantic-program input")]
132    InvalidTarget,
133    /// Tensor dtype differs from the input declaration.
134    #[error("tensor binding dtype mismatch: expected {expected:?}, got {actual:?}")]
135    DTypeMismatch {
136        /// Declared input dtype.
137        expected: tenferro_tensor::DType,
138        /// Bound tensor dtype.
139        actual: tenferro_tensor::DType,
140    },
141    /// Tensor rank differs from the input declaration.
142    #[error("tensor binding rank mismatch: expected {expected}, got {actual}")]
143    RankMismatch {
144        /// Declared input rank.
145        expected: usize,
146        /// Bound tensor rank.
147        actual: usize,
148    },
149    /// A statically exact dimension differs from the input declaration.
150    #[error("tensor binding extent mismatch at axis {axis}: expected {expected}, got {actual}")]
151    ExactExtentMismatch {
152        /// Mismatching axis.
153        axis: usize,
154        /// Declared exact extent.
155        expected: usize,
156        /// Bound tensor extent.
157        actual: usize,
158    },
159    /// A bounded dimension exceeds the declared upper bound.
160    #[error(
161        "tensor binding extent exceeds upper bound at axis {axis}: bound {bound}, got {actual}"
162    )]
163    UpperBoundExceeded {
164        /// Axis whose bound was exceeded.
165        axis: usize,
166        /// Declared upper bound.
167        bound: usize,
168        /// Bound tensor extent.
169        actual: usize,
170    },
171}
172
173/// Errors reported while atomically freezing semantic structure and bindings.
174#[derive(Debug, thiserror::Error)]
175#[non_exhaustive]
176pub enum ProgramFinishError {
177    /// One requested output belongs to another builder or names no value.
178    #[error("program output does not belong to this semantic-program builder")]
179    ForeignOutput,
180    /// Frozen structure failed an invariant check.
181    #[error("semantic-program structural validation failed: {source}")]
182    StructuralValidation {
183        /// Typed structural invariant failure.
184        #[source]
185        source: ProgramStructuralError,
186    },
187    /// A tensor default or large constant does not match its input declaration.
188    #[error("semantic-program binding finalization failed: {source}")]
189    BindingFinalization {
190        /// Typed binding mismatch.
191        #[source]
192        source: ProgramBindingError,
193    },
194}
195
196/// Failures produced by a validated semantic transform transaction.
197#[derive(Debug, thiserror::Error)]
198#[non_exhaustive]
199pub enum SemanticTransformError {
200    /// Program construction or import failed inside the transform.
201    #[error(transparent)]
202    Build(#[from] ProgramBuildError),
203    /// Atomic freeze failed after the transform returned roots.
204    #[error(transparent)]
205    Finish(#[from] ProgramFinishError),
206    /// The transform returned a value not owned by its destination builder.
207    #[error("semantic transform returned a foreign destination value")]
208    ForeignReturnedValue,
209    /// The transform did not carry every input tensor binding forward.
210    #[error("semantic transform discarded one or more tensor bindings")]
211    DroppedBindings,
212    /// The transform deliberately rejected this input.
213    #[error("semantic transform rejected the input")]
214    Rejected,
215}
216
217/// Invalid typed effect-resource identity.
218#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
219pub enum EffectResourceError {
220    /// Resource-family names must be nonempty and versioned.
221    #[error("effect resource family must be a nonempty versioned identifier")]
222    InvalidFamily,
223}