miden_assembly_syntax/sema/
errors.rs1#![allow(unused_assignments)]
3
4use alloc::{sync::Arc, vec::Vec};
5use core::fmt;
6
7use miden_debug_types::{SourceFile, SourceSpan};
8use miden_utils_diagnostics::{Diagnostic, miette};
9
10#[derive(Debug, thiserror::Error, Diagnostic)]
21#[error("syntax error")]
22#[diagnostic(help("see emitted diagnostics for details"))]
23pub struct SyntaxError {
24 #[source_code]
25 pub source_file: Arc<SourceFile>,
26 #[related]
27 pub errors: Vec<SemanticAnalysisError>,
28}
29
30#[derive(Debug, thiserror::Error, Diagnostic)]
36#[error("one or more warnings were emitted")]
37#[diagnostic(help("see below for details"))]
38#[cfg_attr(not(feature = "std"), allow(unused))]
39pub struct SyntaxWarning {
40 #[source_code]
41 pub source_file: Arc<SourceFile>,
42 #[related]
43 pub errors: Vec<SemanticAnalysisError>,
44}
45
46#[derive(Debug, thiserror::Error, Diagnostic)]
48pub enum SemanticAnalysisError {
49 #[error("invalid program: no entrypoint defined")]
50 #[diagnostic(help(
51 "ensure you define an entrypoint somewhere in the body with `begin`..`end`"
52 ))]
53 MissingEntrypoint,
54 #[error("invalid module: unexpected entrypoint definition")]
55 #[diagnostic(help("library modules cannot contain `begin`..`end` blocks"))]
56 UnexpectedEntrypoint {
57 #[label]
58 span: SourceSpan,
59 },
60 #[error("invalid module: multiple conflicting entrypoints defined")]
61 #[diagnostic(help("an executable module can only have a single `begin`..`end` block"))]
62 MultipleEntrypoints {
63 #[label]
64 span: SourceSpan,
65 #[label]
66 prev_span: SourceSpan,
67 },
68 #[error("invalid program: procedure exports are not allowed")]
69 #[diagnostic(help("perhaps you meant to use `proc` instead of `export`?"))]
70 UnexpectedExport {
71 #[label]
72 span: SourceSpan,
73 },
74 #[error("invalid enum type representation: underlying type must be an integral type")]
75 #[diagnostic()]
76 InvalidEnumRepr {
77 #[label]
78 span: SourceSpan,
79 },
80 #[error("invalid enum discriminant: value is not a valid instance of the {repr} type")]
81 #[diagnostic()]
82 InvalidEnumDiscriminant {
83 #[label]
84 span: SourceSpan,
85 repr: crate::ast::types::Type,
86 },
87 #[error("invalid enum discriminant: value conflicts with another variant of the same enum")]
88 #[diagnostic()]
89 EnumDiscriminantConflict {
90 #[label("this discriminant value conflicts with a previous variant")]
91 span: SourceSpan,
92 #[label("discriminant previously observed here")]
93 prev: SourceSpan,
94 },
95 #[error("symbol conflict: found duplicate definitions of the same name")]
96 #[diagnostic()]
97 SymbolConflict {
98 #[label("conflict occurs here")]
99 span: SourceSpan,
100 #[label("previously defined here")]
101 prev_span: SourceSpan,
102 },
103 #[error("symbol undefined: no such name found in scope")]
104 #[diagnostic(help("are you missing an import?"))]
105 SymbolUndefined {
106 #[label]
107 span: SourceSpan,
108 },
109 #[error("unused import")]
110 #[diagnostic(severity(Warning), help("this import is never used and can be safely removed"))]
111 UnusedImport {
112 #[label]
113 span: SourceSpan,
114 },
115 #[error("missing import: the referenced module has not been imported")]
116 #[diagnostic()]
117 MissingImport {
118 #[label("this reference is invalid without a corresponding import")]
119 span: SourceSpan,
120 },
121 #[error("symbol conflict: import would shadow a previous import of the same name")]
122 #[diagnostic(help(
123 "imports must have unique names within a module, \
124 try aliasing one of the imports if both are needed"
125 ))]
126 ImportConflict {
127 #[label("caused by this import")]
128 span: SourceSpan,
129 #[label("previously imported here")]
130 prev_span: SourceSpan,
131 },
132 #[error(
133 "invalid re-exported procedure: kernel modules may not re-export procedures from other modules"
134 )]
135 #[diagnostic()]
136 ReexportFromKernel {
137 #[label]
138 span: SourceSpan,
139 },
140 #[error("invalid instruction usage: 'caller' is only valid in kernel modules")]
141 #[diagnostic()]
142 CallerInKernel {
143 #[label]
144 span: SourceSpan,
145 },
146 #[error("invalid syscall: callee must be resolvable to kernel module")]
147 #[diagnostic()]
148 InvalidSyscallTarget {
149 #[label]
150 span: SourceSpan,
151 },
152 #[error("invalid recursive procedure call")]
153 #[diagnostic(help(
154 "this call induces a cycle that returns back to the caller, you must break that cycle"
155 ))]
156 InvalidRecursiveCall {
157 #[label("caused by this call")]
158 span: SourceSpan,
159 },
160 #[error("invalid recursive procedure call")]
161 #[diagnostic(help("this call is self-recursive, which is not allowed"))]
162 SelfRecursive {
163 #[label]
164 span: SourceSpan,
165 },
166 #[error("invalid immediate: value is larger than expected range")]
167 #[diagnostic()]
168 ImmediateOverflow {
169 #[label]
170 span: SourceSpan,
171 },
172 #[error("invalid module: {}", kind)]
173 #[diagnostic(help("try breaking this module up into submodules"))]
174 LimitExceeded {
175 #[label]
176 span: SourceSpan,
177 kind: LimitKind,
178 },
179 #[error("unused docstring")]
180 #[diagnostic(
181 severity(Warning),
182 help(
183 "this docstring is immediately followed by at least one empty line, then another docstring,\
184 if you intended these to be a single docstring, you should remove the empty lines"
185 )
186 )]
187 UnusedDocstring {
188 #[label]
189 span: SourceSpan,
190 },
191 #[error("unused docstring")]
192 #[diagnostic(
193 severity(Warning),
194 help(
195 "module imports cannot have docstrings, you should use line comment syntax here instead"
196 )
197 )]
198 ImportDocstring {
199 #[label]
200 span: SourceSpan,
201 },
202 #[error("invalid constant")]
203 #[diagnostic(help("this constant does not resolve to a value of the right type"))]
204 InvalidConstant {
205 #[label]
206 span: SourceSpan,
207 },
208 #[error("constant evaluation terminated due to infinite recursion")]
209 #[diagnostic(help("dependencies between constants must form an acyclic graph"))]
210 ConstEvalCycle {
211 #[label("occurs while evaluating this expression")]
212 start: SourceSpan,
213 #[label("cycle occurs because we attempt to eval this constant recursively")]
214 detected: SourceSpan,
215 },
216 #[error("advmap key already defined")]
217 AdvMapKeyAlreadyDefined {
218 #[label]
219 span: SourceSpan,
220 },
221}
222
223#[derive(Debug, Copy, Clone, PartialEq, Eq)]
225pub enum LimitKind {
226 Procedures,
228 Locals,
230 Imports,
232 CalledImports,
236 Instructions,
238}
239
240impl fmt::Display for LimitKind {
241 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
242 match self {
243 Self::Procedures => f.write_str("too many procedures in module"),
244 Self::Locals => f.write_str("too many procedure locals"),
245 Self::Imports => f.write_str("too many imported procedures"),
246 Self::CalledImports => f.write_str("too many calls to imported procedures"),
247 Self::Instructions => f.write_str("too many instructions in block"),
248 }
249 }
250}