diff --git a/Cargo.toml b/Cargo.toml index b1faa4e8e6a..4698285386f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,9 @@ xxx_debug_only_print_generated_code = [ "wasm-bindgen-macro/xxx_debug_only_print_generated_code", ] +## Turns off coverage generation for describe blocks +unstable-coverage = ["wasm-bindgen-macro/unstable-coverage"] + [dependencies] wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.90" } serde = { version = "1.0", optional = true } diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 8be4e953a1f..19f12fdb5af 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -15,6 +15,7 @@ rust-version = "1.57" [features] spans = [] extra-traits = ["syn/extra-traits"] +unstable-coverage = [] [dependencies] bumpalo = "3.0.0" diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index bbcbc9cea14..ac3f535c3d9 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -160,6 +160,16 @@ impl TryToTokens for ast::LinkToModule { } } +#[cfg(feature = "unstable-coverage")] +fn coverage() -> TokenStream { + quote!(#[coverage(off)]) +} + +#[cfg(not(feature = "unstable-coverage"))] +fn coverage() -> TokenStream { + quote!() +} + impl ToTokens for ast::Struct { fn to_tokens(&self, tokens: &mut TokenStream) { let name = &self.rust_name; @@ -169,10 +179,12 @@ impl ToTokens for ast::Struct { let new_fn = Ident::new(&shared::new_function(&name_str), Span::call_site()); let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site()); let unwrap_fn = Ident::new(&shared::unwrap_function(&name_str), Span::call_site()); + let coverage = coverage(); let wasm_bindgen = &self.wasm_bindgen; (quote! { #[automatically_derived] impl #wasm_bindgen::describe::WasmDescribe for #name { + #coverage fn describe() { use #wasm_bindgen::__wbindgen_if_not_std; __wbindgen_if_not_std! { @@ -814,6 +826,7 @@ impl ToTokens for ast::ImportType { }); let no_deref = self.no_deref; + let coverage = coverage(); (quote! { #[automatically_derived] @@ -835,6 +848,7 @@ impl ToTokens for ast::ImportType { use #wasm_bindgen::__rt::core; impl WasmDescribe for #rust_name { + #coverage fn describe() { #description } @@ -1048,6 +1062,7 @@ impl ToTokens for ast::ImportEnum { let variant_paths_ref = &variant_paths; let wasm_bindgen = &self.wasm_bindgen; + let coverage = coverage(); (quote! { #(#attrs)* @@ -1082,6 +1097,7 @@ impl ToTokens for ast::ImportEnum { // It should really be using &str for all of these, but that requires some major changes to cli-support #[automatically_derived] impl #wasm_bindgen::describe::WasmDescribe for #name { + #coverage fn describe() { <#wasm_bindgen::JsValue as #wasm_bindgen::describe::WasmDescribe>::describe() } @@ -1404,6 +1420,7 @@ impl ToTokens for ast::Enum { }); let try_from_cast_clauses = cast_clauses.clone(); let wasm_bindgen = &self.wasm_bindgen; + let coverage = coverage(); (quote! { #[automatically_derived] impl #wasm_bindgen::convert::IntoWasmAbi for #enum_name { @@ -1441,6 +1458,7 @@ impl ToTokens for ast::Enum { #[automatically_derived] impl #wasm_bindgen::describe::WasmDescribe for #enum_name { + #coverage fn describe() { use #wasm_bindgen::describe::*; inform(ENUM); diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index d9262e857f5..92311fae31c 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -27,6 +27,11 @@ #![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))] #![deny(missing_docs)] #![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")] +#![cfg_attr( + feature = "unstable-coverage", + feature(allow_internal_unstable), + allow(internal_features) +)] pub use crate::codegen::TryToTokens; pub use crate::error::Diagnostic; diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs index dc605cac47e..95df5bd77eb 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -264,7 +264,6 @@ fn coverage_args(tmpdir: &Path) -> Option { .map(|s| s.to_str().unwrap().to_string()) .unwrap_or_default(); - match env::var_os("WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT") { Some(s) => { let mut buf = PathBuf::from(s); diff --git a/crates/macro-support/Cargo.toml b/crates/macro-support/Cargo.toml index 1ba1c8717b8..c57525b0bb6 100644 --- a/crates/macro-support/Cargo.toml +++ b/crates/macro-support/Cargo.toml @@ -16,6 +16,7 @@ rust-version = "1.57" spans = ["wasm-bindgen-backend/spans"] extra-traits = ["syn/extra-traits"] strict-macro = [] +unstable-coverage = ["wasm-bindgen-backend/unstable-coverage"] [dependencies] syn = { version = '2.0', features = ['visit', 'full'] } diff --git a/crates/macro/Cargo.toml b/crates/macro/Cargo.toml index ae7a3044ced..a122574a641 100644 --- a/crates/macro/Cargo.toml +++ b/crates/macro/Cargo.toml @@ -19,6 +19,7 @@ proc-macro = true spans = ["wasm-bindgen-macro-support/spans"] xxx_debug_only_print_generated_code = [] strict-macro = ["wasm-bindgen-macro-support/strict-macro"] +unstable-coverage = ["wasm-bindgen-macro-support/unstable-coverage"] [dependencies] wasm-bindgen-macro-support = { path = "../macro-support", version = "=0.2.90" } diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index fbae818750e..15f313a4f18 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -1,4 +1,9 @@ #![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2")] +#![cfg_attr( + feature = "unstable-coverage", + feature(allow_internal_unstable), + allow(internal_features) +)] extern crate proc_macro; @@ -6,6 +11,10 @@ use proc_macro::TokenStream; use quote::quote; #[proc_macro_attribute] +#[cfg_attr( + feature = "unstable-coverage", + allow_internal_unstable(coverage_attribute) +)] pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream { match wasm_bindgen_macro_support::expand(attr.into(), input.into()) { Ok(tokens) => { diff --git a/crates/test/Cargo.toml b/crates/test/Cargo.toml index 8384d50b395..c77a8435623 100644 --- a/crates/test/Cargo.toml +++ b/crates/test/Cargo.toml @@ -10,7 +10,7 @@ rust-version = "1.57" [features] default = [] -unstable-coverage = ["minicov"] +unstable-coverage = ["minicov", "wasm-bindgen/unstable-coverage"] [dependencies] console_error_panic_hook = '0.1' diff --git a/src/closure.rs b/src/closure.rs index 319f4c63c24..78933e399e2 100644 --- a/src/closure.rs +++ b/src/closure.rs @@ -465,6 +465,7 @@ impl WasmDescribe for Closure where T: WasmClosure + ?Sized, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(EXTERNREF); } @@ -565,6 +566,7 @@ macro_rules! doit { where $($var: FromWasmAbi + 'static,)* R: ReturnWasmAbi + 'static, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { #[allow(non_snake_case)] unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>( @@ -622,6 +624,7 @@ macro_rules! doit { where $($var: FromWasmAbi + 'static,)* R: ReturnWasmAbi + 'static, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { #[allow(non_snake_case)] unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>( @@ -763,6 +766,7 @@ where A: RefFromWasmAbi, R: ReturnWasmAbi + 'static, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { #[allow(non_snake_case)] unsafe extern "C" fn invoke( @@ -809,6 +813,7 @@ where A: RefFromWasmAbi, R: ReturnWasmAbi + 'static, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { #[allow(non_snake_case)] unsafe extern "C" fn invoke( diff --git a/src/convert/closures.rs b/src/convert/closures.rs index 8227a6b622f..b0a2ef8ffe4 100644 --- a/src/convert/closures.rs +++ b/src/convert/closures.rs @@ -54,6 +54,7 @@ macro_rules! stack_closures { where $($var: FromWasmAbi,)* R: ReturnWasmAbi { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(FUNCTION); inform($invoke::<$($var,)* R> as u32); @@ -108,6 +109,7 @@ macro_rules! stack_closures { where $($var: FromWasmAbi,)* R: ReturnWasmAbi { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(FUNCTION); inform($invoke_mut::<$($var,)* R> as u32); @@ -177,6 +179,7 @@ where A: RefFromWasmAbi, R: ReturnWasmAbi, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(FUNCTION); inform(invoke1_ref:: as u32); @@ -232,6 +235,7 @@ where A: RefFromWasmAbi, R: ReturnWasmAbi, { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(FUNCTION); inform(invoke1_mut_ref:: as u32); diff --git a/src/describe.rs b/src/describe.rs index d3b5fd0b546..c0dfd64a60e 100644 --- a/src/describe.rs +++ b/src/describe.rs @@ -103,18 +103,21 @@ cfg_if! { } impl WasmDescribe for *const T { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(U32) } } impl WasmDescribe for *mut T { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(U32) } } impl WasmDescribe for [T] { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(SLICE); T::describe(); @@ -122,6 +125,7 @@ impl WasmDescribe for [T] { } impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a T { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(REF); T::describe(); @@ -129,6 +133,7 @@ impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a T { } impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a mut T { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(REFMUT); T::describe(); @@ -166,12 +171,14 @@ if_std! { } impl WasmDescribe for Box<[T]> { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { T::describe_vector(); } } impl WasmDescribe for Vec where Box<[T]>: WasmDescribe { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { >::describe(); } @@ -179,6 +186,7 @@ if_std! { } impl WasmDescribe for Option { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(OPTIONAL); T::describe(); @@ -186,12 +194,14 @@ impl WasmDescribe for Option { } impl WasmDescribe for () { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(UNIT) } } impl> WasmDescribe for Result { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(RESULT); T::describe(); @@ -199,6 +209,7 @@ impl> WasmDescribe for Result { } impl WasmDescribe for Clamped { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { inform(CLAMPED); T::describe(); @@ -206,6 +217,7 @@ impl WasmDescribe for Clamped { } impl WasmDescribe for JsError { + #[cfg_attr(feature = "unstable-coverage", coverage(off))] fn describe() { JsValue::describe(); } diff --git a/src/lib.rs b/src/lib.rs index f38d6a3e2c4..9b5783f6d6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ #![no_std] #![allow(coherence_leak_check)] #![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")] +#![cfg_attr(feature = "unstable-coverage", feature(coverage_attribute))] use core::convert::TryFrom; use core::fmt;