Skip to content

Incorrect "associated type must be specified" for trait objects #23856

Closed
@theemathas

Description

@theemathas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions