Skip to content

Commit

Permalink
Assert all attributes are used by default
Browse files Browse the repository at this point in the history
This commit implements a system that will assert that all
`#[wasm_bindgen]` attributes are actually used during compilation. This
should help ensure that we don't sneak in stray attributes that don't
actually end up having any meaning, and hopefully make it a bit easier
to learn `#[wasm_bindgen]`!
  • Loading branch information
alexcrichton committed Nov 28, 2018
1 parent e3b6286 commit 48d3b74
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 338 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ test = false
doctest = false

[features]
default = ["spans", "std"]
default = ["spans", "std", 'strict-macro']
spans = ["wasm-bindgen-macro/spans"]
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 48d3b74

Please sign in to comment.