diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -1692,11 +1692,20 @@ // reached. We will warn about this so users are less surprised by // the unreachable association. However, we don't have to handle // function types; that's not an object type, so it's handled above. + // + // The logic is somewhat different for C++ because C++ has different + // lvalue to rvalue conversion rules than C. [conv.lvalue]p1 says, + // If T is a non-class type, the type of the prvalue is the cv- + // unqualified version of T. Otherwise, the type of the prvalue is T. + // The result of these rules is that all qualified types in an + // association in C are unreachable, and in C++, only qualified non- + // class types are unreachable. unsigned Reason = 0; QualType QT = Types[i]->getType(); if (QT->isArrayType()) Reason = 1; - else if (QT.hasQualifiers()) + else if (QT.hasQualifiers() && + (!LangOpts.CPlusPlus || !QT->isRecordType())) Reason = 2; if (Reason) diff --git a/clang/test/Sema/generic-selection.c b/clang/test/Sema/generic-selection.c --- a/clang/test/Sema/generic-selection.c +++ b/clang/test/Sema/generic-selection.c @@ -58,7 +58,11 @@ ), int : 0); } -void unreachable_associations(const int i) { +struct Test { + int i; +}; + +void unreachable_associations(const int i, const struct Test t) { _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}} _Generic(i, // ext-warning {{'_Generic' is a C11 extension}} 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}} @@ -67,4 +71,10 @@ int : 4, default : 5 ) == 4, "we had better pick int!"); + _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}} + _Generic(t, // ext-warning {{'_Generic' is a C11 extension}} + struct Test : 1, + const struct Test : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const struct Test' will never be selected because it is qualified}} + default : 3 + ) == 1, "we had better pick struct Test, not const struct Test!"); // C-specific result } diff --git a/clang/test/SemaCXX/generic-selection.cpp b/clang/test/SemaCXX/generic-selection.cpp --- a/clang/test/SemaCXX/generic-selection.cpp +++ b/clang/test/SemaCXX/generic-selection.cpp @@ -44,3 +44,28 @@ static_assert(TypeMask::result == 7, "fail"); static_assert(TypeMask::result == 12, "fail"); static_assert(TypeMask::result == 9, "fail"); + + +struct Test { + int i; +}; + +void unreachable_associations(const int i, const Test t) { + // FIXME: it's not clear to me whether we intended to deviate from the C + // semantics in terms of how qualifiers are handled, so this documents the + // existing behavior but perhaps not the desired behavior. + static_assert( + _Generic(i, + 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}} + 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}} + 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}} + int : 4, + default : 5 + ) == 4, "we had better pick int, not const int!"); + static_assert( + _Generic(t, + Test : 1, + const Test : 2, // Ok in C++, warned in C + default : 3 + ) == 2, "we had better pick const Test, not Test!"); // C++-specific result +}