From e8650f6ad349196c9711e85b71fe7defa7736faf Mon Sep 17 00:00:00 2001 From: firelight flagboy Date: Mon, 4 Sep 2023 12:01:24 +0200 Subject: [PATCH] Add attribute `crate` to proc-macro `wasm_bindgen_test` This attribute allow to specify a path where `wasm-bindgen-test` can be accessed. Closes #3588 --- crates/test-macro/Cargo.toml | 2 +- crates/test-macro/src/lib.rs | 16 ++++++++++++++-- crates/test-macro/ui-tests/crate.rs | 20 ++++++++++++++++++++ crates/test-macro/ui-tests/crate.stderr | 5 +++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 crates/test-macro/ui-tests/crate.rs create mode 100644 crates/test-macro/ui-tests/crate.stderr diff --git a/crates/test-macro/Cargo.toml b/crates/test-macro/Cargo.toml index 6ffc549f2cc..540fd7f0a7a 100644 --- a/crates/test-macro/Cargo.toml +++ b/crates/test-macro/Cargo.toml @@ -14,7 +14,7 @@ proc-macro = true [dependencies] proc-macro2 = "1.0" quote = "1.0" -syn = { version = "2.0", default-features = false, features = [ "parsing", "proc-macro", "derive" ] } +syn = { version = "2.0", default-features = false, features = [ "parsing", "proc-macro", "derive", "printing" ] } [dev-dependencies] wasm-bindgen-test = { path = "../test" } diff --git a/crates/test-macro/src/lib.rs b/crates/test-macro/src/lib.rs index b68afbef2d0..8215bb3206a 100644 --- a/crates/test-macro/src/lib.rs +++ b/crates/test-macro/src/lib.rs @@ -74,10 +74,11 @@ pub fn wasm_bindgen_test( // later slurp up all of these functions and pass them as arguments to the // main test harness. This is the entry point for all tests. let name = format_ident!("__wbgt_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst)); + let wasm_bindgen_path = attributes.wasm_bindgen_path; tokens.extend( quote! { #[no_mangle] - pub extern "C" fn #name(cx: &::wasm_bindgen_test::__rt::Context) { + pub extern "C" fn #name(cx: &#wasm_bindgen_path::__rt::Context) { let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident)); #test_body } @@ -187,15 +188,26 @@ fn compile_error(span: Span, msg: &str) -> proc_macro::TokenStream { quote_spanned! { span => compile_error!(#msg); }.into() } -#[derive(Default)] struct Attributes { r#async: bool, + wasm_bindgen_path: syn::Path, +} + +impl Default for Attributes { + fn default() -> Self { + Self { + r#async: false, + wasm_bindgen_path: syn::parse_quote!(::wasm_bindgen_test), + } + } } impl Attributes { fn parse(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::parse::Result<()> { if meta.path.is_ident("async") { self.r#async = true; + } else if meta.path.is_ident("crate") { + self.wasm_bindgen_path = meta.value()?.parse::()?; } else { return Err(meta.error("unknown attribute")); } diff --git a/crates/test-macro/ui-tests/crate.rs b/crates/test-macro/ui-tests/crate.rs new file mode 100644 index 00000000000..2087f7575b6 --- /dev/null +++ b/crates/test-macro/ui-tests/crate.rs @@ -0,0 +1,20 @@ +#![no_implicit_prelude] + +extern crate wasm_bindgen_test_macro; +// +use wasm_bindgen_test_macro::wasm_bindgen_test; + +pub mod wasm { + pub extern crate wasm_bindgen_test as test; +} + +#[wasm_bindgen_test(crate = ::wasm_bindgen_test)] +fn success_1() {} + +#[wasm_bindgen_test(crate = crate::wasm::test)] +fn success_2() {} + +#[wasm_bindgen_test(crate = foo)] +fn failure_1() {} + +fn main() {} diff --git a/crates/test-macro/ui-tests/crate.stderr b/crates/test-macro/ui-tests/crate.stderr new file mode 100644 index 00000000000..2bace352ee4 --- /dev/null +++ b/crates/test-macro/ui-tests/crate.stderr @@ -0,0 +1,5 @@ +error[E0433]: failed to resolve: use of undeclared crate or module `foo` + --> ui-tests/crate.rs:17:29 + | +17 | #[wasm_bindgen_test(crate = foo)] + | ^^^ use of undeclared crate or module `foo`