[clang] [clang] Avoid superfluous 'const' qualifiers in composite pointer type (PR #88905)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 07:43:29 PDT 2024


https://github.com/offsetof created https://github.com/llvm/llvm-project/pull/88905

Avoid adding `const` on too many levels to the qualification-combined type when going from "array of `N`" to "array of unknown bound of".

Fixes #66599

>From b32f7ef167af02a1640e7d336a139ef43b703460 Mon Sep 17 00:00:00 2001
From: offsetof <131769984+offsetof at users.noreply.github.com>
Date: Tue, 16 Apr 2024 14:12:47 +0000
Subject: [PATCH] [clang] Avoid superfluous 'const' qualifiers in composite
 pointer type

Fix too many levels of 'const' being added to the computed
qualification-combined type when conversions to arrays of unknown
bound are involved, so that (for example) applying the conditional
operator (?:) to operands of types "T (*)[N]" and "T (*)[]" correctly
yields a "T (*)[]" and not "const T (*)[]".
---
 clang/lib/Sema/SemaExprCXX.cpp                |  4 +--
 .../test/SemaCXX/cxx20-p0388-unbound-ary.cpp  | 26 ++++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 74ed3fe7bd5201..e704ca28bed7cf 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7260,8 +7260,8 @@ QualType Sema::FindCompositePointerType(SourceLocation Loc,
         Composite1 = Arr1->getElementType();
         Composite2 = Arr2->getElementType();
         Steps.emplace_back(Step::Array);
-        if (CAT1 || CAT2)
-          NeedConstBefore = Steps.size();
+        if ((CAT1 || CAT2) && Steps.size() > 2)
+          NeedConstBefore = Steps.size() - 2;
         continue;
       }
     }
diff --git a/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp b/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
index f2d5cabad235d0..0e428110d7988c 100644
--- a/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
@@ -170,4 +170,28 @@ void g3() {
 
 } // namespace Eight
 
-#endif
+namespace Nine {
+
+template<auto, class>
+constexpr bool is_of_type = false;
+
+template<class T, T expr>
+constexpr bool is_of_type<expr, T> = true;
+
+using T1 = int (*)[1];
+using T2 = int (*)[];
+static_assert(is_of_type<0 ? T1() : T2(), T2>);
+
+using U1 = int* (**)[1];
+using U2 = int* (**)[];
+using U3 = int* (* const*)[];
+static_assert(is_of_type<0 ? U1() : U2(), U3>);
+
+using V1 = int* (*(**)[])[1];
+using V2 = int* (*(**)[1])[];
+using V3 = int* (* const(* const*)[])[];
+static_assert(is_of_type<0 ? V1() : V2(), V3>);
+
+} // namespace Nine
+
+#endif // __cplusplus >= 202002



More information about the cfe-commits mailing list