Skip to content

Commit 53a13b1

Browse files
committed
fix(compiler) Implement thiserror::Error if std is present.
`thiserror` doesn't work in `no_std` mode, see dtolnay/thiserror#64.
1 parent 98e5b7e commit 53a13b1

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

lib/compiler/src/error.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
use crate::lib::std::string::String;
2+
#[cfg(feature = "std")]
23
use thiserror::Error;
34

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

610
/// The WebAssembly.CompileError object indicates an error during
711
/// WebAssembly decoding or validation.
812
///
913
/// This is based on the [Wasm Compile Error][compile-error] API.
1014
///
1115
/// [compiler-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/CompileError
12-
#[derive(Error, Debug)]
16+
#[derive(Debug)]
17+
#[cfg_attr(feature = "std", derive(Error))]
1318
pub enum CompileError {
1419
/// A Wasm translation error occured.
15-
#[error("WebAssembly translation error: {0}")]
16-
Wasm(#[from] WasmError),
20+
#[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
21+
Wasm(#[cfg_attr(feature = "std", from)] WasmError),
1722

1823
/// A compilation error occured.
19-
#[error("Compilation error: {0}")]
24+
#[cfg_attr(feature = "std", error("Compilation error: {0}"))]
2025
Codegen(String),
2126

2227
/// The module did not pass validation.
23-
#[error("Validation error: {0}")]
28+
#[cfg_attr(feature = "std", error("Validation error: {0}"))]
2429
Validate(String),
2530

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

3035
/// Insufficient resources available for execution.
31-
#[error("Insufficient resources: {0}")]
36+
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
3237
Resource(String),
3338
}
3439

3540
/// A WebAssembly translation error.
3641
///
3742
/// When a WebAssembly function can't be translated, one of these error codes will be returned
3843
/// to describe the failure.
39-
#[derive(Error, Debug)]
44+
#[derive(Debug)]
45+
#[cfg_attr(feature = "std", derive(Error))]
4046
pub enum WasmError {
4147
/// The input WebAssembly code is invalid.
4248
///
4349
/// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
4450
/// code. This should never happen for validated WebAssembly code.
45-
#[error("Invalid input WebAssembly code at offset {offset}: {message}")]
51+
#[cfg_attr(
52+
feature = "std",
53+
error("Invalid input WebAssembly code at offset {offset}: {message}")
54+
)]
4655
InvalidWebAssembly {
4756
/// A string describing the validation error.
4857
message: String,
@@ -53,17 +62,27 @@ pub enum WasmError {
5362
/// A feature used by the WebAssembly code is not supported by the embedding environment.
5463
///
5564
/// Embedding environments may have their own limitations and feature restrictions.
56-
#[error("Unsupported feature: {0}")]
65+
#[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
5766
Unsupported(String),
5867

5968
/// An implementation limit was exceeded.
60-
#[error("Implementation limit exceeded")]
69+
#[cfg_attr(feature = "std", error("Implementation limit exceeded"))]
6170
ImplLimitExceeded,
6271

6372
/// A generic error.
64-
#[error("{0}")]
73+
#[cfg_attr(feature = "std", error("{0}"))]
6574
Generic(String),
6675
}
6776

77+
/// The error that can happen while parsing a `str`
78+
/// to retrieve a [`CpuFeature`].
79+
#[derive(Debug)]
80+
#[cfg_attr(feature = "std", derive(Error))]
81+
pub enum ParseCpuFeatureError {
82+
/// The provided string feature doesn't exist
83+
#[cfg_attr(feature = "std", error("CpuFeature {0} not recognized"))]
84+
Missing(String),
85+
}
86+
6887
/// A convenient alias for a `Result` that uses `WasmError` as the error type.
6988
pub type WasmResult<T> = Result<T, WasmError>;

lib/compiler/src/target.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Target configuration
2+
use crate::error::ParseCpuFeatureError;
23
use enumset::{EnumSet, EnumSetType};
34
use std::str::FromStr;
45
use std::string::ToString;
56
pub use target_lexicon::{
67
Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth,
78
Triple,
89
};
9-
use thiserror::Error;
1010

1111
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1212
use raw_cpuid::CpuId;
@@ -112,15 +112,6 @@ impl CpuFeature {
112112
}
113113
}
114114

115-
/// The error that can happen while parsing a `str`
116-
/// to retrieve a [`CpuFeature`].
117-
#[derive(Error, Debug)]
118-
pub enum ParseCpuFeatureError {
119-
/// The provided string feature doesn't exist
120-
#[error("CpuFeature {0} not recognized")]
121-
Missing(String),
122-
}
123-
124115
// This options should map exactly the GCC options indicated
125116
// here by architectures:
126117
//

0 commit comments

Comments
 (0)