diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 7859871b0493a..372a95c80717c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -294,6 +294,7 @@ Bug Fixes to C++ Support direct-list-initialized from an array is corrected to direct-initialization. - Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327) - Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386) +- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524) Improvements to C++ diagnostics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp index 33a90e0cb8a42..26be78ee8ca15 100644 --- a/clang/lib/Parse/ParseExprCXX.cpp +++ b/clang/lib/Parse/ParseExprCXX.cpp @@ -2203,8 +2203,16 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc, return ParseCXXCondition(nullptr, Loc, CK, MissingOK); } - // Parse the expression. - ExprResult Expr = ParseExpression(); // expression + ExprResult Expr = [&] { + EnterExpressionEvaluationContext Eval( + Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated, + /*LambdaContextDecl=*/nullptr, + /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other, + /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf); + // Parse the expression. + return ParseExpression(); // expression + }(); + if (Expr.isInvalid()) return Sema::ConditionError(); diff --git a/clang/test/SemaCXX/constexpr-if.cpp b/clang/test/SemaCXX/constexpr-if.cpp new file mode 100644 index 0000000000000..494fc45c55c4e --- /dev/null +++ b/clang/test/SemaCXX/constexpr-if.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -std=c++20 -verify=cxx20,expected %s +// RUN: %clang_cc1 -std=c++23 -verify=cxx23,expected %s +// RUN: %clang_cc1 -std=c++26 -verify=cxx26,expected %s + +// expected-no-diagnostics + +namespace GH123524 { +consteval void fn1() {} +void fn2() { + if constexpr (&fn1 != nullptr) { } +} +}