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

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

Closed
pnkfelix opened this issue Dec 2, 2013 · 3 comments · Fixed by #17061
Closed

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

pnkfelix opened this issue Dec 2, 2013 · 3 comments · Fixed by #17061
Labels
A-lifetimes Area: lifetime related E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Dec 2, 2013

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

@nikomatsakis
Copy link
Contributor

Basically yes. Not sure of status of code, have to recheck.

@pnkfelix
Copy link
Member Author

((there was a comment here on a potentially related bug; however. further exploration/reduction led me to conclude that the other bug was orthogonal to this one. Therefore I have transcribed this comment into the description for #10902 and removed it from here.))

@treeman
Copy link
Contributor

treeman commented Sep 2, 2014

Code snippet from OP now compiles.

$ rustc -v
rustc 0.12.0-pre (f6a7ab40e 2014-08-29 08:21:26 +0000)

Can close.

nathantypanski added a commit to nathantypanski/rust that referenced this issue Sep 7, 2014
This test verifies that casting from the same lifetime on a value
to the same lifetime on a trait succeeds. Closes rust-lang#10766.
flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2023
needless_bool: do not simplify code if it loses comments

Fix rust-lang#10710

changelog: [`needless_bool`]: do not simplify code if it loses comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants