miden_assembly_syntax/sema/
errors.rs

1// Allow unused assignments - required by miette::Diagnostic derive macro
2#![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/// The high-level error type for all semantic analysis errors.
11///
12/// This rolls up multiple errors into a single one, and as such, can emit many
13/// diagnostics at once. You should prefer to gather up errors and continue with
14/// analysis for as long as possible before returning an error of this type, so
15/// as to provide as much information to the user as possible, but there are cases
16/// where doing so would not be profitable - it is a judgement call.
17///
18/// The semantic analyzer does this though, and you can examine its implementation
19/// to see how we approach this during the semantic analysis phase specifically.
20#[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/// This type is used when emitting advice/warnings, when those are not being treated as errors.
31///
32/// Like [SyntaxError], this rolls up all such notices into a single batch, and emits them all
33/// at once. The difference is that we never return this as an error from any API, it simply
34/// exists to leverage the diagnostic infrastructure of `miette`.
35#[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/// Represents an error that occurs during semantic analysis
47#[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/// Represents a system limit that was exceeded
224#[derive(Debug, Copy, Clone, PartialEq, Eq)]
225pub enum LimitKind {
226    /// The total number of procedures in a module
227    Procedures,
228    /// The total number of procedure locals
229    Locals,
230    /// The total number of imports in a module
231    Imports,
232    /// The total number of calls to imports
233    ///
234    /// TODO(pauls): Is this even a limit anymore?
235    CalledImports,
236    /// The total number of instructions in a procedure.
237    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}