Skip to content
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

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

Closed
phlopsi opened this issue Dec 5, 2019 · 1 comment
Closed

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

phlopsi opened this issue Dec 5, 2019 · 1 comment

Comments

@phlopsi
Copy link
Contributor

phlopsi commented Dec 5, 2019

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.

@jonas-schievink
Copy link
Contributor

Duplicate of #34426, will be fixed by #67059

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

No branches or pull requests

2 participants