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
use midenc_hir::{
    diagnostics::{miette, Diagnostic, Report},
    SymbolConflictError,
};
use thiserror::Error;

/// A WebAssembly translation error.
///
/// When a WebAssembly function can't be translated, one of these error codes will be returned
/// to describe the failure.
#[allow(missing_docs)]
#[derive(Error, Debug, Diagnostic)]
pub enum WasmError {
    /// The input WebAssembly code is invalid.
    ///
    /// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
    /// code. This should never happen for validated WebAssembly code.
    #[error("invalid input WebAssembly code at offset {offset}: {message}")]
    #[diagnostic()]
    InvalidWebAssembly {
        /// A string describing the validation error.
        message: String,
        /// The bytecode offset where the error occurred.
        offset: usize,
    },

    /// A feature used by the WebAssembly code is not supported by the Miden IR.
    #[error("unsupported WebAssembly code: {0}")]
    #[diagnostic()]
    Unsupported(String),

    /// Too many functions were declared in a module
    #[error("Too many declared functions in the module")]
    #[diagnostic()]
    FuncNumLimitExceeded,

    /// Duplicate symbol names were found in a module
    #[error(transparent)]
    #[diagnostic(transparent)]
    SymbolConflictError(#[from] SymbolConflictError),

    #[error("import metadata is missing: {0}")]
    #[diagnostic()]
    MissingImportMetadata(String),

    #[error("export metadata is missing: {0}")]
    #[diagnostic()]
    MissingExportMetadata(String),

    #[error(transparent)]
    DwarfError(#[from] gimli::Error),

    #[error(transparent)]
    Io(#[from] std::io::Error),
}

impl From<wasmparser::BinaryReaderError> for WasmError {
    fn from(e: wasmparser::BinaryReaderError) -> Self {
        Self::InvalidWebAssembly {
            message: e.message().into(),
            offset: e.offset(),
        }
    }
}

/// A convenient alias for a `Result` that uses `WasmError` as the error type.
pub type WasmResult<T> = Result<T, Report>;

/// Emit diagnostics and return an `Err(WasmError::Unsupported(msg))` where `msg` the string built
/// by calling `format!` on the arguments to this macro.
#[macro_export]
macro_rules! unsupported_diag {
    ($diagnostics:expr, $($arg:tt)*) => {{
        return Err($diagnostics
            .diagnostic(Severity::Error)
            .with_message(format!($($arg)*))
            .into_report());
    }}
}