diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -191,8 +191,14 @@ - Correctly set expression evaluation context as 'immediate function context' in consteval functions. - This fixes `GH51182 ` - + This fixes `GH51182 `_. + +- Skip re-building lambda expressions when they appear as parameters to an immediate invocation. + This fixes `GH56183 `_, + `GH51695 `_, + `GH50455 `_, + `GH54872 `_, + `GH54587 `_. C++2b Feature Support ^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -17598,6 +17598,11 @@ DRSet.erase(E); return E; } + ExprResult TransformLambdaExpr(LambdaExpr *E) { + // Lambdas must be built already. They must not be re-built as it always + // creates new types. + return E; + } bool AlwaysRebuild() { return false; } bool ReplacingOriginal() { return true; } bool AllowSkippingCXXConstructExpr() { diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -939,3 +939,77 @@ static_assert(max(1,2)==2); static_assert(mid(1,2,3)==2); } // namespace GH51182 + +// https://github.com/llvm/llvm-project/issues/56183 +namespace GH56183 { +consteval auto Foo(auto c) { return c; } +consteval auto Bar(auto f) { return f(); } +void test() { + constexpr auto x = Foo(Bar([] { return 'a'; })); + static_assert(x == 'a'); +} +} // namespace GH56183 + +// https://github.com/llvm/llvm-project/issues/51695 +namespace GH51695 { +// Original ======================================== +template +struct type_t {}; + +template +struct list_t {}; + +template +consteval auto pop_front(list_t) -> auto { + return list_t{}; +} + +template +consteval auto apply(list_t, F fn) -> auto { + return fn(type_t{}...); +} + +void test1() { + constexpr auto x = apply(pop_front(list_t{}), + [](type_t...) { return 42; }); + static_assert(x == 42); +} +// Reduced 1 ======================================== +consteval bool zero() { return false; } + +template +consteval bool foo(bool, F f) { + return f(); +} + +void test2() { + constexpr auto x = foo(zero(), []() { return true; }); + static_assert(x); +} + +// Reduced 2 ======================================== +template +consteval auto bar(F f) { return f;} + +void test3() { + constexpr auto x = bar(bar(bar(bar([]() { return true; }))))(); + static_assert(x); + + int a = 1; // expected-note {{declared here}} + auto y = bar(bar(bar(bar([=]() { return a; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}} + // expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}} +} + +} // namespace GH51695 + +// https://github.com/llvm/llvm-project/issues/50455 +namespace GH50455 { +void f() { + []() consteval { int i{}; }(); + []() consteval { int i{}; ++i; }(); +} +void g() { + (void)[](int i) consteval { return i; }(0); + (void)[](int i) consteval { return i; }(0); +} +} // namespace GH50455