@@ -13650,7 +13650,11 @@ static QualType getCommonArrayElementType(ASTContext &Ctx, const T *X,
13650
13650
QualType EX = X->getElementType(), EY = Y->getElementType();
13651
13651
QualType R = Ctx.getCommonSugaredType(EX, EY,
13652
13652
/*Unqualified=*/true);
13653
+ // Qualifiers common to both element types.
13653
13654
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.
13654
13658
QX += EX.getQualifiers() - RQ;
13655
13659
QY += EY.getQualifiers() - RQ;
13656
13660
return R;
@@ -14371,6 +14375,22 @@ QualType ASTContext::getCommonSugaredType(QualType X, QualType Y,
14371
14375
// necessarily canonical types, as they may still have sugared properties.
14372
14376
// QX and QY will store the sum of all qualifiers in Xs and Ys respectively.
14373
14377
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
+
14374
14394
if (SX.Ty != SY.Ty) {
14375
14395
// The canonical nodes differ. Build a common canonical node out of the two,
14376
14396
// unifying their sugar. This may recurse back here.
@@ -14386,7 +14406,7 @@ QualType ASTContext::getCommonSugaredType(QualType X, QualType Y,
14386
14406
SY = Ys.pop_back_val();
14387
14407
}
14388
14408
}
14389
- if (Unqualified )
14409
+ if (KeepCommonQualifiers )
14390
14410
QX = Qualifiers::removeCommonQualifiers(QX, QY);
14391
14411
else
14392
14412
assert(QX == QY);
0 commit comments