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

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 8 04:33:32 PDT 2023


aaron.ballman 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:
> tbaeder wrote:
> > 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.
> Okay, I think I figured it out, I'm just special-casing the static invoker and emitting byte code for its body manually. This will make the patch larger though.
Hmmm, aren't you going to need to emit a body for the static invoker for calling through a function pointer to it (the `call_thru_func_ptr` example)? I would imagine you'd need to emit both the static invoker and the call operator and have the static invoker call the call operator.


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

https://reviews.llvm.org/D146030



More information about the cfe-commits mailing list