Skip to content

Commit 51b5621

Browse files
committed
[clang] Fix const eval of constexpr-unknown relational comparisons.
Like in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.) Fixes #150015.
1 parent 1e7ec35 commit 51b5621

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14631,8 +14631,9 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
1463114631
// - Otherwise pointer comparisons are unspecified.
1463214632
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
1463314633
bool WasArrayIndex;
14634-
unsigned Mismatch = FindDesignatorMismatch(
14635-
getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex);
14634+
unsigned Mismatch =
14635+
FindDesignatorMismatch(getType(LHSValue.Base).getNonReferenceType(),
14636+
LHSDesignator, RHSDesignator, WasArrayIndex);
1463614637
// At the point where the designators diverge, the comparison has a
1463714638
// specified value if:
1463814639
// - we are comparing array indices
@@ -14676,7 +14677,7 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
1467614677
// compare pointers within the object in question; otherwise, the result
1467714678
// depends on where the object is located in memory.
1467814679
if (!LHSValue.Base.isNull() && IsRelational) {
14679-
QualType BaseTy = getType(LHSValue.Base);
14680+
QualType BaseTy = getType(LHSValue.Base).getNonReferenceType();
1468014681
if (BaseTy->isIncompleteType())
1468114682
return Error(E);
1468214683
CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);

clang/test/SemaCXX/constant-expression-p2280r4.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,16 @@ namespace enable_if_2 {
383383
}
384384
}
385385
}
386+
387+
namespace GH150015 {
388+
extern int (& c)[8]; // interpreter-note {{declared here}}
389+
constexpr int x = c <= c+8; // interpreter-error {{constexpr variable 'x' must be initialized by a constant expression}} \
390+
// interpreter-note {{initializer of 'c' is unknown}}
391+
392+
struct X {};
393+
struct Y {};
394+
struct Z : X, Y {};
395+
extern Z z;
396+
constexpr int bases = (void*)(X*)&z <= (Y*)&z; // nointerpreter-error {{constexpr variable 'bases' must be initialized by a constant expression}} \
397+
// nointerpreter-note {{comparison of addresses of subobjects of different base classes has unspecified value}}
398+
}

0 commit comments

Comments
 (0)