Skip to content

Commit bba8467

Browse files
[clang] Fix const eval of constexpr-unknown relational comparisons. (#150088)
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 e178e82 commit bba8467

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14636,7 +14636,9 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
1463614636
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
1463714637
bool WasArrayIndex;
1463814638
unsigned Mismatch = FindDesignatorMismatch(
14639-
getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex);
14639+
LHSValue.Base.isNull() ? QualType()
14640+
: getType(LHSValue.Base).getNonReferenceType(),
14641+
LHSDesignator, RHSDesignator, WasArrayIndex);
1464014642
// At the point where the designators diverge, the comparison has a
1464114643
// specified value if:
1464214644
// - we are comparing array indices
@@ -14680,7 +14682,7 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
1468014682
// compare pointers within the object in question; otherwise, the result
1468114683
// depends on where the object is located in memory.
1468214684
if (!LHSValue.Base.isNull() && IsRelational) {
14683-
QualType BaseTy = getType(LHSValue.Base);
14685+
QualType BaseTy = getType(LHSValue.Base).getNonReferenceType();
1468414686
if (BaseTy->isIncompleteType())
1468514687
return Error(E);
1468614688
CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,17 @@ 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; // interpreter-note{{declared here}}
396+
constexpr int bases = (void*)(X*)&z <= (Y*)&z; // expected-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+
// interpreter-note {{initializer of 'z' is unknown}}
399+
}

0 commit comments

Comments
 (0)