Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | struct A { |
| 6 | A&& Pass(); |
| 7 | }; |
| 8 | |
| 9 | struct B { |
| 10 | B& Pass(); |
| 11 | }; |
| 12 | |
| 13 | struct C { |
| 14 | A a; |
| 15 | }; |
| 16 | |
| 17 | struct D { |
| 18 | D&& NotPass(); |
| 19 | }; |
| 20 | |
dcheng | fc0d822 | 2015-12-17 03:10:52 | [diff] [blame] | 21 | struct E { |
| 22 | E() : a(new A) {} |
| 23 | ~E() { delete a; } |
| 24 | A* a; |
| 25 | }; |
| 26 | |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 27 | struct F { |
| 28 | explicit F(A&&); |
| 29 | F&& Pass(); |
| 30 | }; |
| 31 | |
| 32 | void Test() { |
| 33 | // Pass that returns rvalue reference should use std::move. |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 34 | A a1; |
| 35 | A a2 = std::move(a1); |
| 36 | |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 37 | // Pass that doesn't return a rvalue reference should not be rewritten. |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 38 | B b1; |
dcheng | fc0d822 | 2015-12-17 03:10:52 | [diff] [blame] | 39 | B b2 = b1.Pass(); |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 40 | |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 41 | // std::move() needs to wrap the entire expression when passing a member. |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 42 | C c; |
| 43 | A a3 = std::move(c.a); |
| 44 | |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 45 | // Don't rewrite things that return rvalue references that aren't named Pass. |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 46 | D d1; |
| 47 | D d2 = d1.NotPass(); |
dcheng | fc0d822 | 2015-12-17 03:10:52 | [diff] [blame] | 48 | |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 49 | // Pass via a pointer type should dereference the pointer first. |
dcheng | fc0d822 | 2015-12-17 03:10:52 | [diff] [blame] | 50 | E e; |
| 51 | A a4 = std::move(*e.a); |
dcheng | 4742858 | 2015-12-17 22:19:41 | [diff] [blame] | 52 | |
| 53 | // Nested Pass() is handled correctly. |
| 54 | A a5; |
| 55 | F f = std::move(F(std::move(a5))); |
| 56 | |
| 57 | // Chained Pass is handled (mostly) correctly. The replacement applier dedupes |
| 58 | // the insertion of std::move, so the result is not completely correct... |
| 59 | // ... but hopefully there's very little code following this broken pattern. |
| 60 | A a6; |
| 61 | A a7 = std::move(a6)); |
Daniel Cheng | 2d022200 | 2015-12-05 04:45:28 | [diff] [blame] | 62 | } |