ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

sol::main_protected_function uses wrong function pointer when calls lambda when compiled with GCC #1444

Open elsid opened 1 year ago

elsid commented 1 year ago

Following code compiled with GCC and Clang produces different output:

#include <sol/state.hpp>
#include <sol/object.hpp>
#include <sol/forward.hpp>

#include <cstdio>

int main() {
    sol::state state;
    auto f0 = [] { std::printf("f0\n"); };
    auto f1 = [] { std::printf("f1\n"); };
    sol::main_protected_function f0_object = sol::make_object(state, f0);
    sol::main_protected_function f1_object = sol::make_object(state, f1);
    f1_object();
    return 0;
}

Clang built program produces correct output:

f1

GCC is wrong:

f0

https://godbolt.org/z/dKG4rbdoe

Running this under debugger shows that here fx has value of &decltype(f0)::operator() instead of &decltype(f1)::operator().

elsid commented 1 year ago

The problem is in sol::usertype_traits::metatable function. It produces the same result for both lambda types because it relies on __PRETTY_FUNCTION__ therefore function pointer for the second one is not registered. Clang doesn't have this problem because __PRETTY_FUNCTION__ produces different results for different lambdas: https://godbolt.org/z/PxGrhMh7a.