diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -13533,7 +13533,9 @@ return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); } - if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) { + if (Info.Ctx.getLangOpts().CPlusPlus && + Info.EvalMode == EvalInfo::EM_ConstantExpression && + DestType->isEnumeralType()) { const EnumType *ET = dyn_cast(DestType.getCanonicalType()); const EnumDecl *ED = ET->getDecl(); // Check that the value is within the range of the enumeration values. diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -2421,6 +2421,7 @@ constexpr E1 x1 = static_cast(-8); constexpr E1 x2 = static_cast(8); // expected-error@-1 {{integer value 8 is outside the valid range of values [-8, 7] for this enumeration type}} + E1 x2b = static_cast(8); // ok, not a constant expression context constexpr E2 x3 = static_cast(-8); // expected-error@-1 {{integer value -8 is outside the valid range of values [0, 7] for this enumeration type}}