Skip to content

Commit f4c1c64

Browse files
authored
Merge pull request #1055 from alexcrichton/strict
Assert all attributes are used by default
2 parents e3b6286 + c8a3521 commit f4c1c64

File tree

11 files changed

+351
-357
lines changed

11 files changed

+351
-357
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ std = []
3030
serde-serialize = ["serde", "serde_json", "std"]
3131
nightly = []
3232

33+
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
34+
# all unused attributes
35+
strict-macro = ["wasm-bindgen-macro/strict-macro"]
36+
3337
# This is only for debugging wasm-bindgen! No stability guarantees, so enable
3438
# this at your own peril!
3539
xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_generated_code"]

crates/backend/src/error.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syn::parse::Error;
55
#[macro_export]
66
macro_rules! err_span {
77
($span:expr, $($msg:tt)*) => (
8-
$crate::Diagnostic::span_error(&$span, format!($($msg)*))
8+
$crate::Diagnostic::spanned_error(&$span, format!($($msg)*))
99
)
1010
}
1111

@@ -43,7 +43,16 @@ impl Diagnostic {
4343
}
4444
}
4545

46-
pub fn span_error<T: Into<String>>(node: &ToTokens, text: T) -> Diagnostic {
46+
pub fn span_error<T: Into<String>>(span: Span, text: T) -> Diagnostic {
47+
Diagnostic {
48+
inner: Repr::Single {
49+
text: text.into(),
50+
span: Some((span, span)),
51+
},
52+
}
53+
}
54+
55+
pub fn spanned_error<T: Into<String>>(node: &ToTokens, text: T) -> Diagnostic {
4756
Diagnostic {
4857
inner: Repr::Single {
4958
text: text.into(),

crates/macro-support/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The part of the implementation of the `#[wasm_bindgen]` attribute that is not in
1313
[features]
1414
spans = ["wasm-bindgen-backend/spans"]
1515
extra-traits = ["syn/extra-traits"]
16+
strict-macro = []
1617

1718
[dependencies]
1819
syn = { version = '0.15.0', features = ['visit'] }

crates/macro-support/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ mod parser;
2020

2121
/// Takes the parsed input from a `#[wasm_bindgen]` macro and returns the generated bindings
2222
pub fn expand(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diagnostic> {
23+
parser::reset_attrs_used();
2324
let item = syn::parse2::<syn::Item>(input)?;
2425
let opts = syn::parse2(attr)?;
2526

2627
let mut tokens = proc_macro2::TokenStream::new();
2728
let mut program = backend::ast::Program::default();
2829
item.macro_parse(&mut program, (Some(opts), &mut tokens))?;
2930
program.try_to_tokens(&mut tokens)?;
31+
32+
// If we successfully got here then we should have used up all attributes
33+
// and considered all of them to see if they were used. If one was forgotten
34+
// that's a bug on our end, so sanity check here.
35+
parser::assert_all_attrs_checked();
36+
3037
Ok(tokens)
3138
}

0 commit comments

Comments
 (0)