Skip to content

Commit 1743947

Browse files
committed
Revert "[clang] Check constexpr int->enum conversions consistently. (llvm#143034)"
This reverts commit 6090232.
1 parent b8a171c commit 1743947

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15682,7 +15682,21 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1568215682
return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1568315683
}
1568415684

15685-
if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
15685+
if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext &&
15686+
Info.EvalMode == EvalInfo::EM_ConstantExpression &&
15687+
DestType->isEnumeralType()) {
15688+
15689+
bool ConstexprVar = true;
15690+
15691+
// We know if we are here that we are in a context that we might require
15692+
// a constant expression or a context that requires a constant
15693+
// value. But if we are initializing a value we don't know if it is a
15694+
// constexpr variable or not. We can check the EvaluatingDecl to determine
15695+
// if it constexpr or not. If not then we don't want to emit a diagnostic.
15696+
if (const auto *VD = dyn_cast_or_null<VarDecl>(
15697+
Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
15698+
ConstexprVar = VD->isConstexpr();
15699+
1568615700
const EnumType *ET = dyn_cast<EnumType>(DestType.getCanonicalType());
1568715701
const EnumDecl *ED = ET->getDecl();
1568815702
// Check that the value is within the range of the enumeration values.
@@ -15702,13 +15716,13 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1570215716
ED->getValueRange(Max, Min);
1570315717
--Max;
1570415718

15705-
if (ED->getNumNegativeBits() &&
15719+
if (ED->getNumNegativeBits() && ConstexprVar &&
1570615720
(Max.slt(Result.getInt().getSExtValue()) ||
1570715721
Min.sgt(Result.getInt().getSExtValue())))
1570815722
Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
1570915723
<< llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
1571015724
<< Max.getSExtValue() << ED;
15711-
else if (!ED->getNumNegativeBits() &&
15725+
else if (!ED->getNumNegativeBits() && ConstexprVar &&
1571215726
Max.ult(Result.getInt().getZExtValue()))
1571315727
Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
1571415728
<< llvm::toString(Result.getInt(), 10) << Min.getZExtValue()

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,9 +2514,6 @@ void testValueInRangeOfEnumerationValues() {
25142514
// expected-error@-1 {{constexpr variable 'x2' must be initialized by a constant expression}}
25152515
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
25162516
E1 x2b = static_cast<E1>(8); // ok, not a constant expression context
2517-
static_assert(static_cast<E1>(8), "");
2518-
// expected-error@-1 {{static assertion expression is not an integral constant expression}}
2519-
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
25202517

25212518
constexpr E2 x3 = static_cast<E2>(-8);
25222519
// expected-error@-1 {{constexpr variable 'x3' must be initialized by a constant expression}}
@@ -2555,10 +2552,6 @@ void testValueInRangeOfEnumerationValues() {
25552552
// expected-note@-2 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
25562553

25572554
const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context
2558-
constexpr NumberType neg_one_constexpr = neg_one;
2559-
// expected-error@-1 {{constexpr variable 'neg_one_constexpr' must be initialized by a constant expression}}
2560-
// expected-note@-2 {{initializer of 'neg_one' is not a constant expression}}
2561-
// expected-note@-4 {{declared here}}
25622555

25632556
CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE;
25642557
// expected-error@-1 {{constexpr variable 'system_enum' must be initialized by a constant expression}}

0 commit comments

Comments
 (0)