]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Correct behaviour of layout_compatible_type for aligned types
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Nov 2025 11:11:35 +0000 (22:11 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Nov 2025 23:45:08 +0000 (10:45 +1100)
The standard does not require two types to have the same alignment (and
hence size) to be considered layout-compatible.  The same applies to
members of unions.

gcc/cp/ChangeLog:

* typeck.cc (layout_compatible_type_p): Do not check TYPE_SIZE.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/is-layout-compatible3.C: Adjust expected results.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp2a/is-layout-compatible3.C

index 2ab45f3fff6ec374fe63d076588340b153114b69..0b8ef84bf50aeda80a2b02c16384863033eeb507 100644 (file)
@@ -1883,14 +1883,12 @@ layout_compatible_type_p (tree type1, tree type2)
   type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
 
   if (TREE_CODE (type1) == ENUMERAL_TYPE)
-    return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
-           && same_type_p (finish_underlying_type (type1),
-                           finish_underlying_type (type2)));
+    return same_type_p (finish_underlying_type (type1),
+                       finish_underlying_type (type2));
 
   if (CLASS_TYPE_P (type1)
       && std_layout_type_p (type1)
-      && std_layout_type_p (type2)
-      && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
+      && std_layout_type_p (type2))
     {
       tree field1 = TYPE_FIELDS (type1);
       tree field2 = TYPE_FIELDS (type2);
index 499ba4977d4107afe1c673122cec2e05adb6a6ee..e48679fef11ffc338d5ee36ccd1ecb652d1c743b 100644 (file)
@@ -51,15 +51,15 @@ struct B1 { signed int b; };
 struct alignas (16) C1 : public A1 {};
 struct alignas (16) D1 : public B1 {};
 
-static_assert (!std::is_layout_compatible_v<I, J>);
+static_assert (std::is_layout_compatible_v<I, J>);
 static_assert (!std::is_layout_compatible_v<K, L>);
-static_assert (!std::is_layout_compatible_v<M, N>);
+static_assert (std::is_layout_compatible_v<M, N>);
 static_assert (!std::is_layout_compatible_v<O, P>);
 static_assert (!std::is_layout_compatible_v<P, D>);
 static_assert (std::is_layout_compatible_v<Q, R>);
 static_assert (!std::is_layout_compatible_v<U, V>);
-static_assert (!std::is_layout_compatible_v<A, I>);
-static_assert (!std::is_layout_compatible_v<C, I>);
+static_assert (std::is_layout_compatible_v<A, I>);
+static_assert (std::is_layout_compatible_v<C, I>);
 static_assert (!std::is_layout_compatible_v<E, F>);
 static_assert (std::is_layout_compatible_v<G, H>);
 static_assert (std::is_layout_compatible_v<C1, D1>);