Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Error extending Fn trait then using it as a trait object: the value of the associated type `Output must be specified #24010

Closed

Description

@carllerche
trait Foo: Fn(i32) + Send { }
impl<T:?Sized + Fn(i32) + Send> Foo for T { }

fn wants_foo(t: Box<Foo>) {
}

fn main() {
  let f = Box::new(|x| println!("{}", x));
  wants_foo(f);
}

Output:

<anon>:9:13: 9:14 error: the value of the associated type `Output` (from the trait `core::ops::FnOnce`) must be specified [E0191]
<anon>:9   wants_foo(f);
                     ^

cc @nikomatsakis

Activity

  1. nikomatsakis commented on Apr 3, 2015

    @nikomatsakis
    Contributor

    The problem is that the associated type is defined in a supertrait, and we don't consider that case when we check that the object type is well-formed.

  2. added
    A-trait-system
    A-associated-items
    on Apr 4, 2015
  3. Diggsey commented on May 7, 2015

    @Diggsey
    Contributor

    It can be quite painful to work around this problem, as it's the only way to create aliases for traits.

    When you have bounds such as the following scattered all over the code: Fn(&(Any + Send), &'static str, u32) + 'static, it's much nicer to be able to give it a short name.

  4. jonhoo commented on Jun 1, 2015

    @jonhoo
    Contributor

    @Diggsey: what workaround would you propose? I've been hitting my head against this wall for a while now, and am getting nowhere..

  5. Diggsey commented on Jun 1, 2015

    @Diggsey
    Contributor

    @jonhoo I don't know about your particular use-case, but for me I was using this to create a short alias for a trait - the workaround was just to use the long name everywhere, ie. Fn(&(Any + Send), &'static str, u32) + 'static

  6. jonhoo commented on Jun 1, 2015

    @jonhoo
    Contributor

    My use-case is exactly the same. I was hoping I might be able to use a macro that expanded to the type, but that doesn't seem to work either. I suppose I'll just have to use the full signature for now..

  7. Kimundi commented on Sep 5, 2015

    @Kimundi
    Contributor

    @nikomatsakis Why is that case not considered?

  8. nikomatsakis commented on Sep 9, 2015

    @nikomatsakis
    Contributor

    @Kimundi it's a bug, really.

  9. Kimundi commented on Sep 12, 2015

    @Kimundi
    Contributor

    Ah, okay it sounded like that was intentional :)

  10. DGolubets commented on Feb 28, 2016

    @DGolubets

    Is it going to be fixed in any upcoming version?

  11. nikomatsakis commented on May 5, 2016

    @nikomatsakis
    Contributor

    Note that @durka points out in #29328 this does technically allow one to violate some stability restrictions, but probably not in a way that matters:

    trait FnAlias: Fn() {}
    
    fn main() {
        let _: FnAlias;            // error
        let _: FnAlias<Output=()>; // no error, but I shouldn't be able to do this!
    }
  12. jimmycuadra commented on Mar 5, 2017

    @jimmycuadra
    Contributor

    This bug is very annoying. I have the same use case as @Diggsey and @jonhoo: creating an alias of a trait with cumbersome bounds that I don't want to have to repeat everywhere.

    In case it's not obvious from the title, this doesn't have to do with the Fn trait specifically. It's just about supertrait associated types being ignored when using the subtrait. Here's a simpler illustration:

    trait A {
        type Assoc;
        // Pretend there are several more associated types here to make uses of `Box<A>` inconvenient.
    }
    
    trait B: A<Assoc=()> {}
    
    fn takes_b(b: Box<B>) {}

    which results in:

    error[E0191]: the value of the associated type `Assoc` (from the trait `A`) must be specified
     --> src/lib.rs:8:19
      |
    8 | fn takes_b(b: Box<B>) {}
      |                   ^ missing associated type `Assoc` value
    

    The compiler should be able to infer that type Assoc = () here.

  13. maurer commented on May 23, 2017

    @maurer
    Contributor
    use std::iter::Iterator;
    
    pub trait LeapfrogIterator<T, V> : Iterator<Item=(T,V)> {
        fn next_above(&mut self, &T) -> Option<(T, V)>;
    }
    
    fn main () {
        let v: Box<LeapfrogIterator<i64, i64>> = unimplemented!();
    }

    https://is.gd/VcQmki

    is another concrete use case - if I want to extend Iterator in this way, I need to write LeapfrogIterator<K, V, Item=(K, V)> every time I want to talk about the trait, even though this is the only legal value for Item to take on. (This doesn't express the bug in a new way, but is an example I ran into organically and eventually found this bug due to.)

  14. glyn commented on Jun 15, 2017

    @glyn
    Contributor

    I hit this when trying to create an alias of a FnMut trait. Using a macro still doesn't work as @jonhoo found two years ago, e.g. https://is.gd/h9CZ8H, because trait bounds don't seem to accept macro invocations.

  15. 7 remaining items

  16. durka commented on Jul 4, 2017

    @durka
    Contributor

    @nikomatsakis

    The problem is that the associated type is defined in a supertrait, and we don't consider that case when we check that the object type is well-formed.

    @Kimundi it's a bug, really.

    Is it possible to give some mentoring hints for fixing this?

  17. added a commit that references this issue on Jul 5, 2017
  18. added
    C-bug
    on Jul 22, 2017
  19. mbrubeck commented on Apr 18, 2018

    @mbrubeck
    Contributor

    I think this is the same as #23856.

  20. alexreg commented on Oct 10, 2018

    @alexreg
    Contributor

    @mbrubeck I think so too. Have we confirmed it yet? If so, worth closing this as duplicate.

  21. alexreg commented on Nov 5, 2018

    @alexreg
    Contributor

    Okay, we have a fix! Waiting for review and merge on #55687.

  22. added a commit that references this issue on Nov 10, 2018
    0e912b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsA-trait-systemC-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions