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