Skip to content

[Clang] Demote mixed enumeration arithmetic error to a warning #131811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025

Conversation

cor3ntin
Copy link
Contributor

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes #92340

In C++, defaulted to an error.

C++ removed these features but the removal negatively impact
users.

Fixes llvm#92340
@cor3ntin cor3ntin requested a review from AaronBallman March 18, 2025 14:38
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes #92340


Full diff: https://github.com/llvm/llvm-project/pull/131811.diff

4 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+5-1)
  • (modified) clang/lib/Sema/SemaChecking.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+1-1)
  • (modified) clang/test/SemaCXX/cxx2c-enum-compare.cpp (+3-2)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a88af192fb2a4..6996e7ec1cb29 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7639,9 +7639,13 @@ def warn_arith_conv_mixed_enum_types_cxx20 : Warning<
   "%sub{select_arith_conv_kind}0 "
   "different enumeration types%diff{ ($ and $)|}1,2 is deprecated">,
   InGroup<DeprecatedEnumEnumConversion>;
-def err_conv_mixed_enum_types_cxx26 : Error<
+
+def err_conv_mixed_enum_types: Error <
   "invalid %sub{select_arith_conv_kind}0 "
   "different enumeration types%diff{ ($ and $)|}1,2">;
+def warn_conv_mixed_enum_types_cxx26 : Warning <
+  err_conv_mixed_enum_types.Summary>,
+  InGroup<EnumEnumConversion>, DefaultError;
 
 def warn_arith_conv_mixed_anon_enum_types : Warning<
   warn_arith_conv_mixed_enum_types.Summary>,
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index c6feb35efce85..fc8994c65659f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -15323,7 +15323,7 @@ static bool checkBuiltinVectorMathMixedEnums(Sema &S, Expr *LHS, Expr *RHS,
            R = RHS->getEnumCoercedType(S.Context);
   if (L->isUnscopedEnumerationType() && R->isUnscopedEnumerationType() &&
       !S.Context.hasSameUnqualifiedType(L, R)) {
-    return S.Diag(Loc, diag::err_conv_mixed_enum_types_cxx26)
+    return S.Diag(Loc, diag::err_conv_mixed_enum_types)
            << LHS->getSourceRange() << RHS->getSourceRange()
            << /*Arithmetic Between*/ 0 << L << R;
   }
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e19136b394800..3ff372bdc9d0c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1533,7 +1533,7 @@ void Sema::checkEnumArithmeticConversions(Expr *LHS, Expr *RHS,
     // In C++ 26, usual arithmetic conversions between 2 different enum types
     // are ill-formed.
     if (getLangOpts().CPlusPlus26)
-      DiagID = diag::err_conv_mixed_enum_types_cxx26;
+      DiagID = diag::warn_conv_mixed_enum_types_cxx26;
     else if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() ||
              !R->castAs<EnumType>()->getDecl()->hasNameForLinkage()) {
       // If either enumeration type is unnamed, it's less likely that the
diff --git a/clang/test/SemaCXX/cxx2c-enum-compare.cpp b/clang/test/SemaCXX/cxx2c-enum-compare.cpp
index f47278a60725e..1a7363ecf0be4 100644
--- a/clang/test/SemaCXX/cxx2c-enum-compare.cpp
+++ b/clang/test/SemaCXX/cxx2c-enum-compare.cpp
@@ -1,9 +1,10 @@
-// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify -triple %itanium_abi_triple
+// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both,expected
+// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both,nowarn -Wno-enum-enum-conversion
 
 enum E1 { e };
 enum E2 { f };
 void test() {
-    int b = e <= 3.7; // expected-error {{invalid comparison of enumeration type 'E1' with floating-point type 'double'}}
+    int b = e <= 3.7; // both-error {{invalid comparison of enumeration type 'E1' with floating-point type 'double'}}
     int k = f - e; // expected-error {{invalid arithmetic between different enumeration types ('E2' and 'E1')}}
     int x = 1 ? e : f; // expected-error {{invalid conditional expression between different enumeration types ('E1' and 'E2')}}
 }

@cor3ntin
Copy link
Contributor Author

(this should be backported)

@@ -1,9 +1,10 @@
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify -triple %itanium_abi_triple
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both,expected
// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify=both,nowarn -Wno-enum-enum-conversion
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nowarn is unused, but I think it should be silencing the invalid comparison diagnostic, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing with double seems a lot weirder than comparing with another enum and no one complained about that; I think it should remain an error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you're missing test coverage for when -Wno-enum-enum-conversion disables the diagnostic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I was misreading the test, there is test coverage for this. Sorry!

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but be sure to add a release note on the backport so users know about the change in behavior.

@cor3ntin cor3ntin merged commit e4d910e into llvm:main Mar 18, 2025
7 of 10 checks passed
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Mar 18, 2025
@cor3ntin
Copy link
Contributor Author

/cherry-pick e4d910e

@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

Failed to cherry-pick: e4d910e

https://github.com/llvm/llvm-project/actions/runs/13927528748

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

cor3ntin added a commit to cor3ntin/llvm-project that referenced this pull request Mar 18, 2025
…ing (llvm#131811)

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes llvm#92340
@tstellar tstellar moved this from Needs Triage to Needs Pull Request in LLVM Release Status Mar 18, 2025
@tstellar
Copy link
Collaborator

@cor3ntin Were you able to manually create a backport PR for this?

@cor3ntin
Copy link
Contributor Author

@tstellar it slipped my mind, I can do that later today though

cor3ntin added a commit to cor3ntin/llvm-project that referenced this pull request May 10, 2025
…131811)

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes llvm#92340
cor3ntin added a commit to cor3ntin/llvm-project that referenced this pull request May 10, 2025
…131811)

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes llvm#92340
@cor3ntin
Copy link
Contributor Author

@tstellar #139396

@tstellar tstellar moved this from Needs Backport PR to Done in LLVM Release Status May 13, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request May 24, 2025
…131811)

In C++, defaulted to an error.

C++ removed these features but the removal negatively impacts users.

Fixes llvm#92340
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category release:cherry-pick-failed
Projects
Development

Successfully merging this pull request may close these issues.

unsuppressable error about invalid bitwise operation between different enumeration types
4 participants