brintos

brintos / llvm-project-archived public Read only

0
0
Text · 16.5 KiB · 72cf249 Raw
541 lines · cpp
1// RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors2// RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11-14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors3// RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx11-14,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors4// RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors5// RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors6// RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors7// RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors8 9namespace std {10  __extension__ typedef __SIZE_TYPE__ size_t;11 12  template<typename E> struct initializer_list {13    const E *p; size_t n;14    initializer_list(const E *p, size_t n);15    initializer_list();16  };17}18 19namespace cwg2303 { // cwg2303: 1220#if __cplusplus >= 201103L21template <typename... T>22struct A;23template <>24struct A<> {};25template <typename T, typename... Ts>26struct A<T, Ts...> : A<Ts...> {};27struct B : A<int, int> {};28struct C : A<int, int>, A<int> {};29/* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:30    struct cwg2303::C -> A<int, int> -> A<int>31    struct cwg2303::C -> A<int>}} */32struct D : A<int>, A<int, int> {};33/* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:34    struct cwg2303::D -> A<int>35    struct cwg2303::D -> A<int, int> -> A<int>}} */36struct E : A<int, int> {};37struct F : B, E {};38 39template <typename... T>40void f(const A<T...> &) {41  static_assert(sizeof...(T) == 2, "Should only match A<int,int>");42}43template <typename... T>44void f2(const A<T...> *);45 46void g() {47  f(B{}); // This is no longer ambiguous.48  B b;49  f2(&b);50  f(C{});51  f(D{});52  f(F{});53  /* since-cxx11-error@-1 {{ambiguous conversion from derived class 'const F' to base class 'const A<int, int>':54    struct cwg2303::F -> B -> A<int, int>55    struct cwg2303::F -> E -> A<int, int>}} */56}57#endif58} // namespace cwg230359 60namespace cwg2304 { // cwg2304: 2.861template<typename T> void foo(T, int);62template<typename T> void foo(T&, ...);63struct Q; // #cwg2304-Q64void fn1(Q &data_vectors) {65  foo(data_vectors, 0);66  // expected-error@-1 {{argument type 'cwg2304::Q' is incomplete}}67  //   expected-note@#cwg2304-Q {{forward declaration of 'cwg2304::Q'}}68}69} // namespace cwg230470 71namespace cwg2310 { // cwg2310: partial72#if __cplusplus >= 201103L73template<typename A, typename B>74struct check_derived_from {75  static A a;76  // FIXME: all 3 examples should be rejected in all language modes.77  // FIXME: we should test this in 98 mode.78  // FIXME: we accept this when MSVC triple is used79  static constexpr B *p = &a;80#if !defined(_WIN32) || defined(__MINGW32__)81  // cxx11-14-error@-2 {{cannot initialize a variable of type 'cwg2310::X *const' with an rvalue of type 'cwg2310::Z *'}}82  //   cxx11-14-note@#cwg2310-X {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::X>' requested here}}83  // cxx11-14-error@-4 {{cannot initialize a variable of type 'cwg2310::Y *const' with an rvalue of type 'cwg2310::Z *'}}84  //   cxx11-14-note@#cwg2310-Y {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::Y>' requested here}}85#endif86};87 88struct W {};89struct X {};90struct Y {};91struct Z : W,92  X, check_derived_from<Z, X>, // #cwg2310-X93  check_derived_from<Z, Y>, Y  // #cwg2310-Y94{95  // FIXME: It was properly rejected before, but we're crashing since Clang 11 in C++11 and C++14 modes.96  //        See https://github.com/llvm/llvm-project/issues/5992097#if __cplusplus >= 201703L98  check_derived_from<Z, W> cdf;99#endif100};101#endif102} // namespace cwg2310103 104// cwg2331: na105// cwg2335 is in cwg2335.cxx106 107namespace cwg2311 {  // cwg2311 is open with no proposed resolution108#if __cplusplus >= 201707L109template<typename T>110void test() {111  // Ensure none of these try to call a move constructor.112  T a = T{T(0)};113  T b{T(0)};114  auto c{T(0)};115  T d = {T(0)};116  auto e = {T(0)};117#if __cplusplus >= 202302L118  auto f = auto{T(0)};119#endif120  void(*fn)(T);121  fn({T(0)});122}123 124struct NonMovable {125  NonMovable(int);126  NonMovable(NonMovable&&) = delete;127};128struct NonMovableNonApplicableIList {129  NonMovableNonApplicableIList(int);130  NonMovableNonApplicableIList(NonMovableNonApplicableIList&&) = delete;131  NonMovableNonApplicableIList(std::initializer_list<int>);132};133struct ExplicitMovable {134  ExplicitMovable(int);135  explicit ExplicitMovable(ExplicitMovable&&);136};137struct ExplicitNonMovable {138  ExplicitNonMovable(int);139  explicit ExplicitNonMovable(ExplicitNonMovable&&) = delete;140};141struct ExplicitNonMovableNonApplicableIList {142  ExplicitNonMovableNonApplicableIList(int);143  explicit ExplicitNonMovableNonApplicableIList(ExplicitNonMovableNonApplicableIList&&) = delete;144  ExplicitNonMovableNonApplicableIList(std::initializer_list<int>);145};146struct CopyOnly {147  CopyOnly(int);148  CopyOnly(const CopyOnly&);149  CopyOnly(CopyOnly&&) = delete;150};151struct ExplicitCopyOnly {152  ExplicitCopyOnly(int);153  explicit ExplicitCopyOnly(const ExplicitCopyOnly&);154  explicit ExplicitCopyOnly(ExplicitCopyOnly&&) = delete;155};156 157template void test<NonMovable>();158template void test<NonMovableNonApplicableIList>();159template void test<ExplicitMovable>();160template void test<ExplicitNonMovable>();161template void test<ExplicitNonMovableNonApplicableIList>();162template void test<CopyOnly>();163template void test<ExplicitCopyOnly>();164 165struct any {166    template<typename T>167    any(T&&);168};169 170template<typename T>171struct X {172    X();173    X(T) = delete; // #cwg2311-X174};175 176X<std::initializer_list<any>> x{ X<std::initializer_list<any>>() };177// since-cxx17-error@-1 {{call to deleted constructor of 'X<std::initializer_list<any>>'}}178//   since-cxx17-note@#cwg2311-X {{'X' has been explicitly marked deleted here}}179 180// Per the currently implemented resolution, this does not apply to std::initializer_list.181// An initializer list initialized from `{ e }` always has exactly one element constructed182// from `e`, where previously that could have been a copy of an init list or `e.operator std::initializer_list()`183struct InitListCtor {184  InitListCtor(int);185  InitListCtor(InitListCtor&&) = delete;186  InitListCtor(std::initializer_list<InitListCtor>) = delete; // #cwg2311-InitListCtor187};188 189std::initializer_list<InitListCtor> i;190auto j = std::initializer_list<InitListCtor>{ i };191// since-cxx17-error@-1 {{conversion function from 'std::initializer_list<InitListCtor>' to 'const InitListCtor' invokes a deleted function}}192//   since-cxx17-note@#cwg2311-InitListCtor {{'InitListCtor' has been explicitly marked deleted here}}193#endif194} // namespace cwg2311195 196namespace cwg2338 { // cwg2338: 12197#if __cplusplus >= 201103L198namespace B {199enum E : bool { Zero, One };200static_assert((int)(E)2 == 1, "");201} // namespace B202namespace D {203enum class E : bool { Zero, One };204static_assert((int)(E)2 == 1, "");205} // namespace D206#endif207} // namespace cwg2338208 209namespace cwg2346 { // cwg2346: 11210  void test() {211    const int i2 = 0;212    extern void h2b(int x = i2 + 0); // ok, not odr-use213  }214} // namespace cwg2346215 216namespace cwg2351 { // cwg2351: 20217#if __cplusplus >= 201103L218  static_assert((void{}, true), "");219 220  void f() {221    return void{};222  }223 224  template<typename T>225  void g() {226    return T{};227  }228  template void g<void>();229  template void g<const void>();230 231  void h() {232    return {};233    // since-cxx11-error@-1 {{void function 'h' must not return a value}}234  }235 236  template<typename T, int... I>237  T i() {238    return T{I...};239  }240  template void i<void>();241  template const void i<const void>();242 243  static_assert((void({}), true), "");244  // since-cxx11-error@-1 {{cannot initialize non-class type 'void' with a parenthesized initializer list}}245#else246  int I = (void{}, 0);247  // cxx98-error@-1 {{expected ')'}}248  //   cxx98-note@-2 {{to match this '('}}249  // cxx98-error@-3 {{expected expression}}250#endif251} // namespace cwg2351252 253namespace cwg2352 { // cwg2352: 10254  int **p;255  const int *const *const &f1() { return p; }256  int *const *const &f2() { return p; }257  int **const &f3() { return p; }258 259  const int **const &f4() { return p; }260  // expected-error@-1 {{reference to type 'const int **const' could not bind to an lvalue of type 'int **'}}261  const int *const *&f5() { return p; }262  // expected-error@-1 {{binding reference of type 'const int *const *' to value of type 'int **' not permitted due to incompatible qualifiers}}263 264  // FIXME: We permit this as a speculative defect resolution, allowing265  // qualification conversions when forming a glvalue conditional expression.266  const int * const * const q = 0;267  __typeof(&(true ? p : q)) x = &(true ? p : q);268 269  // FIXME: Should we compute the composite pointer type here and produce an270  // lvalue of type 'const int *const * const'?271  const int * const * r;272  void *y = &(true ? p : r);273  // expected-error@-1 {{rvalue of type 'const int *const *'}}274 275  // FIXME: We order these as a speculative defect resolution.276  void f(const int * const * const &r);277#if __cplusplus >= 201103L278  constexpr279#endif280  int *const *const &f(int * const * const &r) { return r; }281 282  // No temporary is created here.283  int *const *const &check_f = f(p);284#if __cplusplus >= 201103L285  static_assert(&p == &check_f, "");286#endif287} // namespace cwg2352288 289namespace cwg2354 { // cwg2354: 15290#if __cplusplus >= 201103L291enum alignas(64) A {};292// since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}293enum struct alignas(64) B {};294// since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}295#endif296} // namespace cwg2354297 298namespace cwg2356 { // cwg2356: 4299#if __cplusplus >= 201103L300struct A {301  A();302  A(A &&);                        // #1303  template<typename T> A(T &&);   // #2304};305struct B : A {306  using A::A;307  B(const B &);                   // #3308  B(B &&) = default;              // #4, implicitly deleted309  // since-cxx11-warning@-1 {{explicitly defaulted move constructor is implicitly deleted}}310  //   since-cxx11-note@#cwg2356-X {{move constructor of 'B' is implicitly deleted because field 'x' has a deleted move constructor}}311  //   since-cxx11-note@#cwg2356-X {{'X' has been explicitly marked deleted here}}312  //   since-cxx11-note@-4 {{replace 'default' with 'delete'}}313 314  struct X { X(X &&) = delete; } x; // #cwg2356-X315};316extern B b1;317B b2 = static_cast<B&&>(b1);      // calls #3: #1, #2, and #4 are not viable318struct C { operator B&&(); };319B b3 = C();                       // calls #3320#endif321} // namespace cwg2356322 323namespace cwg2358 { // cwg2358: 16324#if __cplusplus >= 201402L325  void f2() {326    int i = 1;327    void g1(int = [xxx=1] { return xxx; }());  // OK328    void g2(int = [xxx=i] { return xxx; }());329    // since-cxx14-error@-1 {{default argument references local variable 'i' of enclosing function}}330  }331#endif332} // namespace cwg2358333 334// CWG2363 was closed as NAD, but its resolution does affirm that335// a friend declaration cannot have an opaque-enumm-specifier.336namespace cwg2363 { // cwg2363: 19337#if __cplusplus >= 201103L338enum class E0;339enum E1 : int;340 341struct A {342  friend enum class E0;343  // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}344  // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}345  //   since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}346 347  friend enum E0;348  // since-cxx11-error@-1 {{elaborated enum specifier cannot be declared as a friend}}349  //   since-cxx11-note@-2 {{remove 'enum' to befriend an enum}}350 351  friend enum class E1;352  // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}353  // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}354  //   since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}355 356  friend enum E1;357  // since-cxx11-error@-1 {{elaborated enum specifier cannot be declared as a friend}}358  //   since-cxx11-note@-2 {{remove 'enum' to befriend an enum}}359 360  friend enum class E2;361  // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}362  // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}363  //   since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}364};365#endif366} // namespace cwg2363367 368namespace cwg2369 { // cwg2369: partial369#if __cplusplus >= 202002L370template <class T> struct Z {371  typedef typename T::x xx;372};373 374template <class T>375concept C = requires { typename T::A; };376template <C T> typename Z<T>::xx f(void *, T); // #1377template <class T> void f(int, T);             // #2378 379struct A {380} a;381 382struct ZZ {383  template <class T, class = typename Z<T>::xx> operator T *();384  operator int();385};386 387void foo() {388  ZZ zz;389  f(1, a); // OK, deduction fails for #1 because there is no conversion from int390           // to void*391  f(zz, 42); // OK, deduction fails for #1 because C<int> is not satisfied392}393 394#endif395} // namespace cwg2369396 397namespace cwg2370 { // cwg2370: no398namespace N {399typedef int type;400void g(type);401void h(type);402} // namespace N403class C {404  typedef N::type N_type;405  // FIXME: `type` should be searched for in N406  // friend void N::g(type);407  friend void N::h(N_type);408};409} // namespace cwg2370410 411namespace cwg2376 { // cwg2376: 21412#if __cplusplus >= 201703L413template<int = 0> class C {};414 415C a;416const volatile C b = C<2>();417C (c) = {};418C* d;419// expected-error@-1 {{cannot form pointer to deduced class template specialization type}}420C e[1];421// expected-error@-1 {{cannot form array of deduced class template specialization type}}422#endif423}424 425namespace cwg2386 { // cwg2386: 9426// Otherwise, if the qualified-id std::tuple_size<E> names a complete class427// type **with a member value**, the expression std::tuple_size<E>::value shall428// be a well-formed integral constant expression429#if __cplusplus >= 201702L430struct Bad1 { int a, b; };431struct Bad2 { int a, b; };432} // namespace cwg2386433namespace std {434template <typename T> struct tuple_size;435template <> struct tuple_size<cwg2386::Bad1> {};436template <> struct tuple_size<cwg2386::Bad2> {437  static const int value = 42;438};439} // namespace std440namespace cwg2386 {441void no_value() { auto [x, y] = Bad1(); }442void wrong_value() { auto [x, y] = Bad2(); }443// since-cxx17-error@-1 {{type 'Bad2' binds to 42 elements, but only 2 names were provided}}444#endif445} // namespace cwg2386446 447// cwg2385: na448 449namespace cwg2387 { // cwg2387: 9450#if __cplusplus >= 201402L451  template<int> int a = 0;452  extern template int a<0>; // ok453 454  template<int> static int b = 0;455  extern template int b<0>;456  // since-cxx14-error@-1 {{explicit instantiation declaration of 'b<0>' with internal linkage}}457 458  template<int> const int c = 0;459  extern template const int c<0>; // ok, has external linkage despite 'const'460 461  template<typename T> T d = 0;462  extern template int d<int>;463  extern template const int d<const int>;464#endif465} // namespace cwg2387466 467namespace cwg2390 { // cwg2390: 14468// Test that macro expansion of the builtin argument works.469#define C clang470#define F fallthrough471#define CF clang::fallthrough472 473#if !__has_cpp_attribute(F)474#error "doesn't have fallthrough"475#endif476 477#if !__has_cpp_attribute(C::F)478#error "doesn't have clang::fallthrough 1"479#endif480 481#if !__has_cpp_attribute(clang::F)482#error "doesn't have clang::fallthrough 2"483#endif484 485#if !__has_cpp_attribute(C::fallthrough)486#error "doesn't have clang::fallthrough 3"487#endif488 489#if !__has_cpp_attribute(CF)490#error "doesn't have clang::fallthrough 4"491#endif492 493#define FUNCLIKE1(x) clang::x494#if !__has_cpp_attribute(FUNCLIKE1(fallthrough))495#error "doesn't have clang::fallthrough through func-like macro 1"496#endif497 498#define FUNCLIKE2(x) _Clang::x499#if !__has_cpp_attribute(FUNCLIKE2(fallthrough))500#error "doesn't have clang::fallthrough through func-like macro 2"501#endif502} // namespace cwg2390503 504namespace cwg2394 { // cwg2394: 15505 506struct A {};507const A a;508 509// Now allowed to default-init B.510struct B { const A a; };511B b;512 513} // namespace cwg2394514 515namespace cwg2396 { // cwg2396: no516  struct A {517    struct B;518    operator B B::*();519  };520  struct B;521 522  // FIXME: per P1787 "Calling a conversion function" example, all of the523  // examples below are well-formed, with B resolving to A::B, but currently524  // it's been resolved to cwg2396::B.525 526  // void f(A a) { a.operator B B::*(); }527  // void g(A a) { a.operator decltype(B()) B::*(); }528  // void g2(A a) { a.operator B decltype(B())::*(); }529} // namespace cwg2396530 531namespace cwg2397 { // cwg2397: 17532#if __cplusplus >= 201103L533  void foo() {534    int a[5];535 536    auto (&b)[5] = a;537    auto (*c)[5] = &a;538  }539#endif540} // namespace cwg2397541