[clang] [clang] Fix array types comparison in getCommonSugaredType (PR #131649)

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 19 10:06:37 PDT 2025


================
@@ -14171,6 +14171,15 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X,
 static auto unwrapSugar(SplitQualType &T, Qualifiers &QTotal) {
   SmallVector<SplitQualType, 8> R;
   while (true) {
+    if (const auto *ATy = dyn_cast<ArrayType>(T.Ty)) {
+      // C++ 9.3.3.4p3: Any type of the form "cv-qualifier-seq array of N U" is
+      // adjusted to "array of N cv-qualifier-seq U".
+      // C23 6.7.3p10: If the specification of an array type includes any type
+      // qualifiers, both the array and the element type are so-qualified.
+      //
+      // To simplify comparison remove the redundant qualifiers from the array.
+      T.Quals.removeCVRQualifiers(Qualifiers::Const | Qualifiers::Volatile);
----------------
spavloff wrote:

This change makes sense, it can fix te case of "array of arry", but it cannot fix the case from https://github.com/llvm/llvm-project/issues/97005:
```
bool a;
constexpr const unsigned char c[] = { 5 };
constexpr const unsigned char d[1] = { 0 };
auto b = (a ? d : c);
```
Here types of `d` and `d` do not contain sugar, corresponding canonical types are identical, `getCommonNonSugarTypeNode` is not called and `getCommonArrayElementType` is not called. However their types are not equal and `assert(QX == QY)` fails.

https://github.com/llvm/llvm-project/pull/131649


More information about the cfe-commits mailing list