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

Hidden lifetime in return types containing trait parameters #22606

Closed
mahkoh opened this issue Feb 20, 2015 · 4 comments
Closed

Hidden lifetime in return types containing trait parameters #22606

mahkoh opened this issue Feb 20, 2015 · 4 comments

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Feb 20, 2015

trait Y { fn f(&self) { } }

struct X;

impl Y for X { }

struct Z<Trait: ?Sized> {
    _data: std::marker::PhantomData<Trait>,
}

fn f<Y: ?Sized>(y: &Y) -> Z<Y> {
    Z { _data: std::marker::PhantomData }
}

fn main() {
    {
        let arc = std::sync::Arc::new(X);
        f(&*arc as &Y)
    };
}
test4.rs:18:13: 18:16 error: `arc` does not live long enough
test4.rs:18         f(&*arc as &Y)
                        ^~~
@mahkoh
Copy link
Contributor Author

mahkoh commented Feb 20, 2015

Worked on monday.

@huonw
Copy link
Member

huonw commented Feb 20, 2015

#22230 and/or #21972. This works:

trait Y { fn f(&self) { } }

struct X;

impl Y for X { }

struct Z<Trait: ?Sized> {
    _data: std::marker::PhantomData<Trait>,
}

fn f<Y: ?Sized>(y: &Y) -> Z<Y> {
    Z { _data: std::marker::PhantomData }
}

fn main() {
    {
        let arc = std::sync::Arc::new(X);
        f(&*arc as &(Y+'static))
    };
}

@mahkoh
Copy link
Contributor Author

mahkoh commented Feb 20, 2015

Ah, I tried this with &'a (Y+'b) but it didn't work. Using 'static seems to solve the problem.

@mahkoh
Copy link
Contributor Author

mahkoh commented Feb 20, 2015

It's a bit surprising that the lifetimes cannot be specified in the function signature, e.g.

fn f<'a, Y: ?Sized+'static>(y: &'a Y) -> Z<Y> {
    Z { _data: std::marker::PhantomData }
}

alone does not solve the problem. But once you know that you have to specify the 'static in the as it's clear.

@mahkoh mahkoh closed this as completed Feb 20, 2015
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