Skip to content

Commit 84d6c85

Browse files
committed
Trigger warnings for unused wasm-bindgen attributes
This attempts to do something similar to #3070, but without potentially dangerous fallout from strict-mode failing on all the existing code out there. Instead of forcing a compiler error like strict-mode does, this PR will internally generate unused variables with spans pointing to unused attributes, so that users get a relatively meaningful warning. Here's how the result looks like on example from #2874: ``` warning: unused variable: `typescript_type` --> tests\headless\snippets.rs:67:28 | 67 | #[wasm_bindgen(getter, typescript_type = "Thing[]")] | ^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_typescript_type` | = note: `#[warn(unused_variables)]` on by default ``` This is not 100% perfect - until Rust has a built-in `compile_warning!`, nothing is - but is a better status quo than the current one and can help users find problematic attributes without actually breaking their builds. Fixes #3038.
1 parent edc5adf commit 84d6c85

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

crates/macro-support/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn expand(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diag
3535
// If we successfully got here then we should have used up all attributes
3636
// and considered all of them to see if they were used. If one was forgotten
3737
// that's a bug on our end, so sanity check here.
38-
parser::assert_all_attrs_checked();
38+
parser::check_unused_attrs(&mut tokens);
3939

4040
Ok(tokens)
4141
}
@@ -51,7 +51,6 @@ pub fn expand_class_marker(
5151

5252
let mut program = backend::ast::Program::default();
5353
item.macro_parse(&mut program, (&opts.class, &opts.js_class))?;
54-
parser::assert_all_attrs_checked(); // same as above
5554

5655
// This is where things are slightly different, we are being expanded in the
5756
// context of an impl so we can't inject arbitrary item-like tokens into the
@@ -76,6 +75,7 @@ pub fn expand_class_marker(
7675
if let Err(e) = program.try_to_tokens(tokens) {
7776
err = Some(e);
7877
}
78+
parser::check_unused_attrs(tokens); // same as above
7979
tokens.append_all(item.attrs.iter().filter(|attr| match attr.style {
8080
syn::AttrStyle::Inner(_) => true,
8181
_ => false,

crates/macro-support/src/parser.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const JS_KEYWORDS: [&str; 20] = [
4141
struct AttributeParseState {
4242
parsed: Cell<usize>,
4343
checks: Cell<usize>,
44+
#[cfg(not(feature = "strict-macro"))]
45+
unused_attrs: std::cell::RefCell<Vec<Ident>>,
4446
}
4547

4648
/// Parsed attributes from a `#[wasm_bindgen(..)]`.
@@ -94,35 +96,40 @@ macro_rules! methods {
9496
($(($name:ident, $variant:ident($($contents:tt)*)),)*) => {
9597
$(methods!(@method $name, $variant($($contents)*));)*
9698

97-
#[cfg(feature = "strict-macro")]
9899
fn check_used(self) -> Result<(), Diagnostic> {
99100
// Account for the fact this method was called
100101
ATTRS.with(|state| state.checks.set(state.checks.get() + 1));
101102

102-
let mut errors = Vec::new();
103-
for (used, attr) in self.attrs.iter() {
104-
if used.get() {
105-
continue
106-
}
107-
// The check below causes rustc to crash on powerpc64 platforms
108-
// with an LLVM error. To avoid this, we instead use #[cfg()]
109-
// and duplicate the function below. See #58516 for details.
110-
/*if !cfg!(feature = "strict-macro") {
111-
continue
112-
}*/
113-
let span = match attr {
114-
$(BindgenAttr::$variant(span, ..) => span,)*
115-
};
116-
errors.push(Diagnostic::span_error(*span, "unused #[wasm_bindgen] attribute"));
103+
let unused =
104+
self.attrs
105+
.iter()
106+
.filter_map(|(used, attr)| if used.get() { None } else { Some(attr) })
107+
.map(|attr| {
108+
match attr {
109+
$(BindgenAttr::$variant(span, ..) => {
110+
#[cfg(feature = "strict-macro")]
111+
{
112+
Diagnostic::span_error(*span, "unused #[wasm_bindgen] attribute")
113+
}
114+
115+
#[cfg(not(feature = "strict-macro"))]
116+
{
117+
Ident::new(stringify!($name), *span)
118+
}
119+
},)*
120+
}
121+
});
122+
123+
#[cfg(feature = "strict-macro")]
124+
{
125+
Diagnostic::from_vec(unused.collect())
117126
}
118-
Diagnostic::from_vec(errors)
119-
}
120127

121-
#[cfg(not(feature = "strict-macro"))]
122-
fn check_used(self) -> Result<(), Diagnostic> {
123-
// Account for the fact this method was called
124-
ATTRS.with(|state| state.checks.set(state.checks.get() + 1));
125-
Ok(())
128+
#[cfg(not(feature = "strict-macro"))]
129+
{
130+
ATTRS.with(|state| state.unused_attrs.borrow_mut().extend(unused));
131+
Ok(())
132+
}
126133
}
127134
};
128135

@@ -1617,12 +1624,25 @@ pub fn reset_attrs_used() {
16171624
ATTRS.with(|state| {
16181625
state.parsed.set(0);
16191626
state.checks.set(0);
1627+
#[cfg(not(feature = "strict-macro"))]
1628+
state.unused_attrs.borrow_mut().clear();
16201629
})
16211630
}
16221631

1623-
pub fn assert_all_attrs_checked() {
1632+
pub fn check_unused_attrs(tokens: &mut TokenStream) {
16241633
ATTRS.with(|state| {
16251634
assert_eq!(state.parsed.get(), state.checks.get());
1635+
#[cfg(not(feature = "strict-macro"))]
1636+
{
1637+
let unused = &*state.unused_attrs.borrow();
1638+
tokens.extend(quote::quote! {
1639+
// Anonymous scope to prevent name clashes.
1640+
const _: () = {
1641+
#(let #unused: ();)*
1642+
};
1643+
});
1644+
}
1645+
let _ = tokens;
16261646
})
16271647
}
16281648

0 commit comments

Comments
 (0)