@@ -13650,7 +13650,11 @@ static QualType getCommonArrayElementType(ASTContext &Ctx, const T *X,
1365013650 QualType EX = X->getElementType(), EY = Y->getElementType();
1365113651 QualType R = Ctx.getCommonSugaredType(EX, EY,
1365213652 /*Unqualified=*/true);
13653+ // Qualifiers common to both element types.
1365313654 Qualifiers RQ = R.getQualifiers();
13655+ // For each side, move to the top level any qualifiers which are not common to
13656+ // both element types. The caller must assume top level qualifiers might
13657+ // be different, even if they are the same type, and can be treated as sugar.
1365413658 QX += EX.getQualifiers() - RQ;
1365513659 QY += EY.getQualifiers() - RQ;
1365613660 return R;
@@ -14371,6 +14375,22 @@ QualType ASTContext::getCommonSugaredType(QualType X, QualType Y,
1437114375 // necessarily canonical types, as they may still have sugared properties.
1437214376 // QX and QY will store the sum of all qualifiers in Xs and Ys respectively.
1437314377 auto Xs = ::unwrapSugar(SX, QX), Ys = ::unwrapSugar(SY, QY);
14378+
14379+ // If this is an ArrayType, the element qualifiers are interchangeable with
14380+ // the top level qualifiers.
14381+ // * In case the canonical nodes are the same, the elements types are already
14382+ // the same.
14383+ // * Otherwise, the element types will be made the same, and any different
14384+ // element qualifiers will be moved up to the top level qualifiers, per
14385+ // 'getCommonArrayElementType'.
14386+ // In both cases, this means there may be top level qualifiers which differ
14387+ // between X and Y. If so, these differing qualifiers are redundant with the
14388+ // element qualifiers, and can be removed without changing the canonical type.
14389+ // The desired behaviour is the same as for the 'Unqualified' case here:
14390+ // treat the redundant qualifiers as sugar, remove the ones which are not
14391+ // common to both sides.
14392+ bool KeepCommonQualifiers = Unqualified || isa<ArrayType>(SX.Ty);
14393+
1437414394 if (SX.Ty != SY.Ty) {
1437514395 // The canonical nodes differ. Build a common canonical node out of the two,
1437614396 // unifying their sugar. This may recurse back here.
@@ -14386,7 +14406,7 @@ QualType ASTContext::getCommonSugaredType(QualType X, QualType Y,
1438614406 SY = Ys.pop_back_val();
1438714407 }
1438814408 }
14389- if (Unqualified )
14409+ if (KeepCommonQualifiers )
1439014410 QX = Qualifiers::removeCommonQualifiers(QX, QY);
1439114411 else
1439214412 assert(QX == QY);
0 commit comments