Skip to content

Commit

Permalink
Merge pull request #1055 from alexcrichton/strict
Browse files Browse the repository at this point in the history
Assert all attributes are used by default
  • Loading branch information
alexcrichton authored Nov 28, 2018
2 parents e3b6286 + c8a3521 commit f4c1c64
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 357 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ std = []
serde-serialize = ["serde", "serde_json", "std"]
nightly = []

# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
# all unused attributes
strict-macro = ["wasm-bindgen-macro/strict-macro"]

# This is only for debugging wasm-bindgen! No stability guarantees, so enable
# this at your own peril!
xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_generated_code"]
Expand Down
13 changes: 11 additions & 2 deletions crates/backend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::parse::Error;
#[macro_export]
macro_rules! err_span {
($span:expr, $($msg:tt)*) => (
$crate::Diagnostic::span_error(&$span, format!($($msg)*))
$crate::Diagnostic::spanned_error(&$span, format!($($msg)*))
)
}

Expand Down Expand Up @@ -43,7 +43,16 @@ impl Diagnostic {
}
}

pub fn span_error<T: Into<String>>(node: &ToTokens, text: T) -> Diagnostic {
pub fn span_error<T: Into<String>>(span: Span, text: T) -> Diagnostic {
Diagnostic {
inner: Repr::Single {
text: text.into(),
span: Some((span, span)),
},
}
}

pub fn spanned_error<T: Into<String>>(node: &ToTokens, text: T) -> Diagnostic {
Diagnostic {
inner: Repr::Single {
text: text.into(),
Expand Down
1 change: 1 addition & 0 deletions crates/macro-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The part of the implementation of the `#[wasm_bindgen]` attribute that is not in
[features]
spans = ["wasm-bindgen-backend/spans"]
extra-traits = ["syn/extra-traits"]
strict-macro = []

[dependencies]
syn = { version = '0.15.0', features = ['visit'] }
Expand Down
7 changes: 7 additions & 0 deletions crates/macro-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ mod parser;

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

let mut tokens = proc_macro2::TokenStream::new();
let mut program = backend::ast::Program::default();
item.macro_parse(&mut program, (Some(opts), &mut tokens))?;
program.try_to_tokens(&mut tokens)?;

// If we successfully got here then we should have used up all attributes
// and considered all of them to see if they were used. If one was forgotten
// that's a bug on our end, so sanity check here.
parser::assert_all_attrs_checked();

Ok(tokens)
}
Loading

0 comments on commit f4c1c64

Please sign in to comment.