Skip to content

implement Testable for Fn* #56

Closed
Closed

Description

@abr-egn

Using rustc 1.0.0-nightly (3d0d9bb6f 2015-01-12 22:56:20 +0000):

fn reverse<T: Clone>(xs: &[T]) -> Vec<T> {
    let mut rev = vec!();
    for x in xs.iter() {
        rev.insert(0, x.clone())
    }
    rev
}

#[test]
fn reverse_identity() {
  fn prop(xs: Vec<i32>) -> bool {
    xs == reverse(reverse(xs.as_slice()).as_slice())
  }
  quickcheck(prop);
}

produces

error: the trait `quickcheck::tester::Testable` is not implemented for the type `fn(collections::vec::Vec<i32>) -> bool {tests::reverse_identity::prop}`

quickcheck(false) compiles; this appears to be specific to the fn impls, including the zero-arg fn.

Activity

  1. BurntSushi commented on Jan 14, 2015

    @BurntSushi
    Owner

    Errm, try: quickcheck(prop as fn(Vec<i32>) -> bool).

  2. abr-egn commented on Jan 14, 2015

    @abr-egn
    Author

    That does work, thank you. I'm a bit baffled that it's required since it's trivially inferrable type information...

  3. BurntSushi commented on Jan 14, 2015

    @BurntSushi
    Owner

    I'm pretty sure it's a known bug, but I can't find it in the Rust issue tracker at the moment. I think this happened when Rust moved everything over to unboxed closures. Unfortunately, I can't remember why this happens.

  4. tomjakubowski commented on Jan 14, 2015

    @tomjakubowski
    Contributor

    The PR that introduced this, I think, is rust-lang/rust#19891. I too remember seeing something that indicated this coercion is supposed to exist at some point but can't find it either.

  5. milibopp commented on Jan 15, 2015

    @milibopp
    Contributor

    Afaik, functions now implement the Fn(…) -> … traits. So I think it would actually be cleaner if quickcheck would impl<F: Fn(…) -> …> Testable for F instead of coercing to a trait object.

    Edit: that does not work together with the coherence rules, does it?

  6. BurntSushi commented on May 2, 2015

    @BurntSushi
    Owner

    I've tried really hard to work around this (by pursuing impls with Fn instead of fn) and I haven't been able to come up with a solution yet.

  7. BurntSushi commented on May 13, 2015

    @BurntSushi
    Owner

    I think I'm currently blocked on this by this bug: rust-lang/rust#25041 (Well, I think it's a bug. It sure feels like it.)

  8. michaelsproul commented on May 23, 2015

    @michaelsproul

    cc me

  9. FranklinChen commented on Jul 26, 2015

    @FranklinChen

    Just to clarify: we are currently blocked on being able to quickcheck an arbitrary closure? (Important to be able to nest to stage generation of values, as in Haskell QuickCheck and Scala ScalaCheck.)

  10. BurntSushi commented on Jul 27, 2015

    @BurntSushi
    Owner

    @FranklinChen Yup. I might be missing another solution to the problem, but the straight-forward path is to implement Testable on closure types, and the bug in rust-lang/rust#25041 seems to be preventing that.

  11. shepmaster commented on Dec 8, 2015

    @shepmaster
    Contributor

    While this still doesn't seem possible at the type level yet, you can reduce a bit of boilerplate by using _ when casting to the function pointer:

    fn prop(xs: Vec<i32>) -> bool {
        xs == reverse(&reverse(&xs[..])[..])
    }
    quickcheck(prop as fn(_) -> _);

    This is a bit nicer as you only have to duplicate the number of arguments, not the types.

  12. FranklinChen commented on Dec 9, 2015

    @FranklinChen

    @shepmaster Ah, thanks for that tip. Could a macro automate that?

  13. changed the title [-]Minimal example does not compile (broken fn impl?)[/-] [+]implement Testable for Fn*[/+] on Nov 28, 2017
  14. BurntSushi commented on Dec 27, 2020

    @BurntSushi
    Owner

    As far as I can tell, this is just never going to work. See rust-lang/rust#25041 for more discussion, where it seems to me the issue has been interpreted as "confusing" rather than a bug to fix. Some reasons are given but I don't quite understand them. I don't see any movement to resolve it, so after half a decade, I'm giving up and calling this wontfix.

    I would very much love to make quickcheck work with closures though. So if there are any fresh ideas, we can re-open is this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions