From 53a13b145dcd3b2b2796ff8f4d2ba30c906f3b81 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 23 Jul 2020 15:08:38 +0200 Subject: [PATCH 1/2] fix(compiler) Implement `thiserror::Error` if `std` is present. `thiserror` doesn't work in `no_std` mode, see https://github.com/dtolnay/thiserror/pull/64. --- lib/compiler/src/error.rs | 43 +++++++++++++++++++++++++++----------- lib/compiler/src/target.rs | 11 +--------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/compiler/src/error.rs b/lib/compiler/src/error.rs index 08e98f91f..88dcd6b7d 100644 --- a/lib/compiler/src/error.rs +++ b/lib/compiler/src/error.rs @@ -1,7 +1,11 @@ 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. @@ -9,26 +13,27 @@ use thiserror::Error; /// 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), } @@ -36,13 +41,17 @@ pub enum CompileError { /// /// 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, @@ -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 = Result; diff --git a/lib/compiler/src/target.rs b/lib/compiler/src/target.rs index 388ded79d..d5f7ee2e3 100644 --- a/lib/compiler/src/target.rs +++ b/lib/compiler/src/target.rs @@ -1,4 +1,5 @@ //! Target configuration +use crate::error::ParseCpuFeatureError; use enumset::{EnumSet, EnumSetType}; use std::str::FromStr; use std::string::ToString; @@ -6,7 +7,6 @@ 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; @@ -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: // From 6df0645ab9b555188ea8a2b43f3fc04758157593 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 23 Jul 2020 15:09:46 +0200 Subject: [PATCH 2/2] fix(compiler) Make it work without `std`. This patch uses our `lib` module that builds an `std` module depending of the `core` or `std` feature flag. --- lib/compiler/src/lib.rs | 9 ++++----- lib/compiler/src/module.rs | 2 +- lib/compiler/src/target.rs | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/compiler/src/lib.rs b/lib/compiler/src/lib.rs index 4a0f90298..ef7a9972b 100644 --- a/lib/compiler/src/lib.rs +++ b/lib/compiler/src/lib.rs @@ -38,15 +38,14 @@ extern crate alloc; mod lib { #[cfg(feature = "core")] pub mod std { - #[macro_use] - pub use alloc::{borrow, boxed, string, vec, sync}; + pub use alloc::{borrow, boxed, str, string, sync, vec}; pub use core::fmt; pub use hashbrown as collections; } #[cfg(feature = "std")] pub mod std { - pub use std::{borrow, boxed, collections, fmt, string, sync, vec}; + pub use std::{borrow, boxed, collections, fmt, str, string, sync, vec}; } } @@ -70,7 +69,7 @@ mod sourceloc; pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap}; #[cfg(feature = "translator")] pub use crate::compiler::{Compiler, CompilerConfig}; -pub use crate::error::{CompileError, WasmError, WasmResult}; +pub use crate::error::{CompileError, ParseCpuFeatureError, WasmError, WasmResult}; pub use crate::function::{ Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody, Functions, @@ -82,7 +81,7 @@ pub use crate::section::{CustomSection, CustomSectionProtection, SectionBody, Se pub use crate::sourceloc::SourceLoc; pub use crate::target::{ Architecture, BinaryFormat, CallingConvention, CpuFeature, Endianness, OperatingSystem, - ParseCpuFeatureError, PointerWidth, Target, Triple, + PointerWidth, Target, Triple, }; #[cfg(feature = "translator")] pub use crate::translator::{ diff --git a/lib/compiler/src/module.rs b/lib/compiler/src/module.rs index 53321243a..d732d563e 100644 --- a/lib/compiler/src/module.rs +++ b/lib/compiler/src/module.rs @@ -1,6 +1,6 @@ +use crate::lib::std::sync::Arc; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; -use std::sync::Arc; use wasm_common::entity::PrimaryMap; use wasm_common::{Features, MemoryIndex, TableIndex}; use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle}; diff --git a/lib/compiler/src/target.rs b/lib/compiler/src/target.rs index d5f7ee2e3..b6c20ffdb 100644 --- a/lib/compiler/src/target.rs +++ b/lib/compiler/src/target.rs @@ -1,8 +1,8 @@ //! Target configuration use crate::error::ParseCpuFeatureError; +use crate::lib::std::str::FromStr; +use crate::lib::std::string::{String, ToString}; use enumset::{EnumSet, EnumSetType}; -use std::str::FromStr; -use std::string::ToString; pub use target_lexicon::{ Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth, Triple,