mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-28 04:22:38 +03:00
Convert some more macro panics to diagnostics (#611)
This should hopefully be the last of the manually written diagnostics!
This commit is contained in:
parent
71dbd08c00
commit
4a78769349
@ -25,8 +25,11 @@ pub trait TryToTokens {
|
|||||||
impl TryToTokens for ast::Program {
|
impl TryToTokens for ast::Program {
|
||||||
// Generate wrappers for all the items that we've found
|
// Generate wrappers for all the items that we've found
|
||||||
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
|
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
|
||||||
|
let mut errors = Vec::new();
|
||||||
for export in self.exports.iter() {
|
for export in self.exports.iter() {
|
||||||
export.try_to_tokens(tokens)?;
|
if let Err(e) = export.try_to_tokens(tokens) {
|
||||||
|
errors.push(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for s in self.structs.iter() {
|
for s in self.structs.iter() {
|
||||||
s.to_tokens(tokens);
|
s.to_tokens(tokens);
|
||||||
@ -42,13 +45,21 @@ impl TryToTokens for ast::Program {
|
|||||||
|
|
||||||
if let Some(ns) = &i.js_namespace {
|
if let Some(ns) = &i.js_namespace {
|
||||||
if types.contains(ns) && i.kind.fits_on_impl() {
|
if types.contains(ns) && i.kind.fits_on_impl() {
|
||||||
let kind = i.kind.try_to_token_stream()?;
|
let kind = match i.kind.try_to_token_stream() {
|
||||||
|
Ok(kind) => kind,
|
||||||
|
Err(e) => {
|
||||||
|
errors.push(e);
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
};
|
||||||
(quote! { impl #ns { #kind } }).to_tokens(tokens);
|
(quote! { impl #ns { #kind } }).to_tokens(tokens);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i.kind.try_to_tokens(tokens)?;
|
if let Err(e) = i.kind.try_to_tokens(tokens) {
|
||||||
|
errors.push(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for e in self.enums.iter() {
|
for e in self.enums.iter() {
|
||||||
e.to_tokens(tokens);
|
e.to_tokens(tokens);
|
||||||
@ -60,6 +71,8 @@ impl TryToTokens for ast::Program {
|
|||||||
c.to_tokens(tokens);
|
c.to_tokens(tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Diagnostic::from_vec(errors)?;
|
||||||
|
|
||||||
// Generate a static which will eventually be what lives in a custom section
|
// Generate a static which will eventually be what lives in a custom section
|
||||||
// of the wasm executable. For now it's just a plain old static, but we'll
|
// of the wasm executable. For now it's just a plain old static, but we'll
|
||||||
// eventually have it actually in its own section.
|
// eventually have it actually in its own section.
|
||||||
@ -405,7 +418,12 @@ impl TryToTokens for ast::Export {
|
|||||||
let ret_ty;
|
let ret_ty;
|
||||||
let convert_ret;
|
let convert_ret;
|
||||||
match &self.function.ret {
|
match &self.function.ret {
|
||||||
Some(syn::Type::Reference(_)) => panic!("can't return a borrowed ref"),
|
Some(syn::Type::Reference(_)) => {
|
||||||
|
bail_span!(
|
||||||
|
self.function.ret,
|
||||||
|
"cannot return a borrowed ref with #[wasm_bindgen]",
|
||||||
|
)
|
||||||
|
}
|
||||||
Some(ty) => {
|
Some(ty) => {
|
||||||
ret_ty = quote! {
|
ret_ty = quote! {
|
||||||
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
|
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
|
||||||
@ -722,7 +740,12 @@ impl TryToTokens for ast::ImportFunction {
|
|||||||
..
|
..
|
||||||
}) => ident.clone(),
|
}) => ident.clone(),
|
||||||
syn::Pat::Wild(_) => syn::Ident::new(&format!("__genarg_{}", i), Span::call_site()),
|
syn::Pat::Wild(_) => syn::Ident::new(&format!("__genarg_{}", i), Span::call_site()),
|
||||||
_ => panic!("unsupported pattern in foreign function"),
|
_ => {
|
||||||
|
bail_span!(
|
||||||
|
pat,
|
||||||
|
"unsupported pattern in #[wasm_bindgen] imported function",
|
||||||
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
abi_argument_names.push(name.clone());
|
abi_argument_names.push(name.clone());
|
||||||
@ -744,7 +767,7 @@ impl TryToTokens for ast::ImportFunction {
|
|||||||
let mut convert_ret;
|
let mut convert_ret;
|
||||||
match &self.js_ret {
|
match &self.js_ret {
|
||||||
Some(syn::Type::Reference(_)) => {
|
Some(syn::Type::Reference(_)) => {
|
||||||
panic!("cannot return references in imports yet");
|
bail_span!(self.js_ret, "cannot return references in #[wasm_bindgen] imports yet");
|
||||||
}
|
}
|
||||||
Some(ref ty) => {
|
Some(ref ty) => {
|
||||||
abi_ret = quote! {
|
abi_ret = quote! {
|
||||||
|
@ -4,7 +4,7 @@ use quote::ToTokens;
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! err_span {
|
macro_rules! err_span {
|
||||||
($span:expr, $($msg:tt)*) => (
|
($span:expr, $($msg:tt)*) => (
|
||||||
::backend::Diagnostic::span_error(&$span, format!($($msg)*))
|
$crate::Diagnostic::span_error(&$span, format!($($msg)*))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +17,10 @@ extern crate wasm_bindgen_shared as shared;
|
|||||||
pub use codegen::TryToTokens;
|
pub use codegen::TryToTokens;
|
||||||
pub use error::Diagnostic;
|
pub use error::Diagnostic;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod error;
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
mod codegen;
|
mod codegen;
|
||||||
pub mod defined;
|
pub mod defined;
|
||||||
mod error;
|
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
15
crates/macro/ui-tests/bad-signatures.rs
Normal file
15
crates/macro/ui-tests/bad-signatures.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#![feature(use_extern_macros)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn foo() -> &u32 {}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
extern "C" {
|
||||||
|
fn foo(Foo(x): Foo);
|
||||||
|
|
||||||
|
fn foo() -> &u32;
|
||||||
|
}
|
20
crates/macro/ui-tests/bad-signatures.stderr
Normal file
20
crates/macro/ui-tests/bad-signatures.stderr
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
error: cannot return a borrowed ref with #[wasm_bindgen]
|
||||||
|
--> $DIR/bad-signatures.rs:8:17
|
||||||
|
|
|
||||||
|
8 | pub fn foo() -> &u32 {}
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: unsupported pattern in #[wasm_bindgen] imported function
|
||||||
|
--> $DIR/bad-signatures.rs:12:12
|
||||||
|
|
|
||||||
|
12 | fn foo(Foo(x): Foo);
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: cannot return references in #[wasm_bindgen] imports yet
|
||||||
|
--> $DIR/bad-signatures.rs:14:17
|
||||||
|
|
|
||||||
|
14 | fn foo() -> &u32;
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
Loading…
Reference in New Issue
Block a user