Skip to content

Commit 77918bb

Browse files
committed
[Clang] Demote mixed enumeration arithmetic error to a warning (#131811)
In C++, defaulted to an error. C++ removed these features but the removal negatively impacts users. Fixes #92340
1 parent 4370072 commit 77918bb

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7567,9 +7567,13 @@ def warn_arith_conv_mixed_enum_types_cxx20 : Warning<
75677567
"%sub{select_arith_conv_kind}0 "
75687568
"different enumeration types%diff{ ($ and $)|}1,2 is deprecated">,
75697569
InGroup<DeprecatedEnumEnumConversion>;
7570-
def err_conv_mixed_enum_types_cxx26 : Error<
7570+
7571+
def err_conv_mixed_enum_types: Error <
75717572
"invalid %sub{select_arith_conv_kind}0 "
75727573
"different enumeration types%diff{ ($ and $)|}1,2">;
7574+
def warn_conv_mixed_enum_types_cxx26 : Warning <
7575+
err_conv_mixed_enum_types.Summary>,
7576+
InGroup<EnumEnumConversion>, DefaultError;
75737577

75747578
def warn_arith_conv_mixed_anon_enum_types : Warning<
75757579
warn_arith_conv_mixed_enum_types.Summary>,

clang/lib/Sema/SemaChecking.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14688,7 +14688,21 @@ bool Sema::BuiltinVectorToScalarMath(CallExpr *TheCall) {
1468814688
return false;
1468914689
}
1469014690

14691-
bool Sema::BuiltinVectorMath(CallExpr *TheCall, QualType &Res, bool FPOnly) {
14691+
static bool checkBuiltinVectorMathMixedEnums(Sema &S, Expr *LHS, Expr *RHS,
14692+
SourceLocation Loc) {
14693+
QualType L = LHS->getEnumCoercedType(S.Context),
14694+
R = RHS->getEnumCoercedType(S.Context);
14695+
if (L->isUnscopedEnumerationType() && R->isUnscopedEnumerationType() &&
14696+
!S.Context.hasSameUnqualifiedType(L, R)) {
14697+
return S.Diag(Loc, diag::err_conv_mixed_enum_types)
14698+
<< LHS->getSourceRange() << RHS->getSourceRange()
14699+
<< /*Arithmetic Between*/ 0 << L << R;
14700+
}
14701+
return false;
14702+
}
14703+
14704+
std::optional<QualType> Sema::BuiltinVectorMath(CallExpr *TheCall,
14705+
bool FPOnly) {
1469214706
if (checkArgCount(TheCall, 2))
1469314707
return true;
1469414708

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,8 +1518,8 @@ static void checkEnumArithmeticConversions(Sema &S, Expr *LHS, Expr *RHS,
15181518
unsigned DiagID;
15191519
// In C++ 26, usual arithmetic conversions between 2 different enum types
15201520
// are ill-formed.
1521-
if (S.getLangOpts().CPlusPlus26)
1522-
DiagID = diag::err_conv_mixed_enum_types_cxx26;
1521+
if (getLangOpts().CPlusPlus26)
1522+
DiagID = diag::warn_conv_mixed_enum_types_cxx26;
15231523
else if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() ||
15241524
!R->castAs<EnumType>()->getDecl()->hasNameForLinkage()) {
15251525
// If either enumeration type is unnamed, it's less likely that the
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify -triple %itanium_abi_triple
1+
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both,expected
2+
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both -Wno-enum-enum-conversion
23

34
enum E1 { e };
45
enum E2 { f };
56
void test() {
6-
int b = e <= 3.7; // expected-error {{invalid comparison of enumeration type 'E1' with floating-point type 'double'}}
7+
int b = e <= 3.7; // both-error {{invalid comparison of enumeration type 'E1' with floating-point type 'double'}}
78
int k = f - e; // expected-error {{invalid arithmetic between different enumeration types ('E2' and 'E1')}}
89
int x = 1 ? e : f; // expected-error {{invalid conditional expression between different enumeration types ('E1' and 'E2')}}
910
}

0 commit comments

Comments
 (0)