Skip to content
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

[Clang] Implement P2864R2 Remove Deprecated Arithmetic Conversion on… #73105

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

cor3ntin
Copy link
Contributor

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 22, 2023
@cor3ntin cor3ntin added c++ c++26 and removed clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 22, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 22, 2023

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

… Enumerations

https://isocpp.org/files/papers/P2864R2.pdf


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

5 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+9)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+11-5)
  • (added) clang/test/SemaCXX/cxx2c-enum-compare.cpp (+9)
  • (modified) clang/www/cxx_status.html (+1-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 157afd9e8629152..1a330dccdce58a8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -182,6 +182,9 @@ C++2c Feature Support
   This is applied to both C++ standard attributes, and other attributes supported by Clang.
   This completes the implementation of `P2361R6 Unevaluated Strings <https://wg21.link/P2361R6>`_
 
+- Implemented `P2864R2 Remove Deprecated Arithmetic Conversion on Enumerations From
+C++26 <https://wg21.link/P2864R2>`_.
+
 
 Resolutions to C++ Defect Reports
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 990692c06d7d3a8..7c9e5999d2c87fa 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7216,6 +7216,11 @@ def warn_arith_conv_enum_float_cxx20 : Warning<
   "%plural{2:with|4:from|:and}0 "
   "%select{enumeration|floating-point}1 type %3 is deprecated">,
   InGroup<DeprecatedEnumFloatConversion>;
+def err_arith_conv_enum_float_cxx26 : Error<
+  "invalid %sub{select_arith_conv_kind}0 "
+  "%select{floating-point|enumeration}1 type %2 "
+  "%plural{2:with|4:from|:and}0 "
+  "%select{enumeration|floating-point}1 type %3">;
 def warn_arith_conv_mixed_enum_types : Warning<
   "%sub{select_arith_conv_kind}0 "
   "different enumeration types%diff{ ($ and $)|}1,2">,
@@ -7224,6 +7229,10 @@ 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<
+  "invalid %sub{select_arith_conv_kind}0 "
+  "different enumeration types%diff{ ($ and $)|}1,2">;
+
 def warn_arith_conv_mixed_anon_enum_types : Warning<
   warn_arith_conv_mixed_enum_types.Summary>,
   InGroup<AnonEnumEnumConversion>, DefaultIgnore;
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fc39d6149c1cc65..d1b2b8084b8ffea 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1503,16 +1503,22 @@ static void checkEnumArithmeticConversions(Sema &S, Expr *LHS, Expr *RHS,
   bool IsCompAssign = ACK == Sema::ACK_CompAssign;
   if ((!IsCompAssign && LEnum && R->isFloatingType()) ||
       (REnum && L->isFloatingType())) {
-    S.Diag(Loc, S.getLangOpts().CPlusPlus20
+    S.Diag(Loc, S.getLangOpts().CPlusPlus26
+                    ? diag::err_arith_conv_enum_float_cxx26
+                : S.getLangOpts().CPlusPlus20
                     ? diag::warn_arith_conv_enum_float_cxx20
                     : diag::warn_arith_conv_enum_float)
-        << LHS->getSourceRange() << RHS->getSourceRange()
-        << (int)ACK << LEnum << L << R;
+        << LHS->getSourceRange() << RHS->getSourceRange() << (int)ACK << LEnum
+        << L << R;
   } else if (!IsCompAssign && LEnum && REnum &&
              !S.Context.hasSameUnqualifiedType(L, R)) {
     unsigned DiagID;
-    if (!L->castAs<EnumType>()->getDecl()->hasNameForLinkage() ||
-        !R->castAs<EnumType>()->getDecl()->hasNameForLinkage()) {
+    // In C++ 26, usual arithmetic conversions between 2 different enum types
+    // are ill-formed.
+    if (S.getLangOpts().CPlusPlus26)
+      DiagID = diag::err_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
       // user cares about this, but this situation is still deprecated in
       // C++2a. Use a different warning group.
diff --git a/clang/test/SemaCXX/cxx2c-enum-compare.cpp b/clang/test/SemaCXX/cxx2c-enum-compare.cpp
new file mode 100644
index 000000000000000..796136c774357bc
--- /dev/null
+++ b/clang/test/SemaCXX/cxx2c-enum-compare.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 %s -std=cxx2c -fsyntax-only -verify -triple %itanium_abi_triple
+
+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 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')}}
+}
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index 621439d0bae9666..9fb6b0cda4da50d 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -161,7 +161,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
  <tr>
   <td>Remove Deprecated Arithmetic Conversion on Enumerations</td>
   <td><a href="https://wg21.link/P2864R2">P2864R2</a></td>
-  <td class="none" align="center">No</td>
+  <td class="unreleased" align="center">Clang 18</td>
  </tr>
 
 </table>

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 22, 2023
@AaronBallman AaronBallman changed the title [Clang] Implement P28646R2 Remove Deprecated Arithmetic Conversion on… [Clang] Implement P2864R2 Remove Deprecated Arithmetic Conversion on… Nov 29, 2023
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 -- I'm a bit worried about breaking working code, but the user has to explicitly opt into C++26 mode and so it's only slightly unreasonable for them to have to change their previously correct, working code because of this. I say we leave it as an error, but if users complain about this being too strict, we can relax it to be a warning that defaults to an error or just leave it deprecated and support it forever.

Comment on lines 185 to 186
- Implemented `P2864R2 Remove Deprecated Arithmetic Conversion on Enumerations From
C++26 <https://wg21.link/P2864R2>`_.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Implemented `P2864R2 Remove Deprecated Arithmetic Conversion on Enumerations From
C++26 <https://wg21.link/P2864R2>`_.
- Implemented `P2864R2 Remove Deprecated Arithmetic Conversion on Enumerations From C++26 <https://wg21.link/P2864R2>`_.

This should fix the Sphinx build issues.

@cor3ntin cor3ntin merged commit 1cbd52f into llvm:main Nov 29, 2023
4 checks passed
@cor3ntin cor3ntin deleted the corentin/P2864R2 branch November 29, 2023 20:27
@dyung
Copy link
Collaborator

dyung commented Nov 29, 2023

@cor3ntin Your commit seems to be causing 2 test failures in the clang-tools-extra tests, can you try to fix them or revert if you need time to investigate?

https://lab.llvm.org/buildbot/#/builders/139/builds/54467

PiotrZSL added a commit that referenced this pull request Nov 29, 2023
Fixes failure in tests from a bugprone-suspicious-enum-usage check
by limiting those test to C++17 only to make CI green.
Tests were broken by change introduced in pull request #73105
@cor3ntin
Copy link
Contributor Author

@PiotrZSL You beat me to it, thanks a lot!

@PiotrZSL
Copy link
Member

Actually that is more like a quick fix, we can live with it. More proper way would be simply to split those tests into pre-c++20 and universal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ c++26 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants