Skip to content

Commit

Permalink
fix(compiler) Implement thiserror::Error if std is present.
Browse files Browse the repository at this point in the history
`thiserror` doesn't work in `no_std` mode, see dtolnay/thiserror#64.
  • Loading branch information
Hywan committed Jul 23, 2020
1 parent 98e5b7e commit 53a13b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
43 changes: 31 additions & 12 deletions lib/compiler/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
use crate::lib::std::string::String;
#[cfg(feature = "std")]
use thiserror::Error;

// Compilation Errors
//
// If `std` feature is enable, we can't use `thiserror` until
// https://github.com/dtolnay/thiserror/pull/64 is merged.

/// The WebAssembly.CompileError object indicates an error during
/// WebAssembly decoding or validation.
///
/// This is based on the [Wasm Compile Error][compile-error] API.
///
/// [compiler-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/CompileError
#[derive(Error, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum CompileError {
/// A Wasm translation error occured.
#[error("WebAssembly translation error: {0}")]
Wasm(#[from] WasmError),
#[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
Wasm(#[cfg_attr(feature = "std", from)] WasmError),

/// A compilation error occured.
#[error("Compilation error: {0}")]
#[cfg_attr(feature = "std", error("Compilation error: {0}"))]
Codegen(String),

/// The module did not pass validation.
#[error("Validation error: {0}")]
#[cfg_attr(feature = "std", error("Validation error: {0}"))]
Validate(String),

/// The compiler doesn't support a Wasm feature
#[error("Feature {0} is not yet supported")]
#[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
UnsupportedFeature(String),

/// Insufficient resources available for execution.
#[error("Insufficient resources: {0}")]
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
}

/// A WebAssembly translation error.
///
/// When a WebAssembly function can't be translated, one of these error codes will be returned
/// to describe the failure.
#[derive(Error, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
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}")]
#[cfg_attr(
feature = "std",
error("Invalid input WebAssembly code at offset {offset}: {message}")
)]
InvalidWebAssembly {
/// A string describing the validation error.
message: String,
Expand All @@ -53,17 +62,27 @@ pub enum WasmError {
/// A feature used by the WebAssembly code is not supported by the embedding environment.
///
/// Embedding environments may have their own limitations and feature restrictions.
#[error("Unsupported feature: {0}")]
#[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
Unsupported(String),

/// An implementation limit was exceeded.
#[error("Implementation limit exceeded")]
#[cfg_attr(feature = "std", error("Implementation limit exceeded"))]
ImplLimitExceeded,

/// A generic error.
#[error("{0}")]
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}

/// The error that can happen while parsing a `str`
/// to retrieve a [`CpuFeature`].
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum ParseCpuFeatureError {
/// The provided string feature doesn't exist
#[cfg_attr(feature = "std", error("CpuFeature {0} not recognized"))]
Missing(String),
}

/// A convenient alias for a `Result` that uses `WasmError` as the error type.
pub type WasmResult<T> = Result<T, WasmError>;
11 changes: 1 addition & 10 deletions lib/compiler/src/target.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Target configuration
use crate::error::ParseCpuFeatureError;
use enumset::{EnumSet, EnumSetType};
use std::str::FromStr;
use std::string::ToString;
pub use target_lexicon::{
Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth,
Triple,
};
use thiserror::Error;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use raw_cpuid::CpuId;
Expand Down Expand Up @@ -112,15 +112,6 @@ impl CpuFeature {
}
}

/// The error that can happen while parsing a `str`
/// to retrieve a [`CpuFeature`].
#[derive(Error, Debug)]
pub enum ParseCpuFeatureError {
/// The provided string feature doesn't exist
#[error("CpuFeature {0} not recognized")]
Missing(String),
}

// This options should map exactly the GCC options indicated
// here by architectures:
//
Expand Down

0 comments on commit 53a13b1

Please sign in to comment.