[PATCH] D146030: [clang][Interp] Handle LambdaExprs

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 7 22:38:00 PDT 2023


tbaeder added inline comments.


================
Comment at: clang/test/AST/Interp/lambda.cpp:92
+  static_assert(foo() == 1); // expected-error {{not an integral constant expression}}
+}
+
----------------
tbaeder wrote:
> aaron.ballman wrote:
> > How about some tests like:
> > ```
> > constexpr int call_thru_func_ptr(int i) {
> >   auto l = [](int i) { return i; };
> >   int (*fp)(int) = l;
> >   return fp(i);  
> > }
> > static_assert(call_thru_func_ptr(12) == 12);
> > 
> > constexpr int call_through_copied_lambda(auto lam, int i) {
> >   auto copy = lam;
> >   return copy(i);
> > }
> > 
> > constexpr int call_through_copied_lambda(auto lam) {
> >   auto copy = lam;
> >   return copy();
> > }
> > 
> > void func() {
> >   constexpr int i = 12;
> >   static_assert(call_through_copied_lambda([i]() { return i; }) == 12);
> > }
> > ```
> Heh:
> ```
> array.cpp:1245:15: error: static assertion expression is not an integral constant expression
>  1245 | static_assert(call_thru_func_ptr(12) == 12);
>       |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> array.cpp:1243:10: note: non-constexpr function '__invoke' cannot be used in a constant expression
>  1243 |   return fp(i);
>       |          ^
> array.cpp:1245:15: note: in call to 'call_thru_func_ptr(12)'
>  1245 | static_assert(call_thru_func_ptr(12) == 12);
>       |               ^
> array.cpp:1239:12: note: declared here
>  1239 |   auto l = [](int i) { return i; };
>       |            ^
> 
> ```
Ah, I didn't know there is something like a "lambda static invoker". I see the current interpreter basically checks for that and then calls the lambda call operator instead. Doing that is hard for me though, because the call operator requires different arguments and I can't just itnogre the static invoker either because it has an empty body.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146030/new/

https://reviews.llvm.org/D146030



More information about the cfe-commits mailing list