Skip to content

Commit 74d1a40

Browse files
authored
[Clang] Ignore assumptions with side effects at compile time (#85534)
Fixes #85519.
1 parent 9d5edfd commit 74d1a40

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,6 +5594,9 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
55945594
if (Assumption->isValueDependent())
55955595
return ESR_Failed;
55965596

5597+
if (Assumption->HasSideEffects(Info.getCtx()))
5598+
continue;
5599+
55975600
bool Value;
55985601
if (!EvaluateAsBooleanCondition(Assumption, Value, Info))
55995602
return ESR_Failed;

clang/test/SemaCXX/cxx23-assume.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,13 @@ static_assert(f5<D>() == 1); // expected-note 3 {{while checking constraint sati
126126
static_assert(f5<double>() == 2);
127127
static_assert(f5<E>() == 1); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
128128
static_assert(f5<F>() == 2); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
129+
130+
// Do not validate assumptions whose evaluation would have side-effects.
131+
constexpr int foo() {
132+
int a = 0;
133+
[[assume(a++)]] [[assume(++a)]]; // expected-warning 2 {{has side effects that will be discarded}} ext-warning 2 {{C++23 extension}}
134+
[[assume((a+=1))]]; // expected-warning {{has side effects that will be discarded}} ext-warning {{C++23 extension}}
135+
return a;
136+
}
137+
138+
static_assert(foo() == 0);

0 commit comments

Comments
 (0)