cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Unable to use enums in inspect expressions - `no matching function for call to 'is'`

Open filipsajdak opened this issue 3 years ago • 0 comments
trafficstars

The current implementation is not supporting enums in inspect expressions.

This cpp2 code

enum class lexeme : std::uint8_t {
    hash,
};

to_string: (e:lexeme) -> auto = {
    return inspect (e) -> std::string {
        is lexeme::hash = "hash";
        is _ = "INTERNAL_ERROR";
    };
}

compiles by cppfront to (skipping enum declaration):

[[nodiscard]] auto to_string(cpp2::in<lexeme> e) -> auto{
    return [&] () -> std::string { auto&& __expr = (e);
        if (cpp2::is<lexeme::hash>(__expr)) { if constexpr( requires{"hash";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF("hash"),std::string> ) return "hash"; else return std::string{}; else return std::string{}; }
        else return "INTERNAL_ERROR"; }
    ()
; }

and fails to compile with the cpp1 compiler with the error:

main.cpp2:26:13: error: no matching function for call to 'is'
[build]         if (cpp2::is<lexeme::hash>(__expr)) { if constexpr( requires{"hash";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF("hash"),std::string> ) return "hash"; else return std::string{}; else return std::string{}; }
[build]             ^~~~~~~~~~~~~~~~~~~~~~
[build] /Users/filipsajdak/dev/execspec/external/cppfront/include/cpp2util.h:517:6: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'C'

The issue is caused that enums cannot match to any provided is function overloads. To handle that we need special overload for enums.

filipsajdak avatar Oct 13 '22 20:10 filipsajdak