miden_assembly/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use alloc::{string::String, sync::Arc, vec::Vec};

use vm_core::mast::MastForestError;

use crate::{
    ast::QualifiedProcedureName,
    diagnostics::{Diagnostic, RelatedError, RelatedLabel, Report, SourceFile},
    LibraryNamespace, LibraryPath, SourceSpan,
};

// ASSEMBLY ERROR
// ================================================================================================

/// An error which can be generated while compiling a Miden assembly program into a MAST.
#[derive(Debug, thiserror::Error, Diagnostic)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum AssemblyError {
    #[error("there are no modules to analyze")]
    #[diagnostic()]
    Empty,
    #[error("assembly failed")]
    #[diagnostic(help("see diagnostics for details"))]
    Failed {
        #[related]
        labels: Vec<RelatedLabel>,
    },
    #[error("found a cycle in the call graph, involving these procedures: {}", nodes.as_slice().join(", "))]
    #[diagnostic()]
    Cycle { nodes: Vec<String> },
    #[error("two procedures found with same mast root, but conflicting definitions ('{first}' and '{second}')")]
    #[diagnostic()]
    ConflictingDefinitions {
        first: QualifiedProcedureName,
        second: QualifiedProcedureName,
    },
    #[error("duplicate definition found for module '{path}'")]
    #[diagnostic()]
    DuplicateModule { path: LibraryPath },
    #[error("undefined module '{path}'")]
    #[diagnostic()]
    UndefinedModule {
        #[label]
        span: SourceSpan,
        #[source_code]
        source_file: Option<Arc<SourceFile>>,
        path: LibraryPath,
    },
    #[error("module namespace is inconsistent with library ('{actual}' vs '{expected}')")]
    #[diagnostic()]
    InconsistentNamespace {
        expected: LibraryNamespace,
        actual: LibraryNamespace,
    },
    #[error("invalid syscall: '{callee}' is not an exported kernel procedure")]
    #[diagnostic()]
    InvalidSysCallTarget {
        #[label("call occurs here")]
        span: SourceSpan,
        #[source_code]
        source_file: Option<Arc<SourceFile>>,
        callee: QualifiedProcedureName,
    },
    #[error("invalid use of 'caller' instruction outside of kernel")]
    #[diagnostic(help(
        "the 'caller' instruction is only allowed in procedures defined in a kernel"
    ))]
    CallerOutsideOfKernel {
        #[label]
        span: SourceSpan,
        #[source_code]
        source_file: Option<Arc<SourceFile>>,
    },

    #[error("invalid procedure: body must contain at least one instruction if it has decorators")]
    #[diagnostic()]
    EmptyProcedureBodyWithDecorators {
        span: SourceSpan,
        #[source_code]
        source_file: Option<Arc<SourceFile>>,
    },
    #[error(transparent)]
    #[diagnostic(transparent)]
    Other(#[from] RelatedError),
    #[error(transparent)]
    Forest(#[from] MastForestError),
}

impl From<Report> for AssemblyError {
    fn from(report: Report) -> Self {
        Self::Other(RelatedError::new(report))
    }
}