Skip to content

Can't use bound lifetime parameter 'self on &self in trait impl #8355

@lilyball

Description

@lilyball

A lifetime parameter declared on the impl line of a trait cannot be used on &self in the function impls. This is rather problematic when the lifetime parameter is needed as part of the trait type parameters.

Here's an example:

pub struct Repeat<T> {
    priv val: T
}

impl<'self, T> Iterator<&'self T> for Repeat<T> {
    #[inline]
    fn next(&'self mut self) -> Option<&'self T> {
        Some(&self.val)
    }
}

impl<T> Repeat<T> {
    #[inline]
    fn new(val: T) -> Repeat<T> {
        Repeat{val: val}
    }
}

Compiling this gives the error

Untitled.rs:7:4: 9:5 error: method `next` has an incompatible type: expected concrete lifetime, but found bound lifetime parameter &
Untitled.rs:7     fn next(&'self mut self) -> Option<&'self T> {
Untitled.rs:8         Some(&self.val)
Untitled.rs:9     }
Untitled.rs:7:49: 9:5 note: expected concrete lifetime is the lifetime &'self  as defined on the block at 7:49
Untitled.rs:7     fn next(&'self mut self) -> Option<&'self T> {
Untitled.rs:8         Some(&self.val)
Untitled.rs:9     }
error: aborting due to previous error

Removing the 'self on &'self mut self also doesn't work. Nor does trying to use fn next<'r>(&'r mut self) ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions