Convert some more macro panics to diagnostics (#611)

This should hopefully be the last of the manually written diagnostics!
This commit is contained in:
Alex Crichton 2018-08-02 11:12:50 -05:00 committed by GitHub
parent 71dbd08c00
commit 4a78769349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 8 deletions

View File

@ -25,8 +25,11 @@ pub trait TryToTokens {
impl TryToTokens for ast::Program {
// Generate wrappers for all the items that we've found
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
let mut errors = Vec::new();
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() {
s.to_tokens(tokens);
@ -42,13 +45,21 @@ impl TryToTokens for ast::Program {
if let Some(ns) = &i.js_namespace {
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);
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() {
e.to_tokens(tokens);
@ -60,6 +71,8 @@ impl TryToTokens for ast::Program {
c.to_tokens(tokens);
}
Diagnostic::from_vec(errors)?;
// 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
// eventually have it actually in its own section.
@ -405,7 +418,12 @@ impl TryToTokens for ast::Export {
let ret_ty;
let convert_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) => {
ret_ty = quote! {
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
@ -722,7 +740,12 @@ impl TryToTokens for ast::ImportFunction {
..
}) => ident.clone(),
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());
@ -744,7 +767,7 @@ impl TryToTokens for ast::ImportFunction {
let mut convert_ret;
match &self.js_ret {
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) => {
abi_ret = quote! {

View File

@ -4,7 +4,7 @@ use quote::ToTokens;
#[macro_export]
macro_rules! err_span {
($span:expr, $($msg:tt)*) => (
::backend::Diagnostic::span_error(&$span, format!($($msg)*))
$crate::Diagnostic::span_error(&$span, format!($($msg)*))
)
}

View File

@ -17,8 +17,10 @@ extern crate wasm_bindgen_shared as shared;
pub use codegen::TryToTokens;
pub use error::Diagnostic;
#[macro_use]
mod error;
pub mod ast;
mod codegen;
pub mod defined;
mod error;
pub mod util;

View 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;
}

View 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