Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2 | |
| 3 | typedef int&& irr; |
| 4 | typedef irr& ilr_c1; // Collapses to int& |
| 5 | typedef int& ilr; |
| 6 | typedef ilr&& ilr_c2; // Collapses to int& |
| 7 | |
| 8 | irr ret_irr() { |
| 9 | return 0; |
| 10 | } |
| 11 | |
| 12 | struct not_int {}; |
| 13 | |
| 14 | int over(int&); |
| 15 | not_int over(int&&); |
| 16 | |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 17 | int over2(const int&); |
| 18 | not_int over2(int&&); |
| 19 | |
| 20 | struct conv_to_not_int_rvalue { |
| 21 | operator not_int &&(); |
| 22 | }; |
| 23 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 24 | void f() { |
| 25 | int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}} |
| 26 | int &&virr2 = 0; |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 27 | int &&virr3 = virr2; // expected-error {{rvalue reference cannot bind to lvalue}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 28 | int i1 = 0; |
| 29 | int &&virr4 = i1; // expected-error {{rvalue reference cannot bind to lvalue}} |
| 30 | int &&virr5 = ret_irr(); |
Sebastian Redl | 157be83 | 2009-03-22 22:30:06 +0000 | [diff] [blame] | 31 | int &&virr6 = static_cast<int&&>(i1); |
| 32 | (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 33 | |
| 34 | int i2 = over(i1); |
| 35 | not_int ni1 = over(0); |
| 36 | int i3 = over(virr2); |
| 37 | not_int ni2 = over(ret_irr()); |
| 38 | |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 39 | int i4 = over2(i1); |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 40 | not_int ni3 = over2(0); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 41 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 42 | ilr_c1 vilr1 = i1; |
| 43 | ilr_c2 vilr2 = i1; |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 44 | |
| 45 | conv_to_not_int_rvalue cnir; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 46 | not_int &&ni4 = cnir; // expected-error {{rvalue reference cannot bind to lvalue}} |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 47 | not_int &ni5 = cnir; // expected-error{{non-const lvalue reference to type 'struct not_int' cannot bind to a value of unrelated type 'struct conv_to_not_int_rvalue'}} |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 48 | not_int &&ni6 = conv_to_not_int_rvalue(); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | try { |
| 52 | } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}} |
| 53 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 54 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 55 | |
| 56 | int&& should_warn(int i) { |
| 57 | // FIXME: The stack address return test doesn't reason about casts. |
| 58 | return static_cast<int&&>(i); // xpected-warning {{returning reference to temporary}} |
| 59 | } |
| 60 | int&& should_not_warn(int&& i) { // But GCC 4.4 does |
| 61 | return static_cast<int&&>(i); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // Test the return dance. This also tests IsReturnCopyElidable. |
| 66 | struct MoveOnly { |
| 67 | MoveOnly(); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 68 | MoveOnly(const MoveOnly&) = delete; // expected-note {{candidate function}} |
| 69 | MoveOnly(MoveOnly&&); // expected-note {{candidate function}} |
| 70 | MoveOnly(int&&); // expected-note {{candidate function}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | MoveOnly returning() { |
| 74 | MoveOnly mo; |
| 75 | return mo; |
| 76 | } |
| 77 | |
| 78 | MoveOnly gmo; |
| 79 | MoveOnly returningNonEligible() { |
| 80 | int i; |
| 81 | static MoveOnly mo; |
| 82 | MoveOnly &r = mo; |
| 83 | if (0) // Copy from global can't be elided |
| 84 | return gmo; // expected-error {{incompatible type returning}} |
| 85 | else if (0) // Copy from local static can't be elided |
| 86 | return mo; // expected-error {{incompatible type returning}} |
| 87 | else if (0) // Copy from reference can't be elided |
| 88 | return r; // expected-error {{incompatible type returning}} |
| 89 | else // Construction from different type can't be elided |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 90 | return i; // expected-error {{no viable conversion from 'int' to 'struct MoveOnly'}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 91 | } |