Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide users who used the suggested (invalid) fix #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/macro-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn expand(attr: TokenStream, input: TokenStream) -> Result<TokenStream, Diag
// 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();
parser::check_unused_attrs(&mut tokens);

Ok(tokens)
}
Expand All @@ -51,7 +51,6 @@ pub fn expand_class_marker(

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

// This is where things are slightly different, we are being expanded in the
// context of an impl so we can't inject arbitrary item-like tokens into the
Expand All @@ -76,6 +75,7 @@ pub fn expand_class_marker(
if let Err(e) = program.try_to_tokens(tokens) {
err = Some(e);
}
parser::check_unused_attrs(tokens); // same as above
tokens.append_all(item.attrs.iter().filter(|attr| match attr.style {
syn::AttrStyle::Inner(_) => true,
_ => false,
Expand Down
74 changes: 49 additions & 25 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const JS_KEYWORDS: [&str; 20] = [
struct AttributeParseState {
parsed: Cell<usize>,
checks: Cell<usize>,
#[cfg(not(feature = "strict-macro"))]
unused_attrs: std::cell::RefCell<Vec<Ident>>,
}

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

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

let mut errors = Vec::new();
for (used, attr) in self.attrs.iter() {
if used.get() {
continue
}
// The check below causes rustc to crash on powerpc64 platforms
// with an LLVM error. To avoid this, we instead use #[cfg()]
// and duplicate the function below. See #58516 for details.
/*if !cfg!(feature = "strict-macro") {
continue
}*/
let span = match attr {
$(BindgenAttr::$variant(span, ..) => span,)*
};
errors.push(Diagnostic::span_error(*span, "unused #[wasm_bindgen] attribute"));
let unused =
self.attrs
.iter()
.filter_map(|(used, attr)| if used.get() { None } else { Some(attr) })
.map(|attr| {
match attr {
$(BindgenAttr::$variant(span, ..) => {
#[cfg(feature = "strict-macro")]
{
Diagnostic::span_error(*span, "unused #[wasm_bindgen] attribute")
}

#[cfg(not(feature = "strict-macro"))]
{
Ident::new(stringify!($name), *span)
}
},)*
}
});

#[cfg(feature = "strict-macro")]
{
Diagnostic::from_vec(unused.collect())
}
Diagnostic::from_vec(errors)
}

#[cfg(not(feature = "strict-macro"))]
fn check_used(self) -> Result<(), Diagnostic> {
// Account for the fact this method was called
ATTRS.with(|state| state.checks.set(state.checks.get() + 1));
Ok(())
#[cfg(not(feature = "strict-macro"))]
{
ATTRS.with(|state| state.unused_attrs.borrow_mut().extend(unused));
Ok(())
}
}
};

Expand Down Expand Up @@ -353,7 +360,11 @@ impl Parse for BindgenAttr {

attrgen!(parsers);

return Err(original.error("unknown attribute"));
return Err(original.error(if attr_string.starts_with("_") {
"unknown attribute: it's safe to remove unused attributes entirely."
} else {
"unknown attribute"
}));
}
}

Expand Down Expand Up @@ -1617,12 +1628,25 @@ pub fn reset_attrs_used() {
ATTRS.with(|state| {
state.parsed.set(0);
state.checks.set(0);
#[cfg(not(feature = "strict-macro"))]
state.unused_attrs.borrow_mut().clear();
})
}

pub fn assert_all_attrs_checked() {
pub fn check_unused_attrs(tokens: &mut TokenStream) {
ATTRS.with(|state| {
assert_eq!(state.parsed.get(), state.checks.get());
#[cfg(not(feature = "strict-macro"))]
{
let unused = &*state.unused_attrs.borrow();
tokens.extend(quote::quote! {
// Anonymous scope to prevent name clashes.
const _: () = {
#(let #unused: ();)*
};
});
}
let _ = tokens;
})
}

Expand Down