Skip to content

Commit e4467fb

Browse files
authored
[clang][ExprConst] Handle dependent switch case statements (#166533)
By rejecting them. Fixes #165555
1 parent ee0818a commit e4467fb

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6007,6 +6007,8 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
60076007
CaseLabels[SC] = this->getLabel();
60086008

60096009
const Expr *Value = CS->getLHS();
6010+
if (Value->isValueDependent())
6011+
return false;
60106012
PrimType ValueT = this->classifyPrim(Value->getType());
60116013

60126014
// Compare the case statement's value to the switch condition.

clang/lib/AST/ExprConstant.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,10 +5452,13 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
54525452
}
54535453

54545454
const CaseStmt *CS = cast<CaseStmt>(SC);
5455-
APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx);
5456-
APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx)
5457-
: LHS;
5458-
if (LHS <= Value && Value <= RHS) {
5455+
const Expr *LHS = CS->getLHS();
5456+
const Expr *RHS = CS->getRHS();
5457+
if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5458+
return ESR_Failed;
5459+
APSInt LHSValue = LHS->EvaluateKnownConstInt(Info.Ctx);
5460+
APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Info.Ctx) : LHSValue;
5461+
if (LHSValue <= Value && Value <= RHSValue) {
54595462
Found = SC;
54605463
break;
54615464
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang_cc1 -std=c++20 %s -verify
2+
// RUN: %clang_cc1 -std=c++20 %s -verify -fexperimental-new-constant-interpreter
3+
4+
constexpr bool e(int){switch(0)0=0:return t(;} // expected-error {{expression is not assignable}} \
5+
// expected-error {{expected 'case' keyword before expression}} \
6+
// expected-error {{expected expression}}

0 commit comments

Comments
 (0)