Skip to content

[clang] Implement CWG1719 "Layout compatibility and cv-qualification revisited" #82358

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 4 commits into from
Feb 21, 2024

Conversation

Endilll
Copy link
Contributor

@Endilll Endilll commented Feb 20, 2024

This patch updates our internal notion of layout-compatible, which in turn fixes __is_layout_compatible intrinsic.

…revisited"

This patch updates our internal notion of layout-compatible, which in turn fixes `__is_layout_compatible` intrinsic.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2024

@llvm/pr-subscribers-clang

Author: Vlad Serebrennikov (Endilll)

Changes

This patch updates our internal notion of layout-compatible, which in turn fixes __is_layout_compatible intrinsic.


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

5 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+6-3)
  • (modified) clang/lib/Sema/SemaChecking.cpp (+7-6)
  • (modified) clang/test/CXX/drs/dr17xx.cpp (+5-6)
  • (modified) clang/test/SemaCXX/type-traits.cpp (+7-7)
  • (modified) clang/www/cxx_dr_status.html (+2-2)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8ea4a9a5a4256c..ffdc2a2629e6e9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -98,9 +98,8 @@ C++20 Feature Support
 
 - Implemented the `__is_layout_compatible` intrinsic to support
   `P0466R5: Layout-compatibility and Pointer-interconvertibility Traits <https://wg21.link/P0466R5>`_.
-  Note: `CWG1719: Layout compatibility and cv-qualification revisited  <https://cplusplus.github.io/CWG/issues/1719.html>`_
-  and `CWG2759: [[no_unique_address] and common initial sequence <https://cplusplus.github.io/CWG/issues/2759.html>`_
-  are not yet implemented.
+  Note: `CWG2759: [[no_unique_address] and common initial sequence <https://cplusplus.github.io/CWG/issues/2759.html>`_
+  is not yet implemented.
 
 C++23 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
@@ -120,6 +119,10 @@ Resolutions to C++ Defect Reports
   in the template parameters, but is deduced from a previous argument.
   (`#78449: <https://github.com/llvm/llvm-project/issues/78449>`_).
 
+- ``const`` and ``volatile`` qualifiers are now ignored when evaluating
+  layout compatibility of two types.
+  (`CWG1719: Layout compatibility and cv-qualification revisited <https://cplusplus.github.io/CWG/issues/1719.html>`_).
+
 C Language Changes
 ------------------
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index d951c0fc2732d2..e8bfb215a5b4c5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -19124,15 +19124,16 @@ static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
   if (T1.isNull() || T2.isNull())
     return false;
 
-  // C++11 [basic.types] p11:
-  // If two types T1 and T2 are the same type, then T1 and T2 are
-  // layout-compatible types.
-  if (C.hasSameType(T1, T2))
-    return true;
-
+  // C++20 [basic.types] p11:
+  // Two types cv1 T1 and cv2 T2 are layout-compatible types
+  // if T1 and T2 are the same type, layout-compatible enumerations (9.7.1),
+  // or layout-compatible standard-layout class types (11.4).
   T1 = T1.getCanonicalType().getUnqualifiedType();
   T2 = T2.getCanonicalType().getUnqualifiedType();
 
+  if (C.hasSameType(T1, T2))
+    return true;
+
   const Type::TypeClass TC1 = T1->getTypeClass();
   const Type::TypeClass TC2 = T2->getTypeClass();
 
diff --git a/clang/test/CXX/drs/dr17xx.cpp b/clang/test/CXX/drs/dr17xx.cpp
index e5cee19337ebd4..d933c244aada42 100644
--- a/clang/test/CXX/drs/dr17xx.cpp
+++ b/clang/test/CXX/drs/dr17xx.cpp
@@ -46,7 +46,7 @@ namespace dr1715 { // dr1715: 3.9
 #endif
 }
 
