1// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s
2
3typedef int&& irr;
4typedef irr& ilr_c1; // Collapses to int&
5typedef int& ilr;
6typedef ilr&& ilr_c2; // Collapses to int&
7
8irr ret_irr() {
9  return 0; // expected-warning {{returning reference to local temporary}}
10}
11
12struct not_int {};
13
14int over(int&);
15not_int over(int&&);
16
17int over2(const int&);
18not_int over2(int&&);
19
20struct conv_to_not_int_rvalue {
21  operator not_int &&();
22};
23
24typedef void (fun_type)();
25void fun();
26fun_type &&make_fun();
27
28void 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  int &&virr4 = i1; // expected-error {{rvalue reference to type 'int' cannot bind to lvalue of type 'int'}}
34  int &&virr5 = ret_irr();
35  int &&virr6 = static_cast<int&&>(i1);
36  (void)static_cast<not_int&&>(i1); // expected-error {{types are not compatible}}
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
43  int i4 = over2(i1);
44  not_int ni3 = over2(0);
45
46  ilr_c1 vilr1 = i1;
47  ilr_c2 vilr2 = i1;
48
49  conv_to_not_int_rvalue cnir;
50  not_int &&ni4 = cnir;
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'}}
52  not_int &&ni6 = conv_to_not_int_rvalue();
53
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
57
58  try {
59  } catch(int&&) { // expected-error {{cannot catch exceptions by rvalue reference}}
60  }
61}
62
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();
75  MoveOnly(const MoveOnly&) = delete;	// expected-note {{candidate constructor}} \
76  // expected-note 3{{explicitly marked deleted here}}
77  MoveOnly(MoveOnly&&);	// expected-note {{candidate constructor}}
78  MoveOnly(int&&);	// expected-note {{candidate constructor}}
79};
80
81MoveOnly gmo;
82MoveOnly returningNonEligible() {
83  int i;
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 if (0) // Copy from reference can't be elided
91    return r; // expected-error {{call to deleted constructor}}
92  else // Construction from different type can't be elided
93    return i; // expected-error {{no viable conversion from 'int' to 'MoveOnly'}}
94}
95