Skip to content

Should &'a Value as &'a Trait work? #10766

Closed
@pnkfelix

Description

@pnkfelix

Inspired by rust-dev posting:
https://mail.mozilla.org/pipermail/rust-dev/2013-November/007054.html

Here is the relevant bit of code from the post:

trait T {}

fn f<'a, V: T>(v: &'a V) -> &'a T {
    v as &'a T
}

We need to ensure that for an expression <source> as <target> that any borrowed pointers in the type of are not obscured (#5723) by the cast.

A �collection of conditions sufficient to enforce this are listed in a comment in librustc/middle/kind.rs that I think is apropos here:

https://github.com/mozilla/rust/blob/master/src/librustc/middle/kind.rs#L488

However, there are probably other conditions that would also suffice that we might add to that set.

In particular, I do not see anything immediately wrong with the example from that mailing list post; the type-expression &'a V should ensure that V does not contain any lifetimes that are shorter than 'a, and therefore it should be safe, when <V: T> to cast v: &'a V to a &'a T.

(It could be that I have misinterpreted the constraints imposed by `&'a V.)

cc: @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions