On 2016-04-28 8:00 AM, Gerald Squelart wrote: > On Thursday, April 28, 2016 at 9:35:56 AM UTC+10, Kyle Huey wrote: >> Can we catch this pattern with a compiler somehow? >> >> Foo foo; >> foo.x = thing; >> DoBar(mozilla::Move(foo)); >> if (foo.x) { /* do stuff */ }
I think so. We already have an analysis which would detect whether the return value of a function is used somewhere or not. We should be able to reuse that to find the call to DoBar(), and then look for future occurrences of foo used as an rvalue in the rest of the function. Once we detect a use of "foo" as an lvalue, further usages of it as an rvalue in the same function should be OK and not trigger the error. File a bug? > Definitely something that would be nice. > > But if we have/implement such a catcher, I'd like to have an annotation to > say "yep I really want to reuse this moved-from object". > Because sometimes the function will choose not to actually move from an > rvalue-ref, or the object knows to revert to a fully-reusable state, etc. What you're describing sounds like a violation of move semantics, right? The first case should only happen if DoBar doesn't accept an rvalue reference, in which case the code above is definitely doing something that the author did not expect, given that they have used Move(). The latter case sounds completely broken, and if there is an actual good use case for it, the C++ move semantics sound like the wrong tool to achieve that goal to me. If you feel like I'm missing something or you can make a strong argument on why breaking move semantics is OK in some cases, please let me know. :-) Cheers, Ehsan _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform