Skip to content
Open
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
2 changes: 2 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5989,6 +5989,8 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
CaseLabels[SC] = this->getLabel();

const Expr *Value = CS->getLHS();
if (Value->isValueDependent())
return false;
PrimType ValueT = this->classifyPrim(Value->getType());

// Compare the case statement's value to the switch condition.
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5452,10 +5452,13 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
}

const CaseStmt *CS = cast<CaseStmt>(SC);
APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx);
APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx)
: LHS;
if (LHS <= Value && Value <= RHS) {
const Expr *LHS = CS->getLHS();
const Expr *RHS = CS->getRHS();
if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
return ESR_Failed;
APSInt LHSValue = LHS->EvaluateKnownConstInt(Info.Ctx);
APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Info.Ctx) : LHSValue;
if (LHSValue <= Value && Value <= RHSValue) {
Found = SC;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions clang/test/SemaCXX/dependent-switch-case.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -std=c++20 %s -verify
// RUN: %clang_cc1 -std=c++20 %s -verify -fexperimental-new-constant-interpreter

constexpr bool e(int){switch(0)0=0:return t(;} // expected-error {{expression is not assignable}} \
// expected-error {{expected 'case' keyword before expression}} \
// expected-error {{expected expression}}