mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-01 18:01:06 +00:00
Improve error message for non-copy struct fields
Make sure the error message points to the type in question instead of to the `#[wasm_bindgen]` macro which can be overly confusing!
This commit is contained in:
parent
16745edf3f
commit
e4400ac8b4
@ -291,11 +291,13 @@ impl ToTokens for ast::StructField {
|
|||||||
let ty = &self.ty;
|
let ty = &self.ty;
|
||||||
let getter = &self.getter;
|
let getter = &self.getter;
|
||||||
let setter = &self.setter;
|
let setter = &self.setter;
|
||||||
|
|
||||||
|
let assert_copy = quote! { assert_copy::<#ty>() };
|
||||||
|
let assert_copy = respan(assert_copy, ty);
|
||||||
(quote! {
|
(quote! {
|
||||||
#[no_mangle]
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
|
#[cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), no_mangle)]
|
||||||
pub unsafe extern "C" fn #getter(js: u32)
|
pub unsafe extern "C" fn #getter(js: u32)
|
||||||
-> <#ty as wasm_bindgen::convert::IntoWasmAbi>::Abi
|
-> <#ty as wasm_bindgen::convert::IntoWasmAbi>::Abi
|
||||||
{
|
{
|
||||||
@ -303,7 +305,7 @@ impl ToTokens for ast::StructField {
|
|||||||
use wasm_bindgen::convert::{GlobalStack, IntoWasmAbi};
|
use wasm_bindgen::convert::{GlobalStack, IntoWasmAbi};
|
||||||
|
|
||||||
fn assert_copy<T: Copy>(){}
|
fn assert_copy<T: Copy>(){}
|
||||||
assert_copy::<#ty>();
|
#assert_copy;
|
||||||
|
|
||||||
let js = js as *mut WasmRefCell<#struct_name>;
|
let js = js as *mut WasmRefCell<#struct_name>;
|
||||||
assert_not_null(js);
|
assert_not_null(js);
|
||||||
@ -714,7 +716,8 @@ impl ToTokens for ast::ImportType {
|
|||||||
|
|
||||||
()
|
()
|
||||||
};
|
};
|
||||||
}).to_tokens(tokens);
|
})
|
||||||
|
.to_tokens(tokens);
|
||||||
|
|
||||||
let deref_target = match self.extends.first() {
|
let deref_target = match self.extends.first() {
|
||||||
Some(target) => quote! { #target },
|
Some(target) => quote! { #target },
|
||||||
@ -1430,3 +1433,31 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
|
|||||||
.to_tokens(tokens);
|
.to_tokens(tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn respan(
|
||||||
|
input: TokenStream,
|
||||||
|
span: &dyn ToTokens,
|
||||||
|
) -> TokenStream {
|
||||||
|
let mut first_span = Span::call_site();
|
||||||
|
let mut last_span = Span::call_site();
|
||||||
|
let mut spans = TokenStream::new();
|
||||||
|
span.to_tokens(&mut spans);
|
||||||
|
|
||||||
|
for (i, token) in spans.into_iter().enumerate() {
|
||||||
|
if i == 0 {
|
||||||
|
first_span = token.span();
|
||||||
|
}
|
||||||
|
last_span = token.span();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_tokens = Vec::new();
|
||||||
|
for (i, mut token) in input.into_iter().enumerate() {
|
||||||
|
if i == 0 {
|
||||||
|
token.set_span(first_span);
|
||||||
|
} else {
|
||||||
|
token.set_span(last_span);
|
||||||
|
}
|
||||||
|
new_tokens.push(token);
|
||||||
|
}
|
||||||
|
new_tokens.into_iter().collect()
|
||||||
|
}
|
||||||
|
10
crates/macro/ui-tests/pub-not-copy.rs
Normal file
10
crates/macro/ui-tests/pub-not-copy.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#![crate_type = "rlib"]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct A {
|
||||||
|
pub field: String,
|
||||||
|
}
|
15
crates/macro/ui-tests/pub-not-copy.stderr
Normal file
15
crates/macro/ui-tests/pub-not-copy.stderr
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
|
||||||
|
--> $DIR/pub-not-copy.rs:9:16
|
||||||
|
|
|
||||||
|
9 | pub field: String,
|
||||||
|
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
|
||||||
|
|
|
||||||
|
note: required by `__wbg_get_a_field::assert_copy`
|
||||||
|
--> $DIR/pub-not-copy.rs:7:1
|
||||||
|
|
|
||||||
|
7 | #[wasm_bindgen]
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
Loading…
x
Reference in New Issue
Block a user