Skip to content

Commit a02d210

Browse files
committed
Catch more errors on non-wasm32 platforms
This commit tweaks the codegen for imported functions and such (anything that relies on some imported intrinsic or function filled in by the CLI) to share as much code as possible on non-wasm32 platforms. This should help us catch more errors before compiling to wasm and also just make it easier to write UI tests! For example a UI test previously couldn't be written for #1528 but now it can be, and one is include (although the error message is quite bad).
1 parent f23b867 commit a02d210

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

crates/backend/src/codegen.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,23 @@ impl ToTokens for ast::Struct {
197197
impl wasm_bindgen::__rt::core::convert::From<#name> for
198198
wasm_bindgen::JsValue
199199
{
200-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
201200
fn from(value: #name) -> Self {
202201
let ptr = wasm_bindgen::convert::IntoWasmAbi::into_abi(
203202
value,
204203
unsafe { &mut wasm_bindgen::convert::GlobalStack::new() },
205204
);
206205

207206
#[link(wasm_import_module = "__wbindgen_placeholder__")]
207+
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
208208
extern "C" {
209209
fn #new_fn(ptr: u32) -> u32;
210210
}
211211

212+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
213+
unsafe fn #new_fn(ptr: u32) -> u32 {
214+
panic!("cannot convert to JsValue outside of the wasm target")
215+
}
216+
212217
unsafe {
213218
<wasm_bindgen::JsValue as wasm_bindgen::convert::FromWasmAbi>
214219
::from_abi(
@@ -217,11 +222,6 @@ impl ToTokens for ast::Struct {
217222
)
218223
}
219224
}
220-
221-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
222-
fn from(_value: #name) -> Self {
223-
panic!("cannot convert to JsValue outside of the wasm target")
224-
}
225225
}
226226

227227
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
@@ -712,24 +712,22 @@ impl ToTokens for ast::ImportType {
712712
}
713713

714714
impl JsCast for #rust_name {
715-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
716715
fn instanceof(val: &JsValue) -> bool {
717716
#[link(wasm_import_module = "__wbindgen_placeholder__")]
717+
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
718718
extern "C" {
719719
fn #instanceof_shim(val: u32) -> u32;
720720
}
721+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
722+
unsafe fn #instanceof_shim(val: u32) -> u32 {
723+
panic!("cannot check instanceof on non-wasm targets");
724+
}
721725
unsafe {
722726
let idx = val.into_abi(&mut wasm_bindgen::convert::GlobalStack::new());
723727
#instanceof_shim(idx) != 0
724728
}
725729
}
726730

727-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
728-
fn instanceof(val: &JsValue) -> bool {
729-
drop(val);
730-
panic!("cannot check instanceof on non-wasm targets");
731-
}
732-
733731
#is_type_of
734732

735733
#[inline]
@@ -998,6 +996,7 @@ impl TryToTokens for ast::ImportFunction {
998996
let import_name = &self.shim;
999997
let attrs = &self.function.rust_attrs;
1000998
let arguments = &arguments;
999+
let abi_arguments = &abi_arguments;
10011000

10021001
let doc_comment = match &self.doc_comment {
10031002
None => "",
@@ -1012,14 +1011,20 @@ impl TryToTokens for ast::ImportFunction {
10121011
let invocation = quote! {
10131012
#(#attrs)*
10141013
#[allow(bad_style)]
1015-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
10161014
#[doc = #doc_comment]
10171015
#[allow(clippy::all)]
10181016
#vis fn #rust_name(#me #(#arguments),*) #ret {
10191017
#[link(wasm_import_module = "__wbindgen_placeholder__")]
1018+
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
10201019
extern "C" {
10211020
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
10221021
}
1022+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
1023+
unsafe fn #import_name(#(#abi_arguments),*) -> #abi_ret {
1024+
panic!("cannot call wasm-bindgen imported functions on \
1025+
non-wasm targets");
1026+
}
1027+
10231028
unsafe {
10241029
#exn_data
10251030
let #ret_ident = {
@@ -1031,17 +1036,6 @@ impl TryToTokens for ast::ImportFunction {
10311036
#convert_ret
10321037
}
10331038
}
1034-
1035-
#(#attrs)*
1036-
#[allow(bad_style, unused_variables)]
1037-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
1038-
#[doc = #doc_comment]
1039-
#[allow(clippy::all)]
1040-
#vis fn #rust_name(#me #(#arguments),*) #ret {
1041-
panic!("cannot call wasm-bindgen imported functions on \
1042-
non-wasm targets");
1043-
}
1044-
10451039
};
10461040

10471041
if let Some(class) = class_ty {
@@ -1164,12 +1158,17 @@ impl ToTokens for ast::ImportStatic {
11641158
#[allow(bad_style)]
11651159
#[allow(clippy::all)]
11661160
#vis static #name: wasm_bindgen::JsStatic<#ty> = {
1167-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
11681161
fn init() -> #ty {
11691162
#[link(wasm_import_module = "__wbindgen_placeholder__")]
1163+
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
11701164
extern "C" {
11711165
fn #shim_name() -> <#ty as wasm_bindgen::convert::FromWasmAbi>::Abi;
11721166
}
1167+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
1168+
unsafe fn #shim_name() -> <#ty as wasm_bindgen::convert::FromWasmAbi>::Abi {
1169+
panic!("cannot access imported statics on non-wasm targets")
1170+
}
1171+
11731172
unsafe {
11741173
<#ty as wasm_bindgen::convert::FromWasmAbi>::from_abi(
11751174
#shim_name(),
@@ -1178,10 +1177,6 @@ impl ToTokens for ast::ImportStatic {
11781177

11791178
}
11801179
}
1181-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
1182-
fn init() -> #ty {
1183-
panic!("cannot access imported statics on non-wasm targets")
1184-
}
11851180
thread_local!(static _VAL: #ty = init(););
11861181
wasm_bindgen::JsStatic {
11871182
__inner: &_VAL,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
extern "C" {
5+
#[wasm_bindgen]
6+
pub fn foo() -> Result<JsValue, JsValue>;
7+
}
8+
9+
fn main() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error[E0277]: the trait bound `std::result::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>: wasm_bindgen::convert::traits::FromWasmAbi` is not satisfied
2+
--> $DIR/missing-catch.rs:3:1
3+
|
4+
3 | #[wasm_bindgen]
5+
| ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::FromWasmAbi` is not implemented for `std::result::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>`
6+
7+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)