Skip to content

Impossible to Specify Appropriate Lifetime with for<'a> &'a ... #67067

Closed
@phlopsi

Description

@phlopsi

I had asked for help on the Rust user forum, but from what I've learned, Rust provides no way to specify lifetimes in a way to succesfully run the loop in the drop-method in the following example:

struct MyStruct<TCollection>
where
    for<'a> &'a TCollection: IntoIterator<Item = &'a ()>,
{
    references: TCollection,
}

impl<TCollection> Drop for MyStruct<TCollection>
where
    for<'b> &'b TCollection: IntoIterator<Item = &'b ()>,
{
    fn drop(&mut self) {
        for _ in &self.references {}
    }
}

fn main() {
    let _ = MyStruct {
        references: Vec::new(),
    };
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0367]: The requirement `for<'b> <&'b TCollection as std::iter::IntoIterator>::Item == &'b ()` is added only by the Drop impl.
  --> src/lib.rs:8:1
   |
8  | / impl<TCollection> Drop for MyStruct<TCollection>
9  | | where
10 | |     for<'b> &'b TCollection: IntoIterator<Item = &'b ()>,
11 | | {
...  |
14 | |     }
15 | | }
   | |_^
   |
note: The same requirement must be part of the struct/enum definition
  --> src/lib.rs:1:1
   |
1  | / struct MyStruct<TCollection>
2  | | where
3  | |     for<'a> &'a TCollection: IntoIterator<Item = &'a ()>,
4  | | {
5  | |     references: TCollection,
6  | | }
   | |_^

error[E0367]: The requirement `for<'b> &'b TCollection: std::iter::IntoIterator` is added only by the Drop impl.
  --> src/lib.rs:8:1
   |
8  | / impl<TCollection> Drop for MyStruct<TCollection>
9  | | where
10 | |     for<'b> &'b TCollection: IntoIterator<Item = &'b ()>,
11 | | {
...  |
14 | |     }
15 | | }
   | |_^
   |
note: The same requirement must be part of the struct/enum definition
  --> src/lib.rs:1:1
   |
1  | / struct MyStruct<TCollection>
2  | | where
3  | |     for<'a> &'a TCollection: IntoIterator<Item = &'a ()>,
4  | | {
5  | |     references: TCollection,
6  | | }
   | |_^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0367`.
error: could not compile `playground`.

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

Rust should be able to determine, that the where-clause in the struct and the Drop-impl are compatible. I consider this a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions