13573b2c84372d9484296fa658f5276f6c09acb92Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s 2da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 3fc8ba61c279f72d3bac5df8754348037df5f76f3Argiris Kirtzidisstruct Base { }; 4ec29a8136449036f485e28f6e1e21ae3fb0289a0John McCallstruct Derived : Base { }; // expected-note{{candidate constructor (the implicit copy constructor) not viable}} 5da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Unrelated { }; 6da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Derived2 : Base { }; 7da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct Diamond : Derived, Derived2 { }; 8da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 9da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBaseRef { 10da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Base&() const; 11da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 12da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 13da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToDerivedRef { 14da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived&() const; 15da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 16da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 17da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBothDerivedRef { 18da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived&(); // expected-note{{candidate function}} 19da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived2&(); // expected-note{{candidate function}} 20da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 21da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 22da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToIntRef { 23da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator int&(); 24da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 25da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 26da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBase { 27da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Base() const; 28da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 29da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 30da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToDerived { 31da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived() const; 32da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 33da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 34da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToBothDerived { 35da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived(); // expected-note{{candidate function}} 36da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator Derived2(); // expected-note{{candidate function}} 37da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 38da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 39da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct ConvertibleToInt { 40da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor operator int(); 41da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 42da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 43da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregortemplate<typename T> T create(); 44da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 45da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// First bullet: lvalue references binding to lvalues (the simple cases). 46da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_lvalue(Base b, Derived d, 47da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base bc, const Derived dc, 48da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Diamond diamond, 49da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int i) { 50da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor // Reference-compatible 51da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &br1 = b; 52da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &br2 = d; 53da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Derived &dr1 = d; 5498624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Derived &dr2 = b; // expected-error{{non-const lvalue reference to type 'Derived' cannot bind to a value of unrelated type 'Base'}} 55da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &br3 = bc; // expected-error{{drops qualifiers}} 56da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &br4 = dc; // expected-error{{drops qualifiers}} 5798624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Base &br5 = diamond; // expected-error{{ambiguous conversion from derived class 'Diamond' to base class 'Base':}} 58da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int &ir = i; 59da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor long &lr = i; // expected-error{{non-const lvalue reference to type 'long' cannot bind to a value of unrelated type 'int'}} 60da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 61da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 62da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_quals(volatile Base b, volatile Derived d, 63da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor volatile const Base bvc, volatile const Derived dvc, 64da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor volatile const int ivc) { 65da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor volatile Base &bvr1 = b; 66da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor volatile Base &bvr2 = d; 67da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner volatile Base &bvr3 = bvc; // expected-error{{binding of reference to type 'volatile Base' to a value of type 'const volatile Base' drops qualifiers}} 68da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner volatile Base &bvr4 = dvc; // expected-error{{binding of reference to type 'volatile Base' to a value of type 'const volatile Derived' drops qualifiers}} 69da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 7003028a90c18c484ab477e6535441ec5825a7328aChris Lattner volatile int &ir = ivc; // expected-error{{binding of reference to type 'volatile int' to a value of type 'const volatile int' drops qualifiers}} 71cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor 72cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor const volatile Base &bcvr1 = b; 73cc845d86b09df95440626e0f5c9c4ce361fc8fffDouglas Gregor const volatile Base &bcvr2 = d; 74da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 75da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 76da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_rvalue() { 7798624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Base &br1 = Base(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Base'}} 7898624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Base &br2 = Derived(); // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a temporary of type 'Derived'}} 79da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const volatile Base &br3 = Base(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Base'}} 80da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const volatile Base &br4 = Derived(); // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a temporary of type 'Derived'}} 81da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 82da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int &ir = 17; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}} 83da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 84da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 85da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_unrelated(Unrelated ur) { 8698624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Base &br1 = ur; // expected-error{{non-const lvalue reference to type 'Base' cannot bind to a value of unrelated type 'Unrelated'}} 87da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const volatile Base &br2 = ur; // expected-error{{volatile lvalue reference to type 'const volatile Base' cannot bind to a value of unrelated type 'Unrelated'}} 88da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 89da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 90da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_lvalue() { 91da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor // Not reference-related, but convertible 92da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &nbr1 = ConvertibleToBaseRef(); 93da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Base &nbr2 = ConvertibleToDerivedRef(); 94da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Derived &ndr1 = ConvertibleToDerivedRef(); 95da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int &ir = ConvertibleToIntRef(); 96da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 97da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 98da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_lvalue_ambig(ConvertibleToBothDerivedRef both) { 99da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor Derived &dr1 = both; 10098624359b74071fdf2575b5b1afb5d3a3a86b4a3John McCall Base &br1 = both; // expected-error{{reference initialization of type 'Base &' with initializer of type 'ConvertibleToBothDerivedRef' is ambiguous}} 101da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 102da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 103da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorstruct IntBitfield { 104da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int i : 17; // expected-note{{bit-field is declared here}} 105da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor}; 106da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 107da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid test_bitfield(IntBitfield ib) { 108da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor int & ir1 = (ib.i); // expected-error{{non-const reference cannot bind to bit-field 'i'}} 109da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 110da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 111da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// Second bullet: const lvalue reference binding to an rvalue with 112da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// similar type (both of which are class types). 113da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_const_lvalue_to_rvalue() { 114da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br1 = create<Base>(); 115da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br2 = create<Derived>(); 116da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Derived &dr1 = create<Base>(); // expected-error{{no viable conversion}} 117da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 118da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br3 = create<const Base>(); 119da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br4 = create<const Derived>(); 120da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 121da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const Base &br5 = create<const volatile Base>(); // expected-error{{binding of reference to type 'const Base' to a value of type 'const volatile Base' drops qualifiers}} 122da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const Base &br6 = create<const volatile Derived>(); // expected-error{{binding of reference to type 'const Base' to a value of type 'const volatile Derived' drops qualifiers}} 123da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 124da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const int &ir = create<int>(); 125da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 126da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor 127da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor// Second bullet: const lvalue reference binds to the result of a conversion. 128da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_const_lvalue_to_class_conv_temporary() { 129da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br1 = ConvertibleToBase(); 130da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Base &br2 = ConvertibleToDerived(); 131da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 132da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregorvoid bind_lvalue_to_conv_rvalue_ambig(ConvertibleToBothDerived both) { 133da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor const Derived &dr1 = both; 134da004db50cc2f5917b3a94d5cc54b4a6e85fdb05Chris Lattner const Base &br1 = both; // expected-error{{reference initialization of type 'const Base &' with initializer of type 'ConvertibleToBothDerived' is ambiguous}} 135da86f093507a7733ae955d7511b88f8bf4f61752Douglas Gregor} 136