13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// RUN: %clang_cc1 -fsyntax-only -verify %s
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct Base { };
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct Derived : Base { }; // expected-note{{candidate constructor (the implicit copy constructor) not viable}}
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if __cplusplus >= 201103L // C++11 or later
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// expected-note@-2 {{candidate constructor (the implicit move constructor) not viable}}
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct Unrelated { };
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct Derived2 : Base { };
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct Diamond : Derived, Derived2 { };
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToBaseRef {
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Base&() const;
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToDerivedRef {
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived&() const;
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToBothDerivedRef {
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived&(); // expected-note{{candidate function}}
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived2&(); // expected-note{{candidate function}}
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToIntRef {
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator int&();
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToBase {
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Base() const;
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToDerived {
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived() const;
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToBothDerived {
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived(); // expected-note{{candidate function}}
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator Derived2(); // expected-note{{candidate function}}
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct ConvertibleToInt {
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  operator int();
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> T create();
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// First bullet: lvalue references binding to lvalues (the simple cases).
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_lvalue(Base b, Derived d,
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           const Base bc, const Derived dc,
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           Diamond diamond,
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                           int i) {
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Reference-compatible
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br1 = b;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br2 = d;
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Derived &dr1 = d;
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Derived &dr2 = b; // expected-error{{non-const lvalue reference to type 'Derived' cannot bind to a value of unrelated type 'Base'}}
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br3 = bc; // expected-error{{drops 'const' qualifier}}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br4 = dc; // expected-error{{drops 'const' qualifier}}
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br5 = diamond; // expected-error{{ambiguous conversion from derived class 'Diamond' to base class 'Base':}}
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int &ir = i;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  long &lr = i; // expected-error{{non-const lvalue reference to type 'long' cannot bind to a value of unrelated type 'int'}}
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_quals(volatile Base b, volatile Derived d,
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       volatile const Base bvc, volatile const Derived dvc,
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                       volatile const int ivc) {
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  volatile Base &bvr1 = b;
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  volatile Base &bvr2 = d;
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  volatile Base &bvr3 = bvc; // expected-error{{binding value of type 'const volatile Base' to reference to type 'volatile Base' drops 'const' qualifier}}
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  volatile Base &bvr4 = dvc; // expected-error{{binding value of type 'const volatile Derived' to reference to type 'volatile Base' drops 'const' qualifier}}
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  volatile int &ir = ivc; // expected-error{{binding value of type 'const volatile int' to reference to type 'volatile int' drops 'const' qualifier}}
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const volatile Base &bcvr1 = b;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const volatile Base &bcvr2 = d;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_rvalue() {
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br1 = Base(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Base'}}
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br2 = Derived(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Derived'}}
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const volatile Base &br3 = Base(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Base'}}
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const volatile Base &br4 = Derived(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Derived'}}
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int &ir = 17; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_unrelated(Unrelated ur) {
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br1 = ur; // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a value of unrelated type 'Unrelated'}}
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const volatile Base &br2 = ur; // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a value of unrelated type 'Unrelated'}}
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_conv_lvalue() {
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Not reference-related, but convertible
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &nbr1 = ConvertibleToBaseRef();
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &nbr2 = ConvertibleToDerivedRef();
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Derived &ndr1 = ConvertibleToDerivedRef();
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int &ir = ConvertibleToIntRef();
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_conv_lvalue_ambig(ConvertibleToBothDerivedRef both) {
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Derived &dr1 = both;
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Base &br1 = both; // expected-error{{reference initialization of type 'Base &' with initializer of type 'ConvertibleToBothDerivedRef' is ambiguous}}
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct IntBitfield {
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int i : 17; // expected-note{{bit-field is declared here}}
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid test_bitfield(IntBitfield ib) {
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int & ir1 = (ib.i); // expected-error{{non-const reference cannot bind to bit-field 'i'}}
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Second bullet: const lvalue reference binding to an rvalue with
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// similar type (both of which are class types).
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_const_lvalue_to_rvalue() {
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br1 = create<Base>();
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br2 = create<Derived>();
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Derived &dr1 = create<Base>(); // expected-error{{no viable conversion}}
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br3 = create<const Base>();
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br4 = create<const Derived>();
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br5 = create<const volatile Base>(); // expected-error{{binding value of type 'const volatile Base' to reference to type 'const Base' drops 'volatile' qualifier}}
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br6 = create<const volatile Derived>(); // expected-error{{binding value of type 'const volatile Derived' to reference to type 'const Base' drops 'volatile' qualifier}}
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const int &ir = create<int>();
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Second bullet: const lvalue reference binds to the result of a conversion.
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_const_lvalue_to_class_conv_temporary() {
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br1 = ConvertibleToBase();
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br2 = ConvertibleToDerived();
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid bind_lvalue_to_conv_rvalue_ambig(ConvertibleToBothDerived both) {
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Derived &dr1 = both;
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const Base &br1 = both; // expected-error{{reference initialization of type 'const Base &' with initializer of type 'ConvertibleToBothDerived' is ambiguous}}
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry