@@ -197,18 +197,23 @@ impl ToTokens for ast::Struct {
197
197
impl wasm_bindgen:: __rt:: core:: convert:: From <#name> for
198
198
wasm_bindgen:: JsValue
199
199
{
200
- #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
201
200
fn from( value: #name) -> Self {
202
201
let ptr = wasm_bindgen:: convert:: IntoWasmAbi :: into_abi(
203
202
value,
204
203
unsafe { & mut wasm_bindgen:: convert:: GlobalStack :: new( ) } ,
205
204
) ;
206
205
207
206
#[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
207
+ #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
208
208
extern "C" {
209
209
fn #new_fn( ptr: u32 ) -> u32 ;
210
210
}
211
211
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
+
212
217
unsafe {
213
218
<wasm_bindgen:: JsValue as wasm_bindgen:: convert:: FromWasmAbi >
214
219
:: from_abi(
@@ -217,11 +222,6 @@ impl ToTokens for ast::Struct {
217
222
)
218
223
}
219
224
}
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
- }
225
225
}
226
226
227
227
#[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
@@ -712,24 +712,22 @@ impl ToTokens for ast::ImportType {
712
712
}
713
713
714
714
impl JsCast for #rust_name {
715
- #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
716
715
fn instanceof( val: & JsValue ) -> bool {
717
716
#[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
717
+ #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
718
718
extern "C" {
719
719
fn #instanceof_shim( val: u32 ) -> u32 ;
720
720
}
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
+ }
721
725
unsafe {
722
726
let idx = val. into_abi( & mut wasm_bindgen:: convert:: GlobalStack :: new( ) ) ;
723
727
#instanceof_shim( idx) != 0
724
728
}
725
729
}
726
730
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
-
733
731
#is_type_of
734
732
735
733
#[ inline]
@@ -998,6 +996,7 @@ impl TryToTokens for ast::ImportFunction {
998
996
let import_name = & self . shim ;
999
997
let attrs = & self . function . rust_attrs ;
1000
998
let arguments = & arguments;
999
+ let abi_arguments = & abi_arguments;
1001
1000
1002
1001
let doc_comment = match & self . doc_comment {
1003
1002
None => "" ,
@@ -1009,17 +1008,45 @@ impl TryToTokens for ast::ImportFunction {
1009
1008
quote ! ( )
1010
1009
} ;
1011
1010
1011
+ // Route any errors pointing to this imported function to the identifier
1012
+ // of the function we're imported from so we at least know what function
1013
+ // is causing issues.
1014
+ //
1015
+ // Note that this is where type errors like "doesn't implement
1016
+ // FromWasmAbi" or "doesn't implement IntoWasmAbi" currently get routed.
1017
+ // I suspect that's because they show up in the signature via trait
1018
+ // projections as types of arguments, and all that needs to typecheck
1019
+ // before the body can be typechecked. Due to rust-lang/rust#60980 (and
1020
+ // probably related issues) we can't really get a precise span.
1021
+ //
1022
+ // Ideally what we want is to point errors for particular types back to
1023
+ // the specific argument/type that generated the error, but it looks
1024
+ // like rustc itself doesn't do great in that regard so let's just do
1025
+ // the best we can in the meantime.
1026
+ let extern_fn = respan (
1027
+ quote ! {
1028
+ #[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
1029
+ #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
1030
+ extern "C" {
1031
+ fn #import_name( #( #abi_arguments) , * ) -> #abi_ret;
1032
+ }
1033
+ #[ cfg( not( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ) ]
1034
+ unsafe fn #import_name( #( #abi_arguments) , * ) -> #abi_ret {
1035
+ panic!( "cannot call wasm-bindgen imported functions on \
1036
+ non-wasm targets") ;
1037
+ }
1038
+ } ,
1039
+ & self . rust_name ,
1040
+ ) ;
1041
+
1012
1042
let invocation = quote ! {
1013
1043
#( #attrs) *
1014
1044
#[ allow( bad_style) ]
1015
- #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
1016
1045
#[ doc = #doc_comment]
1017
1046
#[ allow( clippy:: all) ]
1018
1047
#vis fn #rust_name( #me #( #arguments) , * ) #ret {
1019
- #[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
1020
- extern "C" {
1021
- fn #import_name( #( #abi_arguments) , * ) -> #abi_ret;
1022
- }
1048
+ #extern_fn
1049
+
1023
1050
unsafe {
1024
1051
#exn_data
1025
1052
let #ret_ident = {
@@ -1031,17 +1058,6 @@ impl TryToTokens for ast::ImportFunction {
1031
1058
#convert_ret
1032
1059
}
1033
1060
}
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
-
1045
1061
} ;
1046
1062
1047
1063
if let Some ( class) = class_ty {
@@ -1164,12 +1180,17 @@ impl ToTokens for ast::ImportStatic {
1164
1180
#[ allow( bad_style) ]
1165
1181
#[ allow( clippy:: all) ]
1166
1182
#vis static #name: wasm_bindgen:: JsStatic <#ty> = {
1167
- #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
1168
1183
fn init( ) -> #ty {
1169
1184
#[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
1185
+ #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
1170
1186
extern "C" {
1171
1187
fn #shim_name( ) -> <#ty as wasm_bindgen:: convert:: FromWasmAbi >:: Abi ;
1172
1188
}
1189
+ #[ cfg( not( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ) ]
1190
+ unsafe fn #shim_name( ) -> <#ty as wasm_bindgen:: convert:: FromWasmAbi >:: Abi {
1191
+ panic!( "cannot access imported statics on non-wasm targets" )
1192
+ }
1193
+
1173
1194
unsafe {
1174
1195
<#ty as wasm_bindgen:: convert:: FromWasmAbi >:: from_abi(
1175
1196
#shim_name( ) ,
@@ -1178,10 +1199,6 @@ impl ToTokens for ast::ImportStatic {
1178
1199
1179
1200
}
1180
1201
}
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
- }
1185
1202
thread_local!( static _VAL: #ty = init( ) ; ) ;
1186
1203
wasm_bindgen:: JsStatic {
1187
1204
__inner: & _VAL,
@@ -1449,6 +1466,8 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
1449
1466
}
1450
1467
}
1451
1468
1469
+ /// Converts `span` into a stream of tokens, and attempts to ensure that `input`
1470
+ /// has all the appropriate span information so errors in it point to `span`.
1452
1471
fn respan ( input : TokenStream , span : & dyn ToTokens ) -> TokenStream {
1453
1472
let mut first_span = Span :: call_site ( ) ;
1454
1473
let mut last_span = Span :: call_site ( ) ;
0 commit comments