Skip to content

Commit c7a10ef

Browse files
committed
correctly handle dependent conditions
1 parent b26b29d commit c7a10ef

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20644,7 +20644,7 @@ Sema::ConditionResult Sema::ActOnCondition(Scope *S, SourceLocation Loc,
2064420644
} else if (Cond.isUsable() && !isa<FullExpr>(Cond.get()))
2064520645
Cond = ActOnFinishFullExpr(Cond.get(), Loc, /*DiscardedValue*/ false);
2064620646

20647-
if (Cond.isInvalid())
20647+
if (!Cond.isUsable())
2064820648
return ConditionError();
2064920649

2065020650
return ConditionResult(*this, nullptr, Cond,

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4561,6 +4561,13 @@ bool TreeTransform<Derived>::TransformExprs(Expr *const *Inputs,
45614561
template <typename Derived>
45624562
Sema::ConditionResult TreeTransform<Derived>::TransformCondition(
45634563
SourceLocation Loc, VarDecl *Var, Expr *Expr, Sema::ConditionKind Kind) {
4564+
4565+
EnterExpressionEvaluationContext Eval(
4566+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated,
4567+
/*LambdaContextDecl=*/nullptr,
4568+
/*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
4569+
/*ShouldEnter=*/Kind == Sema::ConditionKind::ConstexprIf);
4570+
45644571
if (Var) {
45654572
VarDecl *ConditionVar = cast_or_null<VarDecl>(
45664573
getDerived().TransformDefinition(Var->getLocation(), Var));

clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ int f() {
256256
if constexpr((S{}, true)) { // expected-warning{{left operand of comma operator has no effect}}
257257
return 1;
258258
}
259-
if constexpr(S s; (s, true)) { // expected-warning{{left operand of comma operator has no effect}}
259+
if constexpr(S s; (S{}, true)) { // expected-warning{{left operand of comma operator has no effect}}
260260
return 1;
261261
}
262262
if constexpr(S s; (s, true)) { // expected-warning{{left operand of comma operator has no effect}}
@@ -267,8 +267,29 @@ int f() {
267267
}
268268
return 0;
269269
}
270+
271+
template <typename T>
272+
int f2() {
273+
if constexpr((T{}, true)) { // expected-warning{{left operand of comma operator has no effect}}
274+
return 1;
275+
}
276+
if constexpr(T s; (T{}, true)) { // expected-warning{{left operand of comma operator has no effect}}
277+
return 1;
278+
}
279+
if constexpr(T s; (s, true)) { // expected-warning{{left operand of comma operator has no effect}}
280+
return 1;
281+
}
282+
if constexpr(constexpr int _ = T{}.i; true) {
283+
return 1;
284+
}
285+
return 0;
286+
}
287+
288+
void test() {
289+
f2<S>(); // expected-note {{in instantiation}}
270290
}
271291

292+
}
272293
namespace GH120197{
273294
struct NonTrivialDtor {
274295
NonTrivialDtor() = default;

0 commit comments

Comments
 (0)