Skip to content

Provide builtin impls of Fn traits for fn-pointers #4493

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

Merged
merged 1 commit into from
May 18, 2020
Merged

Provide builtin impls of Fn traits for fn-pointers #4493

merged 1 commit into from
May 18, 2020

Conversation

hban
Copy link
Contributor

@hban hban commented May 17, 2020

Meant to be, but isn't actually a fix for #2880.

Consider this snippet:

use std::marker::PhantomData;
use std::ops::Deref;

struct Lazy<T, F/* = fn() -> T*/>(F, PhantomData<T>);

impl<T, F> Lazy<T, F> {
    pub fn new(f: F) -> Lazy<T, F> {
        Lazy(f, PhantomData)
    }
}

impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
    type Target = T;

    fn deref(&self) -> &T { todo!() }
}

fn test() {
    let lazy1: Lazy<u32, _> = Lazy::new(|| 0u32);
    let r1 = lazy1.to_string();

    fn make_u32_fn() -> u32 { todo!() }
    let make_u32_fn_ptr: fn() -> u32 = make_u32_fn;
    let lazy2: Lazy<u32, _> = Lazy::new(make_u32_fn_ptr);
    let r2 = lazy2.to_string();
}
  • On current master:
    • When type default is commented-out, r1 is correctly inferred, r2 in {unknown}.
    • When type default is not commented-out, both r1 and r2 are {unknown}.
  • With this PR:
    • When type default is commented-out, both r1 and r2 are correctly inferred.
    • When type default is not commented-out, both r1 and r2 are {unknown}.

Well, it's a improvement at least. I guess this thing with type defaults is a different problem.

I also tried add Fn impls for fn items, but wasn't successful. So this PR only adds those impls for fn pointers.

@flodiebold
Copy link
Member

flodiebold commented May 17, 2020

Thanks for your contribution! This is somewhat unfortunate though, both because there was another PR for the same thing just yesterday (#4483), and because the instructions in the bug were outdated -- Chalk is now providing built-in impls, so these should be added there as well: rust-lang/chalk#363.

I think at least the test would be good to keep though, even if it's just documenting how it's currently not working.

@hban
Copy link
Contributor Author

hban commented May 17, 2020

Bad timing, indeed 😄

That "good to keep" test, you mean one added in this PR, or that snippet in my first comment?

@flodiebold
Copy link
Member

Hm, both, actually!

@hban
Copy link
Contributor Author

hban commented May 18, 2020

Done.

@flodiebold
Copy link
Member

Thanks!

bors r+

@bors
Copy link
Contributor

bors bot commented May 18, 2020

@bors bors bot merged commit ad03e4d into rust-lang:master May 18, 2020
@hban hban deleted the builtin-fn-traits-for-fn-ptrs branch May 18, 2020 13:00
@cynecx cynecx mentioned this pull request Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants