Skip to content

Commit 83c9deb

Browse files
authored
Don't emit unexpected cfgs in proc-macros (#4284)
This changes when the `#[coverage]` attribute is emitted by adding an internal `coverage` crate feature to the proc-macro and their utility crates. This crate feature is enabled by `wasm-bindgen` and `wasm-bindgen-test` when `cfg(wasm_bindgen_unstable_test_coverage)` is enabled.
1 parent f071c7e commit 83c9deb

File tree

11 files changed

+61
-58
lines changed

11 files changed

+61
-58
lines changed

.github/workflows/main.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ jobs:
6767
- run: cargo clippy --no-deps --all-features -p example-tests -- -D warnings
6868
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-externref-xform -- -D warnings
6969
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p wasm-bindgen-futures -- -D warnings
70-
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-macro -- -D warnings
71-
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-macro-support -- -D warnings
70+
- run: cargo clippy --no-deps --features spans,strict-macro -p wasm-bindgen-macro -- -D warnings
71+
- run: cargo clippy --no-deps --features extra-traits,spans,strict-macro -p wasm-bindgen-macro-support -- -D warnings
7272
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-multi-value-xform -- -D warnings
7373
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-shared -- -D warnings
7474
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p wasm-bindgen-test -- -D warnings
75-
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-test-macro -- -D warnings
75+
- run: cargo clippy --no-deps -p wasm-bindgen-test-macro -- -D warnings
7676
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-threads-xform -- -D warnings
7777
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p typescript-tests -- -D warnings
7878
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-wasm-conventions -- -D warnings

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-featu
5252
"atomics",
5353
] }
5454

55+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), wasm_bindgen_unstable_test_coverage))'.dependencies]
56+
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-features = false, features = [
57+
"coverage",
58+
] }
59+
5560
[dev-dependencies]
5661
wasm-bindgen-test = { path = 'crates/test' }
5762

crates/backend/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version = "0.2.95"
1515

1616
[features]
1717
atomics = []
18+
coverage = []
1819
default = ["std"]
1920
extra-traits = ["syn/extra-traits"]
2021
spans = []

