brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · ed9d2b7 Raw
93 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s2 3template <typename T, typename U = void*>4struct A {5  enum {6    id = _Generic(T{}, // expected-error {{controlling expression type 'char' not compatible with any generic association type}}7        int: 1, // expected-note {{compatible type 'int' specified here}}8        float: 2,9        U: 3) // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}10  };11};12 13static_assert(A<int>::id == 1, "fail");14static_assert(A<float>::id == 2, "fail");15static_assert(A<double, double>::id == 3, "fail");16 17A<char> a1; // expected-note {{in instantiation of template class 'A<char>' requested here}}18A<short, int> a2; // expected-note {{in instantiation of template class 'A<short, int>' requested here}}19 20template <typename T, typename U>21struct B {22  enum {23    id = _Generic(T{},24        int: 1, // expected-note {{compatible type 'int' specified here}}25        int: 2, // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}26        U: 3)27  };28};29 30template <unsigned Arg, unsigned... Args> struct Or {31  enum { result = Arg | Or<Args...>::result };32};33 34template <unsigned Arg> struct Or<Arg> {35  enum { result = Arg };36};37 38template <class... Args> struct TypeMask {39  enum {40   result = Or<_Generic(Args{}, int: 1, long: 2, short: 4, float: 8)...>::result41  };42};43 44static_assert(TypeMask<int, long, short>::result == 7, "fail");45static_assert(TypeMask<float, short>::result == 12, "fail");46static_assert(TypeMask<int, float, float>::result == 9, "fail");47 48 49struct Test {50  int i;51};52 53void unreachable_associations(const int i, const Test t) {54  // FIXME: it's not clear to me whether we intended to deviate from the C55  // semantics in terms of how qualifiers are handled, so this documents the56  // existing behavior but perhaps not the desired behavior.57  static_assert(58    _Generic(i,59      const int : 1,    // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}60      volatile int : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'volatile int' will never be selected because it is qualified}}61      int[12] : 3,      // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'int[12]' will never be selected because it is of array type}}62      int : 4,63      default : 564    ) == 4, "we had better pick int, not const int!");65  static_assert(66    _Generic(t,67      Test : 1,68      const Test : 2,  // Ok in C++, warned in C69      default : 370    ) == 2, "we had better pick const Test, not Test!"); // C++-specific result71}72 73namespace GH55562 {74struct S { // expected-note {{declared here}}75  int i;76};77 78void func(struct S s) {79  // We would previously reject this because the parser thought 'struct S :'80  // was the start of a definition (with a base class specifier); it's not, it81  // is an elaborated type specifier followed by the association's value and82  // it should work the same as in C.83  (void)_Generic(s, struct S : 1);84  (void)_Generic(s, struct T : 1); // expected-error {{controlling expression type 'struct S' not compatible with any generic association type}}85 86  // The rest of these cases test that we still produce a reasonable diagnostic87  // when referencing an unknown type or trying to define a type in other ways.88  (void)_Generic(s, struct U { int a; } : 1); // expected-error {{'U' cannot be defined in a type specifier}}89  (void)_Generic(s, struct V : S);            // expected-error {{'S' does not refer to a value}}90  (void)_Generic(s, struct W : S { int b; } : 1); // expected-error {{expected '(' for function-style cast or type construction}}91}92} // namespace GH5556293