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.06k stars 492 forks source link

On MSVC std::filesystem::path cannot be used as Lua type #1593

Closed zziger closed 3 months ago

zziger commented 3 months ago

On Windows compilation fails if std::filesystem::path type is passed to Lua. Similar issue happens when registering custom usertype or customization traits for std::filesystem::path.

Example code

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

#include <filesystem>
#include <iostream>

namespace sol {
    template <>
    struct is_container<std::filesystem::path> : std::false_type {};

    template <>
    struct is_to_stringable<std::filesystem::path> : std::false_type {};
}

int main(int, char*[]) {
    sol::state lua;
    lua.open_libraries();
    lua["test"] = std::filesystem::path("test");
    lua.script("print(test)");

    std::cout << std::endl;
    return 0;
}

Sol2 v3.3.0 This code does not compile on MSVC (VS 2022, MSVC 19.38.33133), but compiles on Windows with GCC (GCC 11.4.0, MinGW 13.2.0-rt_v11-rev0)

Compilation error message on MSVC

Expand error ``` C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(897): error C2664: 'int sol::stack::unqualified_pusher::push(lua_State *,wchar_t)': cannot convert argument 2 from 'const std::filesystem::path' to 'wchar_t' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(897): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_push.hpp(1226): note: see declaration of 'sol::stack::unqualified_pusher::push' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(897): note: while trying to match the argument list '(lua_State *, const std::filesystem::path)' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(897): note: the template instantiation context (the oldest one first) is main.cpp(23): note: see reference to function template instantiation 'sol::table_proxy> &&sol::table_proxy>::operator =(T &&) &&' being compiled with [ T=std::filesystem::path ] main.cpp(23): note: see the first reference to 'sol::table_proxy>::operator =' in 'main' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/table_proxy.hpp(122): note: see reference to function template instantiation 'sol::table_proxy> &&sol::table_proxy>::set<_Ty>(T &&) &&' being compiled with [ _Ty=std::filesystem::path, T=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/table_proxy.hpp(88): note: see reference to function template instantiation 'void sol::table_proxy>::tuple_set<0,_Ty>(std::integer_sequence,T &&) &&' being compiled with [ _Ty=std::filesystem::path, T=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/table_proxy.hpp(56): note: see reference to function template instantiation 'sol::basic_table_core &sol::basic_table_core::traverse_set(const char (&)[5],_Ty &&)' being compiled with [ _Ty=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/table_core.hpp(462): note: see reference to function template instantiation 'void sol::basic_table_core::traverse_set_deep(int,Key,_Ty &&) const' being compiled with [ _Ty=std::filesystem::path, Key=const char (&)[5] ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/table_core.hpp(264): note: see reference to function template instantiation 'void sol::stack::set_field(lua_State *,Key,Value &&,int)' being compiled with [ _Ty=std::filesystem::path, Key=const char (&)[5], Value=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(1250): note: see reference to function template instantiation 'void sol::stack::field_setter::set(lua_State *,Key,Value &&,int)' being compiled with [ _Ty=std::filesystem::path, Key=const char (&)[5], Value=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_field.hpp(217): note: see reference to function template instantiation 'int sol::stack::push<_Ty,>(lua_State *,T &&)' being compiled with [ _Ty=std::filesystem::path, T=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(878): note: see reference to function template instantiation 'int sol::stack::unqualified_pusher::push<_Ty>(lua_State *,_Ty &&)' being compiled with [ _Ty=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_push.hpp(372): note: see reference to function template instantiation 'int sol::stack::push,_Ty,,void>(lua_State *,Arg &&)' being compiled with [ T=Tu, _Ty=std::filesystem::path, Arg=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(897): note: see reference to function template instantiation 'int sol::stack::unqualified_pusher::push<_Ty,>(lua_State *,Arg &&)' being compiled with [ _Ty=std::filesystem::path, Arg=std::filesystem::path ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_push.hpp(186): note: see reference to function template instantiation 'int sol::stack::unqualified_pusher::push_keyed(lua_State *,K,_Ty &&)' being compiled with [ _Ty=std::filesystem::path, K=const std::string & ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_push.hpp(175): note: see reference to function template instantiation 'void sol::stack::stack_detail::set_undefined_methods_on(sol::stack_reference)' being compiled with [ T=Tu ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_core.hpp(193): note: see reference to function template instantiation 'void sol::detail::insert_default_registrations(IFx,Fx)' being compiled with [ IFx=sol::detail::indexed_insert &, Fx=bool (__cdecl &)(sol::meta_function) ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_core.hpp(126): note: see reference to class template instantiation 'sol::container_detail::u_c_launch>' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container_launch.hpp(132): note: while compiling class template member function 'int sol::container_detail::u_c_launch>::real_pairs_call(lua_State *)' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container_launch.hpp(133): note: see reference to alias template instantiation 'sol::container_detail::has_traits_pairs>>' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container_launch.hpp(133): note: see reference to class template instantiation 'sol::container_detail::has_traits_pairs_test>>' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(235): note: see reference to class template instantiation 'sol::usertype_container>' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1559): note: see reference to class template instantiation 'sol::container_detail::usertype_container_default,void>' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1366): note: while compiling class template member function 'int sol::container_detail::usertype_container_default,void>::pairs(lua_State *)' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container_launch.hpp(125): note: see the first reference to 'sol::container_detail::usertype_container_default,void>::pairs' in 'sol::container_detail::u_c_launch>::real_pairs_traits' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container_launch.hpp(133): note: see the first reference to 'sol::container_detail::u_c_launch>::real_pairs_traits' in 'sol::container_detail::u_c_launch>::real_pairs_call' C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1368): note: see reference to function template instantiation 'int sol::container_detail::usertype_container_default,void>::pairs_associative(std::false_type,lua_State *)' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1232): note: see reference to function template instantiation 'int sol::container_detail::usertype_container_default,void>::next_iter(lua_State *)' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1217): note: see reference to function template instantiation 'int sol::container_detail::usertype_container_default,void>::next_associative(std::false_type,lua_State *)' being compiled C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/usertype_container.hpp(1209): note: see reference to function template instantiation 'int sol::stack::stack_detail::push_reference(lua_State *,Arg)' being compiled with [ Arg=const std::filesystem::path & ] C:\Users\zziger\AppData\Local\.xmake\packages\s\sol2\v3.3.0\1aa9b23f2d2040d49728efabdcb8d03d\include\sol/stack_core.hpp(936): note: see reference to function template instantiation 'int sol::stack::push(lua_State *,Arg)' being compiled with [ Arg=const std::filesystem::path & ] > in main.cpp ```
zziger commented 3 months ago

My bad, seems like

namespace sol
{
    template <>
    struct is_automagical<std::filesystem::path> : std::false_type {};
}

fixes the error. Closing the issue.