227 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -fsyntax-only %s -verify2 3#define UIE __attribute__((using_if_exists))4 5namespace test_basic {6namespace NS {}7 8using NS::x UIE; // expected-note{{using declaration annotated with 'using_if_exists' here}}9x usex(); // expected-error{{reference to unresolved using declaration}}10 11using NotNS::x UIE; // expected-error{{use of undeclared identifier 'NotNS'}}12 13using NS::NotNS::x UIE; // expected-error{{no member named 'NotNS' in namespace 'test_basic::NS'}}14} // namespace test_basic15 16namespace test_redecl {17namespace NS {}18 19using NS::x UIE;20using NS::x UIE;21 22namespace NS1 {}23namespace NS2 {}24namespace NS3 {25int A(); // expected-note{{target of using declaration}}26struct B {}; // expected-note{{target of using declaration}}27int C(); // expected-note{{conflicting declaration}}28struct D {}; // expected-note{{conflicting declaration}}29} // namespace NS330 31using NS1::A UIE;32using NS2::A UIE; // expected-note{{using declaration annotated with 'using_if_exists' here}} expected-note{{conflicting declaration}}33using NS3::A UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}}34int i = A(); // expected-error{{reference to unresolved using declaration}}35 36using NS1::B UIE;37using NS2::B UIE; // expected-note{{conflicting declaration}} expected-note{{using declaration annotated with 'using_if_exists' here}}38using NS3::B UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}}39B myB; // expected-error{{reference to unresolved using declaration}}40 41using NS3::C UIE;42using NS2::C UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}} expected-note{{target of using declaration}}43int j = C();44 45using NS3::D UIE;46using NS2::D UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}} expected-note{{target of using declaration}}47D myD;48} // namespace test_redecl49 50namespace test_dependent {51template <class B>52struct S : B {53 using B::mf UIE; // expected-note 3 {{using declaration annotated with 'using_if_exists' here}}54 using typename B::mt UIE; // expected-note{{using declaration annotated with 'using_if_exists' here}}55};56 57struct BaseEmpty {58};59struct BaseNonEmpty {60 void mf();61 typedef int mt;62};63 64template <class Base>65struct UseCtor : Base {66 using Base::Base UIE; // expected-error{{'using_if_exists' attribute cannot be applied to an inheriting constructor}}67};68struct BaseCtor {};69 70void f() {71 S<BaseEmpty> empty;72 S<BaseNonEmpty> nonempty;73 empty.mf(); // expected-error {{reference to unresolved using declaration}}74 nonempty.mf();75 (&empty)->mf(); // expected-error {{reference to unresolved using declaration}}76 (&nonempty)->mf();77 78 S<BaseEmpty>::mt y; // expected-error {{reference to unresolved using declaration}}79 S<BaseNonEmpty>::mt z;80 81 S<BaseEmpty>::mf(); // expected-error {{reference to unresolved using declaration}}82 83 UseCtor<BaseCtor> usector;84}85 86template <class B>87struct Implicit : B {88 using B::mf UIE; // expected-note {{using declaration annotated with 'using_if_exists' here}}89 using typename B::mt UIE; // expected-note 2 {{using declaration annotated with 'using_if_exists' here}}90 91 void use() {92 mf(); // expected-error {{reference to unresolved using declaration}}93 mt x; // expected-error {{reference to unresolved using declaration}}94 }95 96 mt alsoUse(); // expected-error {{reference to unresolved using declaration}}97};98 99void testImplicit() {100 Implicit<BaseNonEmpty> nonempty;101 Implicit<BaseEmpty> empty; // expected-note {{in instantiation}}102 nonempty.use();103 empty.use(); // expected-note {{in instantiation}}104}105 106template <class>107struct NonDep : BaseEmpty {108 using BaseEmpty::x UIE; // expected-note{{using declaration annotated with 'using_if_exists' here}}109 x y(); // expected-error{{reference to unresolved using declaration}}110};111} // namespace test_dependent112 113namespace test_using_pack {114template <class... Ts>115struct S : Ts... {116 using typename Ts::x... UIE; // expected-error 2 {{target of using declaration conflicts with declaration already in scope}} expected-note{{conflicting declaration}} expected-note{{target of using declaration}}117};118 119struct E1 {};120struct E2 {};121S<E1, E2> a;122 123struct F1 {124 typedef int x; // expected-note 2 {{conflicting declaration}}125};126struct F2 {127 typedef int x; // expected-note 2 {{target of using declaration}}128};129S<F1, F2> b;130 131S<E1, F2> c; // expected-note{{in instantiation of template class}}132S<F1, E2> d; // expected-note{{in instantiation of template class}}133 134template <class... Ts>135struct S2 : Ts... {136 using typename Ts::x... UIE; // expected-error 2 {{target of using declaration conflicts with declaration already in scope}} expected-note 3 {{using declaration annotated with 'using_if_exists' here}} expected-note{{conflicting declaration}} expected-note{{target of using declaration}}137 138 x mem(); // expected-error 3 {{reference to unresolved using declaration}}139};140 141S2<E1, E2> e; // expected-note{{in instantiation of template class}}142S2<F1, F2> f;143S2<E1, F2> g; // expected-note{{in instantiation of template class}}144S2<F1, E2> h; // expected-note{{in instantiation of template class}}145 146template <class... Ts>147struct S3 : protected Ts... {148 using Ts::m... UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}} expected-note{{target of using declaration}}149};150struct B1 {151 enum { m }; // expected-note{{conflicting declaration}}152};153struct B2 {};154 155S3<B1, B2> i; // expected-note{{in instantiation of template}}156S<B2, B1> j;157 158} // namespace test_using_pack159 160namespace test_nested {161namespace NS {}162 163using NS::x UIE; // expected-note {{using declaration annotated with 'using_if_exists' here}}164 165namespace NS2 {166using ::test_nested::x UIE;167}168 169NS2::x y; // expected-error {{reference to unresolved using declaration}}170} // namespace test_nested171 172namespace test_scope {173int x; // expected-note{{conflicting declaration}}174void f() {175 int x; // expected-note{{conflicting declaration}}176 {177 using ::x UIE; // expected-note {{using declaration annotated with 'using_if_exists' here}}178 (void)x; // expected-error {{reference to unresolved using declaration}}179 }180 181 {182 using test_scope::x;183 using ::x UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}} expected-note{{target of using declaration}}184 (void)x;185 }186 187 (void)x;188 189 using ::x UIE; // expected-error{{target of using declaration conflicts with declaration already in scope}} expected-note{{target of using declaration}}190 (void)x;191}192} // namespace test_scope193 194namespace test_appertains_to {195namespace NS {196typedef int x;197}198 199// FIXME: This diagnostics is wrong.200using alias UIE = NS::x; // expected-error {{'using_if_exists' attribute only applies to named declarations, types, and value declarations}}201 202template <class>203using template_alias UIE = NS::x; // expected-error {{'using_if_exists' attribute only applies to named declarations, types, and value declarations}}204 205void f() UIE; // expected-error {{'using_if_exists' attribute only applies to named declarations, types, and value declarations}}206 207using namespace NS UIE; // expected-error {{'using_if_exists' attribute only applies to named declarations, types, and value declarations}}208} // namespace test_appertains_to209 210typedef int *fake_FILE;211int fake_printf();212 213namespace std {214using ::fake_FILE UIE;215using ::fake_printf UIE;216using ::fake_fopen UIE; // expected-note {{using declaration annotated with 'using_if_exists' here}}217using ::fake_size_t UIE; // expected-note {{using declaration annotated with 'using_if_exists' here}}218} // namespace std219 220int main() {221 std::fake_FILE file;222 file = std::fake_fopen(); // expected-error {{reference to unresolved using declaration}} expected-error{{incompatible integer to pointer}}223 std::fake_size_t size; // expected-error {{reference to unresolved using declaration}}224 size = fake_printf();225 size = std::fake_printf();226}227