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

return x; and x not equivalent at end of function? #43837

Closed
lheckemann opened this issue Aug 13, 2017 · 5 comments
Closed

return x; and x not equivalent at end of function? #43837

lheckemann opened this issue Aug 13, 2017 · 5 comments

Comments

@lheckemann
Copy link

I have some code which the borrow checker doesn't agree with. However, it works fine if I replace the expression at the end of the function with a return statement containing the same expression. I believed that return x; was fully equivalent to x at the end of a function, but apparently not? Not sure if this is a documentation issue or a compiler bug, but after raising this on #rust-beginners it was recommended that I raise it here :)

@arielb1
Copy link
Contributor

arielb1 commented Aug 13, 2017

They are not equivalent. The destructor for possible.drain() runs earlier in the return version and later (too later) in the no-return version.

@arielb1 arielb1 closed this as completed Aug 13, 2017
@lheckemann
Copy link
Author

In that case I think this is a documentation issue. https://doc.rust-lang.org/reference/items.html#functions suggests that they are equivalent:

Every control path in a function logically ends with a return expression or a diverging expression. If the outermost block of a function has a value-producing expression in its final-expression position, that expression is interpreted as an implicit return expression applied to the final-expression.

@arielb1
Copy link
Contributor

arielb1 commented Aug 13, 2017

That paragraph is actually sort-of correct - what matters is whether there's a semicolon (making for a return statement) or not (see https://play.rust-lang.org/?gist=763b754c7393eb72d3d8b79721dba3ba&version=stable - a non-semicoloned return expression.

OTOH, that reference section is entirely a lie for several reasons. Let's fix it

@ghost
Copy link

ghost commented Aug 13, 2017

The destructor for possible.drain() runs earlier in the return version and later (too later) in the no-return version.

But why isn't this applied though?
note: values in a scope are dropped in the opposite order they are created

If it happens that the lifetimes of both 'possible' and 'possible.drain()' end at the same point(when function scope ends?), then shouldn't they be dropped in the opposite order they are created.

Why does The destructor for possible.drain() run later (too later) in the no-return version. ?

EDIT: well, nevermind this, I guess there are more pressing issues like rust-lang/rfcs#811 which may or may not affect this when fixed.

EDIT2: thanks to oli_obk for linking me to the old 2015 issue here #21114 (comment)

@ghost
Copy link

ghost commented Feb 26, 2018

the code in OP is now working by prepending the line #![feature(nll)] (Rust nightly required, currently)

now, let me just take a moment to praise the NLL gods... 🥇

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