Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for non-copy struct fields #1430

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,21 @@ impl ToTokens for ast::StructField {
let ty = &self.ty;
let getter = &self.getter;
let setter = &self.setter;

let assert_copy = quote! { assert_copy::<#ty>() };
let assert_copy = respan(assert_copy, ty);
(quote! {
#[no_mangle]
#[doc(hidden)]
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[allow(clippy::all)]
#[cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), no_mangle)]
pub unsafe extern "C" fn #getter(js: u32)
-> <#ty as wasm_bindgen::convert::IntoWasmAbi>::Abi
{
use wasm_bindgen::__rt::{WasmRefCell, assert_not_null};
use wasm_bindgen::convert::{GlobalStack, IntoWasmAbi};

fn assert_copy<T: Copy>(){}
assert_copy::<#ty>();
#assert_copy;

let js = js as *mut WasmRefCell<#struct_name>;
assert_not_null(js);
Expand Down Expand Up @@ -714,7 +716,8 @@ impl ToTokens for ast::ImportType {

()
};
}).to_tokens(tokens);
})
.to_tokens(tokens);

let deref_target = match self.extends.first() {
Some(target) => quote! { #target },
Expand Down Expand Up @@ -1430,3 +1433,31 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
.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 changes: 10 additions & 0 deletions crates/macro/ui-tests/pub-not-copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![crate_type = "rlib"]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct A {
pub field: String,
}
15 changes: 15 additions & 0 deletions crates/macro/ui-tests/pub-not-copy.stderr
Original file line number Diff line number Diff line change
@@ -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`.