crates/backend/src/codegen.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ impl ToTokens for ast::Struct {
224224
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
225225
let unwrap_fn = Ident::new(&shared::unwrap_function(&name_str), Span::call_site());
226226
let wasm_bindgen = &self.wasm_bindgen;
227+
let maybe_no_coverage = coverage();
227228
(quote! {
228229
#[automatically_derived]
229230
impl #wasm_bindgen::describe::WasmDescribe for #name {
@@ -300,7 +301,7 @@ impl ToTokens for ast::Struct {
300301
#[doc(hidden)]
301302
// `allow_delayed` is whether it's ok to not actually free the `ptr` immediately
302303
// if it's still borrowed.
303-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
304+
#maybe_no_coverage
304305
pub unsafe extern "C" fn #free_fn(ptr: u32, allow_delayed: u32) {
305306
use #wasm_bindgen::__rt::alloc::rc::Rc;
306307

@@ -476,6 +477,7 @@ impl ToTokens for ast::StructField {
476477
quote! { assert_copy::<#ty>() }
477478
};
478479
let maybe_assert_copy = respan(maybe_assert_copy, ty);
480+
let maybe_no_coverage = coverage();
479481

480482
// Split this out so that it isn't affected by `quote_spanned!`.
481483
//
@@ -496,7 +498,7 @@ impl ToTokens for ast::StructField {
496498
const _: () = {
497499
#[cfg_attr(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), no_mangle)]
498500
#[doc(hidden)]
499-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
501+
#maybe_no_coverage
500502
pub unsafe extern "C" fn #getter(js: u32)
501503
-> #wasm_bindgen::convert::WasmRet<<#ty as #wasm_bindgen::convert::IntoWasmAbi>::Abi>
502504
{
@@ -538,7 +540,7 @@ impl ToTokens for ast::StructField {
538540
const _: () = {
539541
#[no_mangle]
540542
#[doc(hidden)]
541-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
543+
#maybe_no_coverage
542544
pub unsafe extern "C" fn #setter(
543545
js: u32,
544546
#(#args,)*
@@ -787,6 +789,8 @@ impl TryToTokens for ast::Export {
787789
quote! {}
788790
};
789791

792+
let maybe_no_coverage = coverage();
793+
790794
(quote! {
791795
#[automatically_derived]
792796
const _: () = {
@@ -795,7 +799,7 @@ impl TryToTokens for ast::Export {
795799
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
796800
export_name = #export_name,
797801
)]
798-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
802+
#maybe_no_coverage
799803
pub unsafe extern "C" fn #generated_name(#(#args),*) -> #wasm_bindgen::convert::WasmRet<#projection::Abi> {
800804
#start_check
801805

@@ -1156,6 +1160,8 @@ impl ToTokens for ast::StringEnum {
11561160
let hole = variant_count + 1;
11571161
let attrs = &self.rust_attrs;
11581162

1163+
let maybe_no_coverage = coverage();
1164+
11591165
let invalid_to_str_msg = format!(
11601166
"Converting an invalid string enum ({}) back to a string is currently not supported",
11611167
enum_name
@@ -1242,7 +1248,7 @@ impl ToTokens for ast::StringEnum {
12421248

12431249
#[automatically_derived]
12441250
impl #wasm_bindgen::describe::WasmDescribe for #enum_name {
1245-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
1251+
#maybe_no_coverage
12461252
fn describe() {
12471253
use #wasm_bindgen::describe::*;
12481254
inform(STRING_ENUM);
@@ -1541,6 +1547,7 @@ impl ToTokens for ast::Enum {
15411547
} else {
15421548
quote! { u32 }
15431549
};
1550+
let maybe_no_coverage = coverage();
15441551
let cast_clauses = self.variants.iter().map(|variant| {
15451552
let variant_name = &variant.name;
15461553
quote! {
@@ -1588,7 +1595,7 @@ impl ToTokens for ast::Enum {
15881595

15891596
#[automatically_derived]
15901597
impl #wasm_bindgen::describe::WasmDescribe for #enum_name {
1591-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
1598+
#maybe_no_coverage
15921599
fn describe() {
15931600
use #wasm_bindgen::describe::*;
15941601
inform(ENUM);
@@ -1853,6 +1860,8 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
18531860
return;
18541861
}
18551862

1863+
let maybe_no_coverage = coverage();
1864+
18561865
let name = Ident::new(&format!("__wbindgen_describe_{}", ident), ident.span());
18571866
let inner = &self.inner;
18581867
let attrs = &self.attrs;
@@ -1864,7 +1873,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
18641873
#(#attrs)*
18651874
#[no_mangle]
18661875
#[doc(hidden)]
1867-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
1876+
#maybe_no_coverage
18681877
pub extern "C" fn #name() {
18691878
use #wasm_bindgen::describe::*;
18701879
// See definition of `link_mem_intrinsics` for what this is doing
@@ -1955,3 +1964,10 @@ fn respan(input: TokenStream, span: &dyn ToTokens) -> TokenStream {
19551964
}
19561965
new_tokens.into_iter().collect()
19571966
}
1967+
1968+
fn coverage() -> Option<TokenStream> {
1969+
#[cfg(feature = "coverage")]
1970+
return Some(quote! { #[coverage(off)] });
1971+
#[cfg(not(feature = "coverage"))]
1972+
None
1973+
}

crates/macro-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version = "0.2.95"
1515

1616
[features]
1717
atomics = ["wasm-bindgen-backend/atomics"]
18+
coverage = ["wasm-bindgen-backend/coverage"]
1819
default = ["std"]
1920
extra-traits = ["syn/extra-traits"]
2021
spans = ["wasm-bindgen-backend/spans"]

crates/macro/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ proc-macro = true
1818

1919
[features]
2020
atomics = ["wasm-bindgen-macro-support/atomics"]
21+
coverage = ["wasm-bindgen-macro-support/coverage"]
2122
default = ["std"]
2223
spans = ["wasm-bindgen-macro-support/spans"]
2324
std = ["wasm-bindgen-macro-support/std"]
@@ -34,6 +35,3 @@ trybuild = "1.0"
3435
wasm-bindgen = { path = "../.." }
3536
wasm-bindgen-futures = { path = "../futures" }
3637
web-sys = { path = "../web-sys", features = ["Worker"] }
37-
38-
[lints.rust]
39-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

crates/macro/src/lib.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2")]
22
#![cfg_attr(
3-
any(
4-
wasm_bindgen_unstable_test_coverage,
5-
all(not(feature = "std"), feature = "atomics")
6-
),
3+
any(feature = "coverage", all(not(feature = "std"), feature = "atomics")),
74
feature(allow_internal_unstable),
85
allow(internal_features)
96
)]
@@ -14,10 +11,7 @@ use proc_macro::TokenStream;
1411
use quote::quote;
1512

1613
#[proc_macro_attribute]
17-
#[cfg_attr(
18-
wasm_bindgen_unstable_test_coverage,
19-
allow_internal_unstable(coverage_attribute)
20-
)]
14+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
2115
#[cfg_attr(
2216
all(not(feature = "std"), feature = "atomics"),
2317
allow_internal_unstable(thread_local)
@@ -48,10 +42,7 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
4842
/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
4943
/// ```
5044
#[proc_macro]
51-
#[cfg_attr(
52-
wasm_bindgen_unstable_test_coverage,
53-
allow_internal_unstable(coverage_attribute)
54-
)]
45+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
5546
pub fn link_to(input: TokenStream) -> TokenStream {
5647
match wasm_bindgen_macro_support::expand_link_to(input.into()) {
5748
Ok(tokens) => {
@@ -68,10 +59,7 @@ pub fn link_to(input: TokenStream) -> TokenStream {
6859
}
6960

7061
#[proc_macro_attribute]
71-
#[cfg_attr(
72-
wasm_bindgen_unstable_test_coverage,
73-
allow_internal_unstable(coverage_attribute)
74-
)]
62+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
7563
pub fn __wasm_bindgen_class_marker(attr: TokenStream, input: TokenStream) -> TokenStream {
7664
match wasm_bindgen_macro_support::expand_class_marker(attr.into(), input.into()) {
7765
Ok(tokens) => {

crates/test-macro/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ version = "0.3.45"
1212
[lib]
1313
proc-macro = true
1414

15+
[features]
16+
coverage = []
17+
1518
[dependencies]
1619
proc-macro2 = "1.0"
1720
quote = "1.0"
@@ -26,6 +29,3 @@ syn = { version = "2.0", default-features = false, features = [
2629
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2730
trybuild = "1.0"
2831
wasm-bindgen-test = { path = "../test" }
29-
30-
[lints.rust]
31-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

crates/test-macro/src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! going on here.
33
44
#![cfg_attr(
5-
wasm_bindgen_unstable_test_coverage,
5+
feature = "coverage",
66
feature(allow_internal_unstable),
77
allow(internal_features)
88
)]
@@ -18,10 +18,7 @@ use std::sync::atomic::*;
1818
static CNT: AtomicUsize = AtomicUsize::new(0);
1919

2020
#[proc_macro_attribute]
21-
#[cfg_attr(
22-
wasm_bindgen_unstable_test_coverage,
23-
allow_internal_unstable(coverage_attribute)
24-
)]
21+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
2522
pub fn wasm_bindgen_test(
2623
attr: proc_macro::TokenStream,
2724
body: proc_macro::TokenStream,
@@ -109,12 +106,17 @@ pub fn wasm_bindgen_test(
109106
// main test harness. This is the entry point for all tests.
110107
let name = format_ident!("__wbgt_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst));
111108
let wasm_bindgen_path = attributes.wasm_bindgen_path;
109+
let coverage = if cfg!(feature = "coverage") {
110+
Some(quote! { #[coverage(off)] })
111+
} else {
112+
None
113+
};
112114
tokens.extend(
113115
quote! {
114116
const _: () = {
115117
#[no_mangle]
116118
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
117-
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
119+
#coverage
118120
pub extern "C" fn #name(cx: &#wasm_bindgen_path::__rt::Context) {
119121
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
120122
#test_body

crates/test/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.45' }
2424

2525
[target.'cfg(all(target_arch = "wasm32", wasm_bindgen_unstable_test_coverage))'.dependencies]
2626
minicov = "0.3"
27+
wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.45', features = ["coverage"] }
2728

2829
[lints.rust]
2930
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

guide/src/wasm-bindgen-test/coverage.md

+11-20
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ You can ask the runner to generate coverage data from functions marked as `#[was
99

1010
## Enabling the feature
1111

12-
To enable this feature, you need to set `cfg(wasm_bindgen_unstable_test_coverage)` for `wasm-bindgen-test` and its dependencies.
13-
14-
Currently it is particularly difficult to [deliver compile-line arguments to proc-macros when cross-compiling with Cargo][1]. To circumvent this [host-config] can be used.
15-
16-
[1]: https://github.com/rust-lang/cargo/issues/4423
17-
[host-config]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#host-config
12+
To enable this feature, you need to enable `cfg(wasm_bindgen_unstable_test_coverage)`.
1813

1914
## Generating the data
2015

@@ -26,18 +21,18 @@ Due to the current limitation of `llvm-cov`, we can't collect profiling symbols
2621

2722
### Arguments to the test runner
2823

29-
The following environment variables can be used to control the coverage output when [executing the test runner][2]:
24+
The following environment variables can be used to control the coverage output when [executing the test runner][1]:
3025

31-
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT` to control the file name of the profraw or the directory in which it is placed
26+
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT` to control the file name of the profraw or the directory in which it is placed. It might be necessary to provide the full path if e.g. running tests in a workspace.
3227
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_PREFIX` to add a custom prefix to the profraw files. This can be useful if you're running the tests automatically in succession.
3328

34-
[2]: usage.html#appendix-using-wasm-bindgen-test-without-wasm-pack
29+
[1]: usage.html#appendix-using-wasm-bindgen-test-without-wasm-pack
3530

3631
### Target features
3732

38-
This feature relies on the [minicov] crate, which provides a profiling runtime for WebAssembly. It in turn uses [cc] to compile the runtime to Wasm, which [currently doesn't support accounting for target feature][3]. Use e.g. `CFLAGS_wasm32_unknown_unknown="-matomics -mbulk-memory"` to account for that.
33+
This feature relies on the [minicov] crate, which provides a profiling runtime for WebAssembly. It in turn uses [cc] to compile the runtime to Wasm, which [currently doesn't support accounting for target feature][2]. Use e.g. `CFLAGS_wasm32_unknown_unknown="-matomics -mbulk-memory"` to account for that.
3934

40-
[3]: https://github.com/rust-lang/cc-rs/issues/268
35+
[2]: https://github.com/rust-lang/cc-rs/issues/268
4136
[cc]: https://crates.io/crates/cc
4237
[minicov]: https://crates.io/crates/minicov
4338

@@ -47,13 +42,10 @@ This adapts code taken from the [Rustc book], see that for more examples and gen
4742

4843
```sh
4944
# Run the tests:
50-
# - `CARGO_HOST_RUSTFLAGS` to pass the configuration to `wasm-bindgen-macro`.
51-
# - `-Ztarget-applies-to-host -Zhost-config` to enable `CARGO_HOST_RUSTFLAGS`.
52-
# - `--tests` to not run documentation tests, which is currently not supported.
53-
CARGO_HOST_RUSTFLAGS=--cfg=wasm_bindgen_unstable_test_coverage \
45+
# `--tests` to not run documentation tests, which is currently not supported.
5446
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
5547
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner \
56-
cargo +nightly test -Ztarget-applies-to-host -Zhost-config --tests
48+
cargo +nightly test --tests
5749
# Compile to object files:
5850
# - Extract a list of compiled artifacts from Cargo and filter them with `jq`.
5951
# - Figure out the path to the LLVM IR file corresponding to an artifact.
@@ -62,9 +54,8 @@ crate_name=name_of_the_tested_crate_in_snake_case
6254
objects=()
6355
IFS=$'\n'
6456
for file in $(
65-
CARGO_HOST_RUSTFLAGS=--cfg=wasm_bindgen_unstable_test_coverage \
6657
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
67-
cargo +nightly test -Ztarget-applies-to-host -Zhost-config --tests --no-run --message-format=json | \
58+
cargo +nightly test --tests --no-run --message-format=json | \
6859
jq -r "select(.reason == \"compiler-artifact\") | (select(.target.kind == [\"test\"]) // select(.target.name == \"$crate_name\")) | .filenames[0]"
6960
)
7061
do
@@ -89,7 +80,7 @@ llvm-cov-19 show -show-instantiations=false -Xdemangler=rustfilt -output-dir cov
8980

9081
## Attribution
9182

92-
These methods have originally been pioneered by [Hacken OÜ], see [their guide][4] as well.
83+
These methods have originally been pioneered by [Hacken OÜ], see [their guide][3] as well.
9384

94-
[4]: https://hknio.github.io/wasmcov
85+
[3]: https://hknio.github.io/wasmcov
9586
[Hacken OÜ]: https://hacken.io

0 commit comments

Comments
 (0)