Skip to content

[clang] Check constexpr int->enum conversions consistently. #143034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15245,21 +15245,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
}

if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext &&
Info.EvalMode == EvalInfo::EM_ConstantExpression &&
DestType->isEnumeralType()) {

bool ConstexprVar = true;

// We know if we are here that we are in a context that we might require
// a constant expression or a context that requires a constant
// value. But if we are initializing a value we don't know if it is a
// constexpr variable or not. We can check the EvaluatingDecl to determine
// if it constexpr or not. If not then we don't want to emit a diagnostic.
if (const auto *VD = dyn_cast_or_null<VarDecl>(
Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
ConstexprVar = VD->isConstexpr();

if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
const EnumType *ET = dyn_cast<EnumType>(DestType.getCanonicalType());
const EnumDecl *ED = ET->getDecl();
// Check that the value is within the range of the enumeration values.
Expand All @@ -15279,13 +15265,13 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
ED->getValueRange(Max, Min);
--Max;

if (ED->getNumNegativeBits() && ConstexprVar &&
if (ED->getNumNegativeBits() &&
(Max.slt(Result.getInt().getSExtValue()) ||
Min.sgt(Result.getInt().getSExtValue())))
Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
<< llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
<< Max.getSExtValue() << ED;
else if (!ED->getNumNegativeBits() && ConstexprVar &&
else if (!ED->getNumNegativeBits() &&
Max.ult(Result.getInt().getZExtValue()))
Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
<< llvm::toString(Result.getInt(), 10) << Min.getZExtValue()
Expand Down
7 changes: 7 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,9 @@ void testValueInRangeOfEnumerationValues() {
// expected-error@-1 {{constexpr variable 'x2' must be initialized by a constant expression}}
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
E1 x2b = static_cast<E1>(8); // ok, not a constant expression context
static_assert(static_cast<E1>(8), "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these are the cases my previous checks missed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The static_assert case wasn't emitting an error because of the Info.EvalMode == EvalInfo::EM_ConstantExpression check. The "neg_one_constexpr" case wasn't emitting an error because of the ConstexprVar check.

(Looking again, I guess I could have also come up with a separate testcase for the InConstantContext check, but I didn't really think too deeply about it.)

// expected-error@-1 {{static assertion expression is not an integral constant expression}}
// expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}

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

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

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