xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/rval-references.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc typedef int&& irr;
4*f4a2713aSLionel Sambuc typedef irr& ilr_c1; // Collapses to int&
5*f4a2713aSLionel Sambuc typedef int& ilr;
6*f4a2713aSLionel Sambuc typedef ilr&& ilr_c2; // Collapses to int&
7*f4a2713aSLionel Sambuc 
ret_irr()8*f4a2713aSLionel Sambuc irr ret_irr() {
9*f4a2713aSLionel Sambuc   return 0; // expected-warning {{returning reference to local temporary}}
10*f4a2713aSLionel Sambuc }
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc struct not_int {};
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc int over(int&);
15*f4a2713aSLionel Sambuc not_int over(int&&);
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc int over2(const int&);
18*f4a2713aSLionel Sambuc not_int over2(int&&);
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc struct conv_to_not_int_rvalue {
21*f4a2713aSLionel Sambuc   operator not_int &&();
22*f4a2713aSLionel Sambuc };
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc typedef void (fun_type)();
25*f4a2713aSLionel Sambuc void fun();
26*f4a2713aSLionel Sambuc fun_type &&make_fun();
27*f4a2713aSLionel Sambuc 
f()28*f4a2713aSLionel Sambuc void f() {
29*f4a2713aSLionel Sambuc   int &&virr1; // expected-error {{declaration of reference variable 'virr1' requires an initializer}}
30*f4a2713aSLionel Sambuc   int &&virr2 = 0;
31*f4a2713aSLionel Sambuc   int &&virr3 = virr2; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
32*f4a2713aSLionel Sambuc   int i1 = 0;
33*f4a2713aSLionel Sambuc   int &&virr4 = i1; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
34*f4a2713aSLionel Sambuc   int &&virr5 = ret_irr();
35*f4a2713aSLionel Sambuc   int &&virr6 = static_cast<int&&>(i1);
36*f4a2713aSLionel Sambuc   (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}}
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   int i2 = over(i1);
39*f4a2713aSLionel Sambuc   not_int ni1 = over(0);
40*f4a2713aSLionel Sambuc   int i3 = over(virr2);
41*f4a2713aSLionel Sambuc   not_int ni2 = over(ret_irr());
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc   int i4 = over2(i1);
44*f4a2713aSLionel Sambuc   not_int ni3 = over2(0);
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc   ilr_c1 vilr1 = i1;
47*f4a2713aSLionel Sambuc   ilr_c2 vilr2 = i1;
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc   conv_to_not_int_rvalue cnir;
50*f4a2713aSLionel Sambuc   not_int &&ni4 = cnir;
51*f4a2713aSLionel Sambuc   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'}}
52*f4a2713aSLionel Sambuc   not_int &&ni6 = conv_to_not_int_rvalue();
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   fun_type &&fun_ref = fun; // works because functions are special
55*f4a2713aSLionel Sambuc   fun_type &&fun_ref2 = make_fun(); // same
56*f4a2713aSLionel Sambuc   fun_type &fun_lref = make_fun(); // also special
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   try {
59*f4a2713aSLionel Sambuc   } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}}
60*f4a2713aSLionel Sambuc   }
61*f4a2713aSLionel Sambuc }
62*f4a2713aSLionel Sambuc 
should_warn(int i)63*f4a2713aSLionel Sambuc int&& should_warn(int i) {
64*f4a2713aSLionel Sambuc   // FIXME: The stack address return test doesn't reason about casts.
65*f4a2713aSLionel Sambuc   return static_cast<int&&>(i); // xpected-warning {{returning reference to temporary}}
66*f4a2713aSLionel Sambuc }
should_not_warn(int && i)67*f4a2713aSLionel Sambuc int&& should_not_warn(int&& i) { // But GCC 4.4 does
68*f4a2713aSLionel Sambuc   return static_cast<int&&>(i);
69*f4a2713aSLionel Sambuc }
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc // Test the return dance. This also tests IsReturnCopyElidable.
73*f4a2713aSLionel Sambuc struct MoveOnly {
74*f4a2713aSLionel Sambuc   MoveOnly();
75*f4a2713aSLionel Sambuc   MoveOnly(const MoveOnly&) = delete;	// expected-note {{candidate constructor}} \
76*f4a2713aSLionel Sambuc   // expected-note 3{{explicitly marked deleted here}}
77*f4a2713aSLionel Sambuc   MoveOnly(MoveOnly&&);	// expected-note {{candidate constructor}}
78*f4a2713aSLionel Sambuc   MoveOnly(int&&);	// expected-note {{candidate constructor}}
79*f4a2713aSLionel Sambuc };
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc MoveOnly gmo;
returningNonEligible()82*f4a2713aSLionel Sambuc MoveOnly returningNonEligible() {
83*f4a2713aSLionel Sambuc   int i;
84*f4a2713aSLionel Sambuc   static MoveOnly mo;
85*f4a2713aSLionel Sambuc   MoveOnly &r = mo;
86*f4a2713aSLionel Sambuc   if (0) // Copy from global can't be elided
87*f4a2713aSLionel Sambuc     return gmo; // expected-error {{call to deleted constructor}}
88*f4a2713aSLionel Sambuc   else if (0) // Copy from local static can't be elided
89*f4a2713aSLionel Sambuc     return mo; // expected-error {{call to deleted constructor}}
90*f4a2713aSLionel Sambuc   else if (0) // Copy from reference can't be elided
91*f4a2713aSLionel Sambuc     return r; // expected-error {{call to deleted constructor}}
92*f4a2713aSLionel Sambuc   else // Construction from different type can't be elided
93*f4a2713aSLionel Sambuc     return i; // expected-error {{no viable conversion from 'int' to 'MoveOnly'}}
94*f4a2713aSLionel Sambuc }
95