Skip to content

Commit 4a78769

Browse files
authored
Convert some more macro panics to diagnostics (#611)
This should hopefully be the last of the manually written diagnostics!
1 parent 71dbd08 commit 4a78769

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

crates/backend/src/codegen.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ pub trait TryToTokens {
2525
impl TryToTokens for ast::Program {
2626
// Generate wrappers for all the items that we've found
2727
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
28+
let mut errors = Vec::new();
2829
for export in self.exports.iter() {
29-
export.try_to_tokens(tokens)?;
30+
if let Err(e) = export.try_to_tokens(tokens) {
31+
errors.push(e);
32+
}
3033
}
3134
for s in self.structs.iter() {
3235
s.to_tokens(tokens);
@@ -42,13 +45,21 @@ impl TryToTokens for ast::Program {
4245

4346
if let Some(ns) = &i.js_namespace {
4447
if types.contains(ns) && i.kind.fits_on_impl() {
45-
let kind = i.kind.try_to_token_stream()?;
48+
let kind = match i.kind.try_to_token_stream() {
49+
Ok(kind) => kind,
50+
Err(e) => {
51+
errors.push(e);
52+
continue
53+
}
54+
};
4655
(quote! { impl #ns { #kind } }).to_tokens(tokens);
4756
continue;
4857
}
4958
}
5059

51-
i.kind.try_to_tokens(tokens)?;
60+
if let Err(e) = i.kind.try_to_tokens(tokens) {
61+
errors.push(e);
62+
}
5263
}
5364
for e in self.enums.iter() {
5465
e.to_tokens(tokens);
@@ -60,6 +71,8 @@ impl TryToTokens for ast::Program {
6071
c.to_tokens(tokens);
6172
}
6273

74+
Diagnostic::from_vec(errors)?;
75+
6376
// Generate a static which will eventually be what lives in a custom section
6477
// of the wasm executable. For now it's just a plain old static, but we'll
6578
// eventually have it actually in its own section.
@@ -405,7 +418,12 @@ impl TryToTokens for ast::Export {
405418
let ret_ty;
406419
let convert_ret;
407420
match &self.function.ret {
408-
Some(syn::Type::Reference(_)) => panic!("can't return a borrowed ref"),
421+
Some(syn::Type::Reference(_)) => {
422+
bail_span!(
423+
self.function.ret,
424+
"cannot return a borrowed ref with #[wasm_bindgen]",
425+
)
426+
}
409427
Some(ty) => {
410428
ret_ty = quote! {
411429
-> <#ty as ::wasm_bindgen::convert::IntoWasmAbi>::Abi
@@ -722,7 +740,12 @@ impl TryToTokens for ast::ImportFunction {
722740
..
723741
}) => ident.clone(),
724742
syn::Pat::Wild(_) => syn::Ident::new(&format!("__genarg_{}", i), Span::call_site()),
725-
_ => panic!("unsupported pattern in foreign function"),
743+
_ => {
744+
bail_span!(
745+
pat,
746+
"unsupported pattern in #[wasm_bindgen] imported function",
747+
)
748+
}
726749
};
727750

728751
abi_argument_names.push(name.clone());
@@ -744,7 +767,7 @@ impl TryToTokens for ast::ImportFunction {
744767
let mut convert_ret;
745768
match &self.js_ret {
746769
Some(syn::Type::Reference(_)) => {
747-
panic!("cannot return references in imports yet");
770+
bail_span!(self.js_ret, "cannot return references in #[wasm_bindgen] imports yet");
748771
}
749772
Some(ref ty) => {
750773
abi_ret = quote! {

crates/backend/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use quote::ToTokens;
44
#[macro_export]
55
macro_rules! err_span {
66
($span:expr, $($msg:tt)*) => (
7-
::backend::Diagnostic::span_error(&$span, format!($($msg)*))
7+
$crate::Diagnostic::span_error(&$span, format!($($msg)*))
88
)
99
}
1010

crates/backend/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ extern crate wasm_bindgen_shared as shared;
1717
pub use codegen::TryToTokens;
1818
pub use error::Diagnostic;
1919

20+
#[macro_use]
21+
mod error;
22+
2023
pub mod ast;
2124
mod codegen;
2225
pub mod defined;
23-
mod error;
2426
pub mod util;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(use_extern_macros)]
2+
3+
extern crate wasm_bindgen;
4+
5+
use wasm_bindgen::prelude::*;
6+
7+
#[wasm_bindgen]
8+
pub fn foo() -> &u32 {}
9+
10+
#[wasm_bindgen]
11+
extern "C" {
12+
fn foo(Foo(x): Foo);
13+
14+
fn foo() -> &u32;
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: cannot return a borrowed ref with #[wasm_bindgen]
2+
--> $DIR/bad-signatures.rs:8:17
3+
|
4+
8 | pub fn foo() -> &u32 {}
5+
| ^^^^
6+
7+
error: unsupported pattern in #[wasm_bindgen] imported function
8+
--> $DIR/bad-signatures.rs:12:12
9+
|
10+
12 | fn foo(Foo(x): Foo);
11+
| ^^^^^^
12+
13+
error: cannot return references in #[wasm_bindgen] imports yet
14+
--> $DIR/bad-signatures.rs:14:17
15+
|
16+
14 | fn foo() -> &u32;
17+
| ^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)