Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %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() { |
Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 9 | return 0; // expected-warning {{returning reference to local temporary}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 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 | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 24 | typedef void (fun_type)(); |
| 25 | void fun(); |
| 26 | fun_type &&make_fun(); |
| 27 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 28 | void f() { |
| 29 | int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}} |
| 30 | int &&virr2 = 0; |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 31 | int &&virr3 = virr2; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 32 | int i1 = 0; |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 33 | int &&virr4 = i1; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 34 | int &&virr5 = ret_irr(); |
Sebastian Redl | 157be83 | 2009-03-22 22:30:06 +0000 | [diff] [blame] | 35 | int &&virr6 = static_cast<int&&>(i1); |
| 36 | (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}} |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 37 | |
| 38 | int i2 = over(i1); |
| 39 | not_int ni1 = over(0); |
| 40 | int i3 = over(virr2); |
| 41 | not_int ni2 = over(ret_irr()); |
| 42 | |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 43 | int i4 = over2(i1); |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 44 | not_int ni3 = over2(0); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 45 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 46 | ilr_c1 vilr1 = i1; |
| 47 | ilr_c2 vilr2 = i1; |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 48 | |
| 49 | conv_to_not_int_rvalue cnir; |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 50 | not_int &&ni4 = cnir; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 51 | not_int &ni5 = cnir; // expected-error{{non-const lvalue reference to type 'not_int' cannot bind to a value of unrelated type 'conv_to_not_int_rvalue'}} |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 52 | not_int &&ni6 = conv_to_not_int_rvalue(); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 53 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 54 | fun_type &&fun_ref = fun; // works because functions are special |
| 55 | fun_type &&fun_ref2 = make_fun(); // same |
| 56 | fun_type &fun_lref = make_fun(); // also special |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 57 | |
| 58 | try { |
| 59 | } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}} |
| 60 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 61 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 62 | |
| 63 | int&& should_warn(int i) { |
| 64 | // FIXME: The stack address return test doesn't reason about casts. |
| 65 | return static_cast<int&&>(i); // xpected-warning {{returning reference to temporary}} |
| 66 | } |
| 67 | int&& should_not_warn(int&& i) { // But GCC 4.4 does |
| 68 | return static_cast<int&&>(i); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // Test the return dance. This also tests IsReturnCopyElidable. |
| 73 | struct MoveOnly { |
| 74 | MoveOnly(); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 75 | MoveOnly(const MoveOnly&) = delete; // expected-note {{candidate constructor}} \ |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 76 | // expected-note 3{{explicitly marked deleted here}} |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 77 | MoveOnly(MoveOnly&&); // expected-note {{candidate constructor}} |
| 78 | MoveOnly(int&&); // expected-note {{candidate constructor}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 81 | MoveOnly gmo; |
| 82 | MoveOnly returningNonEligible() { |
| 83 | int i; |
| 84 | static MoveOnly mo; |
| 85 | MoveOnly &r = mo; |
| 86 | if (0) // Copy from global can't be elided |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 87 | return gmo; // expected-error {{call to deleted constructor}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 88 | else if (0) // Copy from local static can't be elided |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 89 | return mo; // expected-error {{call to deleted constructor}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 90 | else if (0) // Copy from reference can't be elided |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 91 | return r; // expected-error {{call to deleted constructor}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 92 | else // Construction from different type can't be elided |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 93 | return i; // expected-error {{no viable conversion from 'int' to 'MoveOnly'}} |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 94 | } |