-namespace dr1719 { // dr1719: no
+namespace dr1719 { // dr1719: 19
 #if __cplusplus >= 201103L
 struct CStruct {
   int one;
@@ -66,11 +66,10 @@ struct CStructWithQualifiers {
 static_assert(__is_layout_compatible(CStruct, const CStruct2), "");
 static_assert(__is_layout_compatible(CStruct, volatile CStruct2), "");
 static_assert(__is_layout_compatible(const CStruct, volatile CStruct2), "");
-// FIXME: all of the following pairs of types are layout-compatible
-static_assert(!__is_layout_compatible(int, const int), "");
-static_assert(!__is_layout_compatible(int, volatile int), "");
-static_assert(!__is_layout_compatible(const int, volatile int), "");
-static_assert(!__is_layout_compatible(CStruct, CStructWithQualifiers), "");
+static_assert(__is_layout_compatible(int, const int), "");
+static_assert(__is_layout_compatible(int, volatile int), "");
+static_assert(__is_layout_compatible(const int, volatile int), "");
+static_assert(__is_layout_compatible(CStruct, CStructWithQualifiers), "");
 #endif
 } // namespace dr1719
 
diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp
index 6ff04b6c8c7223..94889d654abeca 100644
--- a/clang/test/SemaCXX/type-traits.cpp
+++ b/clang/test/SemaCXX/type-traits.cpp
@@ -1711,13 +1711,13 @@ void is_layout_compatible(int n)
 {
   static_assert(__is_layout_compatible(void, void), "");
   static_assert(!__is_layout_compatible(void, int), "");
-  static_assert(!__is_layout_compatible(void, const void), ""); // FIXME: this is CWG1719
-  static_assert(!__is_layout_compatible(void, volatile void), ""); // FIXME: this is CWG1719
-  static_assert(!__is_layout_compatible(const int, volatile int), ""); // FIXME: this is CWG1719
+  static_assert(__is_layout_compatible(void, const void), "");
+  static_assert(__is_layout_compatible(void, volatile void), "");
+  static_assert(__is_layout_compatible(const int, volatile int), "");
   static_assert(__is_layout_compatible(int, int), "");
-  static_assert(!__is_layout_compatible(int, const int), ""); // FIXME: this is CWG1719
-  static_assert(!__is_layout_compatible(int, volatile int), ""); // FIXME: this is CWG1719
-  static_assert(!__is_layout_compatible(const int, volatile int), ""); // FIXME: this is CWG1719
+  static_assert(__is_layout_compatible(int, const int), "");
+  static_assert(__is_layout_compatible(int, volatile int), "");
+  static_assert(__is_layout_compatible(const int, volatile int), "");
   static_assert(!__is_layout_compatible(int, unsigned int), "");
   static_assert(!__is_layout_compatible(char, unsigned char), "");
   static_assert(!__is_layout_compatible(char, signed char), "");
@@ -1758,7 +1758,7 @@ void is_layout_compatible(int n)
   static_assert(!__is_layout_compatible(CppStructNonStandardByVirtBase, CppStructNonStandardByVirtBase2), "");
   static_assert(!__is_layout_compatible(CppStructNonStandardBySameBase, CppStructNonStandardBySameBase2), "");
   static_assert(!__is_layout_compatible(CppStructNonStandardBy2ndVirtBase, CppStructNonStandardBy2ndVirtBase2), "");
-  static_assert(!__is_layout_compatible(CStruct, CStructWithQualifiers), ""); // FIXME: this is CWG1719
+  static_assert(__is_layout_compatible(CStruct, CStructWithQualifiers), "");
   static_assert(__is_layout_compatible(CStruct, CStructNoUniqueAddress) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
   static_assert(__is_layout_compatible(CStructNoUniqueAddress, CStructNoUniqueAddress2) == bool(__has_cpp_attribute(no_unique_address)), ""); // FIXME: this is CWG2759
   static_assert(__is_layout_compatible(CStruct, CStructAlignment), "");
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index e9b18b1e283e66..38e2cb63142662 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -7812,7 +7812,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
     <td><a href="https://cplusplus.github.io/CWG/issues/1334.html">1334</a></td>
     <td>NAD</td>
     <td>Layout compatibility and cv-qualification</td>
-    <td class="unknown" align="center">Unknown</td>
+    <td class="unreleased" align="center">Superseded by <a href="#1719">1719</a></td>
   </tr>
   <tr id="1335">
     <td><a href="https://cplusplus.github.io/CWG/issues/1335.html">1335</a></td>
@@ -10122,7 +10122,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
     <td><a href="https://cplusplus.github.io/CWG/issues/1719.html">1719</a></td>
     <td>CD4</td>
     <td>Layout compatibility and cv-qualification revisited</td>
-    <td class="unknown" align="center">Unknown</td>
+    <td class="unreleased" align="center">Clang 19</td>
   </tr>
   <tr id="1720">
     <td><a href="https://cplusplus.github.io/CWG/issues/1720.html">1720</a></td>

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!

@Endilll Endilll added the c++ label Feb 21, 2024
@Endilll Endilll merged commit 7318585 into llvm:main Feb 21, 2024
@Endilll Endilll deleted the cwg1719 branch February 21, 2024 15:02
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Mar 21, 2024
…9a3027d34

Local branch amd-gfx e849a30 Merged main:6d160a49c2e7f36367de3f61f0460e28921450d5 into amd-gfx:71200611d382
Remote branch main 7318585 [clang] Implement CWG1719 "Layout compatibility and cv-qualification revisited" (llvm#82358)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ 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.

4 participants