blob: fc341e87f4a404c67dc68dcecffbee0ebcee1e71 [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002
3typedef int&& irr;
4typedef irr& ilr_c1; // Collapses to int&
5typedef int& ilr;
6typedef ilr&& ilr_c2; // Collapses to int&
7
8irr ret_irr() {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00009 return 0; // expected-warning {{returning reference to local temporary}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000010}
11
12struct not_int {};
13
14int over(int&);
15not_int over(int&&);
16
Douglas Gregor2ff44782009-03-20 20:21:37 +000017int over2(const int&);
18not_int over2(int&&);
19
20struct conv_to_not_int_rvalue {
21 operator not_int &&();
22};
23
Sebastian Redl4680bf22010-06-30 18:13:39 +000024typedef void (fun_type)();
25void fun();
26fun_type &&make_fun();
27
Sebastian Redl7c80bd62009-03-16 23:22:08 +000028void f() {
29 int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}}
30 int &&virr2 = 0;
Douglas Gregorfb5d7ef2011-01-21 01:04:33 +000031 int &&virr3 = virr2; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000032 int i1 = 0;
Douglas Gregorfb5d7ef2011-01-21 01:04:33 +000033 int &&virr4 = i1; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000034 int &&virr5 = ret_irr();
Sebastian Redl157be832009-03-22 22:30:06 +000035 int &&virr6 = static_cast<int&&>(i1);
36 (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000037
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 Gregor2ff44782009-03-20 20:21:37 +000043 int i4 = over2(i1);
Sebastian Redla9845802009-03-29 15:27:50 +000044 not_int ni3 = over2(0);
Douglas Gregor2ff44782009-03-20 20:21:37 +000045
Sebastian Redl7c80bd62009-03-16 23:22:08 +000046 ilr_c1 vilr1 = i1;
47 ilr_c2 vilr2 = i1;
Douglas Gregor2ff44782009-03-20 20:21:37 +000048
49 conv_to_not_int_rvalue cnir;
Douglas Gregorb2855ad2011-01-21 00:52:42 +000050 not_int &&ni4 = cnir;
John McCall7c2342d2010-03-10 11:27:22 +000051 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 Redla9845802009-03-29 15:27:50 +000052 not_int &&ni6 = conv_to_not_int_rvalue();
Sebastian Redlf2e21e52009-03-22 23:49:27 +000053
Sebastian Redl4680bf22010-06-30 18:13:39 +000054 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 Redlf2e21e52009-03-22 23:49:27 +000057
58 try {
59 } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}}
60 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +000061}
Sebastian Redle2b68332009-04-12 17:16:29 +000062
63int&& 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}
67int&& 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.
73struct MoveOnly {
74 MoveOnly();
John McCall81201622010-01-08 04:41:39 +000075 MoveOnly(const MoveOnly&) = delete; // expected-note {{candidate constructor}} \
Douglas Gregor18ef5e22009-12-18 05:02:21 +000076 // expected-note 3{{explicitly marked deleted here}}
John McCallb1622a12010-01-06 09:43:14 +000077 MoveOnly(MoveOnly&&); // expected-note {{candidate constructor}}
78 MoveOnly(int&&); // expected-note {{candidate constructor}}
Sebastian Redle2b68332009-04-12 17:16:29 +000079};
80
Sebastian Redle2b68332009-04-12 17:16:29 +000081MoveOnly gmo;
82MoveOnly returningNonEligible() {
83 int i;
84 static MoveOnly mo;
85 MoveOnly &r = mo;
86 if (0) // Copy from global can't be elided
Douglas Gregor18ef5e22009-12-18 05:02:21 +000087 return gmo; // expected-error {{call to deleted constructor}}
Sebastian Redle2b68332009-04-12 17:16:29 +000088 else if (0) // Copy from local static can't be elided
Douglas Gregor18ef5e22009-12-18 05:02:21 +000089 return mo; // expected-error {{call to deleted constructor}}
Sebastian Redle2b68332009-04-12 17:16:29 +000090 else if (0) // Copy from reference can't be elided
Douglas Gregor18ef5e22009-12-18 05:02:21 +000091 return r; // expected-error {{call to deleted constructor}}
Sebastian Redle2b68332009-04-12 17:16:29 +000092 else // Construction from different type can't be elided
John McCall7c2342d2010-03-10 11:27:22 +000093 return i; // expected-error {{no viable conversion from 'int' to 'MoveOnly'}}
Sebastian Redle2b68332009-04-12 17:16:29 +000094}