-
Notifications
You must be signed in to change notification settings - Fork 13k
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
RFC: Return by move #3413
Comments
To write return by copyin cases where this "might" be confusing, you already have to write out the copy, right? i can't think of any other conflicts; this sounds fine to me. |
So to return a value we could either write:
or
This seems ok to me. |
@brson Yes, either would be correct. |
I was considering a similar rule but that was syntactically determined. Something like:
I feel strongly that the rule should be the same for blocks and returns. The idea of generalizing to an arbitrary owned expression is also plausible, though it means that we won't know if something is a move or not until borrowck. That's probably ok. |
Under my proposal, then,
would always be entirely equivalent to
and also to:
and so forth. I think it is important to main this invariant. One problem I can see, though, is that |
@nikomatsakis Yes, I agree that tail exprs and explicit returns should be handled the same way. I just forgot to mention that. |
I'm mostly in agreement (tentatively, not really knowing the complications) with Niko in terms of treating the tail position, in general, as moved-out rather than copied-out. It's not "running a liveness algorithm" to figure out which expression is the tail expression of a block or function: it's a straight syntactic feature, known the moment we parse. Honestly, if you can't figure that out by inspection, you have bigger problems in your code! (Though I don't understand why |
Oh. I guess there might be some complication here in the region-inference bit Niko and I were discussing, to lengthen rvalue lifetimes from inside blocks. Though I struggle to come up with an example (especially one borrowck wouldn't catch). Am I barking up a meaningful tree here? |
I guess this is OK. |
@graydon that's not quite what I meant. I just meant that if we are not careful with how we define the rules, then So the rule has to be more subtle and consider the "destination" of the copy. Something like: "a reference to a local variable is an implicit move if the destination will be out of the variable's scope", but of course we need to define "destination". Essentially it seems like a top-down sort of analysis to me, where each block has a notion of its "destination", with the fn body and any block that appears after a return having a destination of "outside the fn" (in terms of the implementation, perhaps the node id 0). Internal blocks have a destination of the expression that they are a part of. When looking at a block whose tail expression is a local variable, we consider the block's destination id and compare it against the id of the block where the variable was declared. This can be generalized in a fairly straightforward way to arbitrary "owned" expressions, if that seems worth the trouble. |
In the meeting today, we agreed not to implement this after all, for simplicity. It's not that bad to have to write explicit |
phase_rustdoc: add a heuristic to make us more certain that this is really rustdoc Also add anyhow to test-cargo-miri; it has a custom build probe and is widely used so let's make sure the build script does not fail.
In changing code to prepare for #2633, I'm noticing that most of the "copying a noncopyable" errors when I disable last-use have to do with a
return
, usually a return of a local variable. I propose that areturn x
should, by default, meanreturn (move x)
whenx
is an owned lvalue. This avoids the need to insert lots of explicitmove
s once last-use is disabled.An argument against this proposal is that the point of #2633 is to make more things explicit, and this proposal makes more things implicit again. However, I think it's easier to understand that "return is move" when you're returning something you own than it is to understand how last-use works.
The text was updated successfully, but these errors were encountered: