A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix generated code for `anyhow: true` in wit-bindgen (#12334)
Nick Fitzgerald9a8741009e82
diff --git a/crates/component-macro/tests/expanded/resources-export_concurrent.rs b/crates/component-macro/tests/expanded/resources-export_concurrent.rsindex c3d5ec53a8..f7a4b34441 100644--- a/crates/component-macro/tests/expanded/resources-export_concurrent.rs+++ b/crates/component-macro/tests/expanded/resources-export_concurrent.rs@@ -329,7 +329,7 @@ pub mod exports {.ok_or_else(|| {wasmtime::format_err!("instance export `foo:foo/simple-export` does \- not have export `{name}`"+ not have export `{name}`")})};@@ -486,7 +486,7 @@ pub mod exports {.ok_or_else(|| {wasmtime::format_err!("instance export `foo:foo/export-using-import` does \- not have export `{name}`"+ not have export `{name}`")})};@@ -648,7 +648,7 @@ pub mod exports {.ok_or_else(|| {wasmtime::format_err!("instance export `foo:foo/export-using-export1` does \- not have export `{name}`"+ not have export `{name}`")})};@@ -744,7 +744,7 @@ pub mod exports {.ok_or_else(|| {wasmtime::format_err!("instance export `foo:foo/export-using-export2` does \- not have export `{name}`"+ not have export `{name}`")})};diff --git a/crates/component-macro/tests/expanded/resources-import_concurrent.rs b/crates/component-macro/tests/expanded/resources-import_concurrent.rsindex 1ced3553c9..ea6347bc06 100644--- a/crates/component-macro/tests/expanded/resources-import_concurrent.rs+++ b/crates/component-macro/tests/expanded/resources-import_concurrent.rs@@ -1138,7 +1138,7 @@ pub mod exports {.ok_or_else(|| {wasmtime::format_err!("instance export `foo:foo/uses-resource-transitively` does \- not have export `{name}`"+ not have export `{name}`")})};diff --git a/crates/environ/src/lib.rs b/crates/environ/src/lib.rsindex 0306bdd1f7..1fabe90141 100644--- a/crates/environ/src/lib.rs+++ b/crates/environ/src/lib.rs@@ -99,5 +99,10 @@ impl<T> ToWasmtimeResult<T> for anyhow::Result<T> {}}+// Only for use with `bindgen!`-generated code.+#[doc(hidden)]+#[cfg(feature = "anyhow")]+pub use anyhow;+/// Version number of this crate.pub const VERSION: &str = env!("CARGO_PKG_VERSION");diff --git a/crates/wasmtime/src/lib.rs b/crates/wasmtime/src/lib.rsindex a6761be4c5..20dea9a1f6 100644--- a/crates/wasmtime/src/lib.rs+++ b/crates/wasmtime/src/lib.rs@@ -414,6 +414,11 @@ pub use wasmtime_environ::error;#[cfg(feature = "anyhow")]pub use wasmtime_environ::ToWasmtimeResult;+// Only for use in `bindgen!`-generated code.+#[doc(hidden)]+#[cfg(feature = "anyhow")]+pub use wasmtime_environ::anyhow;+pub use self::error::{Error, Result, bail, ensure, format_err};/// A re-exported instance of Wasmtime's `wasmparser` dependency.diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rsindex 2cd188cb8f..5fd35d7d9d 100644--- a/crates/wit-bindgen/src/lib.rs+++ b/crates/wit-bindgen/src/lib.rs@@ -2669,6 +2669,15 @@ impl<'a> InterfaceGenerator<'a> {}}+ fn push_wasmtime_or_anyhow_result(&mut self) {+ let wt = self.generator.wasmtime_path();+ uwrite!(self.src, "{wt}::");+ if self.generator.opts.anyhow {+ self.push_str("anyhow::");+ }+ self.push_str("Result");+ }+fn generate_function_result(&mut self, func: &Function, flags: FunctionFlags) {if !flags.contains(FunctionFlags::TRAPPABLE) {self.print_result_ty(func.result, TypeMode::Owned);@@ -2688,8 +2697,8 @@ impl<'a> InterfaceGenerator<'a> {} else {// All other functions get their return values wrapped in an wasmtime::Result.// Returning the anyhow::Error case can be used to trap.- let wt = self.generator.wasmtime_path();- uwrite!(self.src, "{wt}::Result<");+ self.push_wasmtime_or_anyhow_result();+ self.push_str("<");self.print_result_ty(func.result, TypeMode::Owned);self.push_str(">");}@@ -2978,17 +2987,21 @@ impl<'a> InterfaceGenerator<'a> {self.src,"fn drop<T>(accessor: &{wt}::component::Accessor<T, Self>, rep: {wt}::component::Resource<{camel}>)- -> impl ::core::future::Future<Output = {wt}::Result<()>> + Send where Self: Sized;+ -> impl ::core::future::Future<Output =");+ self.push_wasmtime_or_anyhow_result();+ self.push_str("<()>> + Send where Self: Sized;");} else {uwrite!(self.src,"fn drop<T>(accessor: {wt}::component::Access<T, Self>, rep: {wt}::component::Resource<{camel}>)- -> {wt}::Result<()>;+ ->");+ self.push_wasmtime_or_anyhow_result();+ self.push_str("<()>;");}extra_with_store_function = true;@@ -3043,7 +3056,8 @@ fn drop<T>(accessor: {wt}::component::Access<T, Self>, rep: {wt}::component::Resif flags.contains(FunctionFlags::ASYNC) {uwrite!(self.src, "impl ::core::future::Future<Output =");}- uwrite!(self.src, "{wt}::Result<()>");+ self.push_wasmtime_or_anyhow_result();+ self.push_str("<()>");if flags.contains(FunctionFlags::ASYNC) {uwrite!(self.src, "> + Send");}@@ -3054,12 +3068,14 @@ fn drop<T>(accessor: {wt}::component::Access<T, Self>, rep: {wt}::component::Reslet custom_name = &self.generator.trappable_errors[id];let snake = name.to_snake_case();let camel = name.to_upper_camel_case();- uwriteln!(+ uwrite!(self.src,"-fn convert_{snake}(&mut self, err: {root}{custom_name}) -> {wt}::Result<{camel}>;+fn convert_{snake}(&mut self, err: {root}{custom_name}) ->");+ self.push_wasmtime_or_anyhow_result();+ uwrite!(self.src, "<{camel}>;");}}}@@ -3113,10 +3129,14 @@ fn convert_{snake}(&mut self, err: {root}{custom_name}) -> {wt}::Result<{camel}>self.src.push_str("async ");await_ = ".await";}+ uwrite!(+ self.src,+ "fn drop(&mut self, rep: {wt}::component::Resource<{camel}>) -> ",+ );+ self.push_wasmtime_or_anyhow_result();uwriteln!(self.src,- "-fn drop(&mut self, rep: {wt}::component::Resource<{camel}>) -> {wt}::Result<()> {{+ "<()> {{{trait_name}::drop(*self, rep){await_}}}",@@ -3127,10 +3147,14 @@ fn drop(&mut self, rep: {wt}::component::Resource<{camel}>) -> {wt}::Result<()>let custom_name = &self.generator.trappable_errors[id];let snake = name.to_snake_case();let camel = name.to_upper_camel_case();+ uwrite!(+ self.src,+ "fn convert_{snake}(&mut self, err: {root}{custom_name}) -> ",+ );+ self.push_wasmtime_or_anyhow_result();uwriteln!(self.src,- "-fn convert_{snake}(&mut self, err: {root}{custom_name}) -> {wt}::Result<{camel}> {{+ "<{camel}> {{{trait_name}::convert_{snake}(*self, err)}}",