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