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

custom_test_frameworks doesn't work with plain functions #65210

Open
exFalso opened this issue Oct 8, 2019 · 2 comments
Open

custom_test_frameworks doesn't work with plain functions #65210

exFalso opened this issue Oct 8, 2019 · 2 comments
Labels
A-libtest Area: #[test] related C-bug Category: This is a bug. F-custom_test_frameworks `#![feature(custom_test_frameworks)]` requires-nightly This issue requires a nightly compiler in some way.

Comments

@exFalso
Copy link

exFalso commented Oct 8, 2019

The following code:

#![feature(custom_test_frameworks)]
#![test_runner(my_test_runner)]

fn my_test_runner(tests: &[&fn() -> ()]) {
    for test in tests {
        test();
    }
}

#[test_case]
fn test_1() {}

#[test_case]
fn test_2() {}

results in

11 | fn test_1() {}
   | ^^^^^^^^^^^^^^ expected fn pointer, found fn item
   |
   = note: expected type `&fn()`
              found type `&fn() {test_1}`

The workaround mentioned in https://stackoverflow.com/questions/27895946/expected-fn-item-found-a-different-fn-item-when-working-with-function-pointer doesn't work, as these functions are passed in by the compiler, so we cannot do the coercion.

Other variants that don't work:

fn my_test_runner(tests: &[fn() -> ()]) {
fn my_test_runner<F: Fn() -> ()>(tests: &[F]) {
fn my_test_runner<F: Fn() -> ()>(tests: &[&F]) {
@exFalso
Copy link
Author

exFalso commented Oct 8, 2019

If anyone hits this, my current workaround is to use static lambdas, i.e:

#[test_case]
static TEST_1: fn() -> () = || {};

#[test_case]
static TEST_2: fn() -> () = || {};

@jonas-schievink jonas-schievink added A-libtest Area: #[test] related C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. labels Oct 8, 2019
@Centril Centril added the F-custom_test_frameworks `#![feature(custom_test_frameworks)]` label Oct 8, 2019
@yannick-was-taken
Copy link

Note for anyone coming across this issue:

You can use dyn Fn() instead of &fn() -> (), which works:

fn my_test_runner(tests: &[&dyn Fn()]) {
    for test in tests {
        test();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-libtest Area: #[test] related C-bug Category: This is a bug. F-custom_test_frameworks `#![feature(custom_test_frameworks)]` requires-nightly This issue requires a nightly compiler in some way.
Projects
Status: No status
Development

No branches or pull requests

4 participants