Rust: Incorrect "associated type must be specified" for trait objects

Created on 30 Mar 2015  Â·  11Comments  Â·  Source: rust-lang/rust

Code:

trait MyIterator: Iterator<Item = char> {}

impl<T: ?Sized + Iterator<Item=char> + 'static> MyIterator for T {}

struct TestStruct {
    data: Box<MyIterator + 'static>,
}

fn new_struct(string: &'static str) -> TestStruct {
    TestStruct {
        data: Box::new(string.chars()) as Box<MyIterator + 'static>,
        //also does not compile
        //data: Box::new(string.chars()) as Box<MyIterator<Item=char> + 'static>,
    }
}

fn main() {}

Error:

<anon>:11:15: 11:39 error: the value of the associated type `Item` (from the trait `core::iter::Iterator`) must be specified [E0191]
<anon>:11         data: Box::new(string.chars()) as Box<MyIterator + 'static>,
                        ^~~~~~~~~~~~~~~~~~~~~~~~

playpen

I expect this code to compile without errors.

EDIT

This is the error on the current version:

   Compiling playground v0.0.1 (file:///playground)
error[E0191]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) must be specified
 --> src/main.rs:6:15
  |
6 |     data: Box<MyIterator + 'static>,
  |               ^^^^^^^^^^^^^^^^^^^^ missing associated type `Item` value

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Changing line 6 to data: Box<MyIterator<Item=char> + 'static>, moves the error to line 11:

   Compiling playground v0.0.1 (file:///playground)
error[E0191]: the value of the associated type `Item` (from the trait `std::iter::Iterator`) must be specified
  --> src/main.rs:11:47
   |
11 |         data: Box::new(string.chars()) as Box<MyIterator + 'static>,
   |                                               ^^^^^^^^^^^^^^^^^^^^ missing associated type `Item` value

error[E0308]: mismatched types
  --> src/main.rs:11:15
   |
11 |         data: Box::new(string.chars()) as Box<MyIterator + 'static>,
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `MyIterator<Item=char>`, found trait `MyIterator`
   |
   = note: expected type `std::boxed::Box<MyIterator<Item=char> + 'static>`
              found type `std::boxed::Box<MyIterator + 'static>`
   = help: here are some functions which might fulfill your needs:
           - .collect()
           - .product()
           - .sum()

error: aborting due to 2 previous errors

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

However, commenting line 11 and uncommenting line 13 makes the code compile fine.

A-associated-items C-bug

Most helpful comment

Just ran into this bug while trying to mix Box with Stream and Sink supertraits from futures crate. Very annoying!

All 11 comments

Very strange. I'd expect the Item=char version to compile.

(comment copied from duplicate #36978 for locality purposes, since it contains a more-M WE)

Consider the following:

trait Foo { type FooT; }
trait Bar { }
trait FooBar: Foo<FooT=usize> + Bar { }
type BoxedFooBar = Box<FooBar>;

Since the value of FooT is known from the definition of FooBar, line 4 should be fine — if FB: FooBar then necessarily FB::FooT = usize. However, this fails to compile with rustc 1.12.0, with the error:

error[E0191]: the value of the associated type `FooT` (from the trait `Foo`) must be specified
 --> <anon>:4:24
  |
4 | type BoxedFooBar = Box<FooBar>;
  |                        ^^^^^^ missing associated type `FooT` value

To make it compile, one must duplicate the information at every use-site:

trait Foo { type FooT; }
trait Bar { }
trait FooBar: Foo<FooT=usize> + Bar { }
type BoxedFooBar = Box<FooBar<FooT=usize>>;

Am I looking at an example of this, or something else entirely? Playground example; that fails to compile with the same error as above in this thread.

(Note that this example contains an error on my part: I intended to use the template parameter C, and write &mut C, and not have that trait object. If I correct that, it does compile.)

Yes, this is the same thing.

Just ran into this today. As mentioned by @thanatos and in some linked duplicates, it appears to only impact trait objects. (if it impacted generic type parameters, I think there would be a lot more fuss over it!)


trait MyFunc: FnMut() {}
impl<F:FnMut()> MyFunc for F {}

// OK
fn takes_generic<F: MyFunc>(mut f: F) { f(); }
// Error: missing associated type `Output` value
fn takes_trait_object(f: &mut MyFunc) { f(); }

fn main() {}

Changing the trait object to a generic type parameter is a workaround in many, but not all, use cases.

Just ran into this bug while trying to mix Box with Stream and Sink supertraits from futures crate. Very annoying!

I ran into the same bug, in the same situation like @vorot93.

I ran into that problem too, it's quite annoying. I hope a fix will pop soon.

Any progress on this lately?

I think this issue has been resolved as of PR #55687, correct? The linked playgrounds no longer fail to compile, at least on a cursory inspection. Could we close this issue out?

Looks fixed to me.

Was this page helpful?
0 / 5 - 0 ratings