Unfortunately I couldn't find an existing test file in test/SemaCXX that focused on lambdas in constant expressions.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang/lib/AST/Interp/ByteCodeEmitter.cpp | ||
---|---|---|
56–57 | UB alert -- _L is reserved; let's pick names without leading underscores or shadowing. | |
clang/lib/AST/Interp/ByteCodeEmitter.h | ||
73–74 | Can you add comments explaining what the unsigned, bool pair is encoding? | |
75 | Why is this a pointer? Since it's not really implemented, I'd recommend removing this for now and adding it back when you add support for capturing this. | |
clang/test/AST/Interp/lambda.cpp | ||
93 | 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); } |
clang/test/AST/Interp/lambda.cpp | ||
---|---|---|
93 | 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; }; | ^ |
clang/test/AST/Interp/lambda.cpp | ||
---|---|---|
93 | 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. |
clang/test/AST/Interp/lambda.cpp | ||
---|---|---|
93 | 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. |
clang/test/AST/Interp/lambda.cpp | ||
---|---|---|
93 | 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. |
clang/test/AST/Interp/lambda.cpp | ||
---|---|---|
6 | Fun case int constexpr f() { return [x = 10] { decltype(x) y; // type int b/c not odr use // refers to original init-capture auto &z = x; // type const int & b/c odr use // refers to lambdas copy of x y = 10; // Ok //z = 10; // Ill-formed return y; }(); } constexpr int x = f(); |
Can you add comments explaining what the unsigned, bool pair is encoding?