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

Lifetime error with associated types, methods, and type aliases. #22356

Closed
brendanzab opened this issue Feb 15, 2015 · 1 comment · Fixed by #22620
Closed

Lifetime error with associated types, methods, and type aliases. #22356

brendanzab opened this issue Feb 15, 2015 · 1 comment · Fixed by #22620
Labels
A-associated-items Area: Associated items (types, constants & functions) A-borrow-checker Area: The borrow checker

Comments

@brendanzab
Copy link
Member

Sorry, I couldn't think up a better name for this issue. It popped up in gfx-rs/gfx#564.

pub struct Handle<T, I>(T, I);

impl<T, I> Handle<T, I> {
    pub fn get_info(&self) -> &I {
        let Handle(_, ref info) = *self;
        info
    }
}

pub struct BufferHandle<D: Device, T> {
    raw: RawBufferHandle<D>,
}

impl<D: Device, T> BufferHandle<D, T> {
    pub fn get_info(&self) -> &String {
        self.raw.get_info()
    }
}

pub type RawBufferHandle<D: Device> = Handle<<D as Device>::Buffer, String>;

pub trait Device {
    type Buffer;
}

<anon>:16:9: 16:17 error: the associated type `<D as Device>::Buffer` may not live long enough [E0311]
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
<anon>:16:9: 16:17 help: consider adding an explicit lifetime bound for `<D as Device>::Buffer`
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
<anon>:15:39: 17:6 note: the associated type `<D as Device>::Buffer` must be valid for the anonymous lifetime #1 defined on the block at 15:38...
<anon>:15     pub fn get_info(&self) -> &String {
<anon>:16         self.raw.get_info()
<anon>:17     }
<anon>:16:9: 16:17 note: ...so that the reference type `&Handle<<D as Device>::Buffer, collections::string::String>` does not outlive the data it points at
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
<anon>:16:9: 16:17 error: the associated type `<D as Device>::Buffer` may not live long enough [E0311]
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
<anon>:16:9: 16:17 help: consider adding an explicit lifetime bound for `<D as Device>::Buffer`
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
<anon>:15:39: 17:6 note: the associated type `<D as Device>::Buffer` must be valid for the anonymous lifetime #1 defined on the block at 15:38...
<anon>:15     pub fn get_info(&self) -> &String {
<anon>:16         self.raw.get_info()
<anon>:17     }
<anon>:16:9: 16:17 note: ...so that the reference type `&Handle<<D as Device>::Buffer, collections::string::String>` does not outlive the data it points at
<anon>:16         self.raw.get_info()
                  ^~~~~~~~
error: aborting due to 2 previous errors

Moving the implementation of Handle::get_info into BufferHandle::get_info seems to fix the problem:

impl<D: Device, T> BufferHandle<D, T> {
    pub fn get_info(&self) -> &String {
        let Handle(_, ref info) = self.raw;
        info
    }
}
@brendanzab brendanzab added A-borrow-checker Area: The borrow checker A-associated-items Area: Associated items (types, constants & functions) labels Feb 15, 2015
brendanzab added a commit to brendanzab/gfx-rs that referenced this issue Feb 15, 2015
@japaric
Copy link
Member

japaric commented Feb 15, 2015

This looks like #22246. cc @nikomatsakis

brendanzab added a commit to brendanzab/gfx-rs that referenced this issue Feb 17, 2015
edwardw added a commit to edwardw/rust that referenced this issue Feb 21, 2015
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-borrow-checker Area: The borrow checker
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants