Correct the diagnostic behavior for unreachable _Generic associations in C++

New diagnostics were added for unreachable generic selection expression
associations in ca75ac5f04, but it did
not account for a difference in behavior between C and C++ regarding
lvalue to rvalue conversions. So we would issue diagnostics about a
selection being unreachable and then reach it. This corrects the
diagnostic behavior in that case.

Differential Revision: https://reviews.llvm.org/D125882
This commit is contained in:
Aaron Ballman 2022-05-18 12:44:24 -04:00
parent 087ef34fff
commit 47b8424a53
3 changed files with 46 additions and 2 deletions

View File

@ -1692,11 +1692,20 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc,
// 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)

View File

@ -58,7 +58,11 @@ void GH50227(void) {
), 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 @@ void unreachable_associations(const int i) {
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
}

View File

@ -44,3 +44,28 @@ template <class... Args> struct TypeMask {
static_assert(TypeMask<int, long, short>::result == 7, "fail");
static_assert(TypeMask<float, short>::result == 12, "fail");
static_assert(TypeMask<int, float, float>::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
}