cppfront
cppfront copied to clipboard
[BUG] No support for enum `as` basetype
Describe the bug
If as is to be a uniform cast syntax, then I would expect to be able to get raw values from enums with it, but that is not currently the case.
To Reproduce Steps to reproduce the behavior:
E: @enum<u8> type = {
Value1;
Value2;
Value3;
}
main: () = {
// v1 : u8 = E::Value1; // ERROR: can't implicitly convert as expected
v2 : u8 = E::Value2.get_raw_value(); // Expected working behavior
v3 : u8 = E::Value3 as u8; // ERROR: Uniform cast syntax doesn't work
}
Compilation environment:
- cppfront commit 76c2941b29e1e17f8086d3581df03d0bb12bc793
- g++ (SUSE Linux) 13.2.1 20230912 [revision b96e66fd4ef3e36983969fb8cdd1956f551a074b]
- Using meson with the official incantation
Discussion
Obviously, with get_raw_value available this is not pressing from a "being able to get things done" point of view, but I found it surprising. I expected that being explicit about wanting the raw value was sufficient.
** Edit note ** The way meson handles subprojects seems to be confusing me. Originally posted quoting quite an old commit, but the error persists on the current leading edge.
@dwmckee Have you tried this branch? (I'm not sure if it adds this, but it's ongoing work.) https://github.com/hsutter/cppfront/pull/701
@dwmckee Have you tried this branch? (I'm not sure if it adds this, but it's ongoing work.) #701
I have now.
[4/5] Compiling C++ object enum_as_basetype.p/meson-generated_enum_as_basetype.cpp.o
FAILED: enum_as_basetype.p/meson-generated_enum_as_basetype.cpp.o
c++ -Ienum_as_basetype.p -I. -I.. -I../subprojects/cppfront/include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++20 -O0 -g -MD -MQ enum_as_basetype.p/meson-generated_enum_as_basetype.cpp.o -MF enum_as_basetype.p/meson-generated_enum_as_basetype.cpp.o.d -o enum_as_basetype.p/meson-generated_enum_as_basetype.cpp.o -c enum_as_basetype.p/enum_as_basetype.cpp
In file included from enum_as_basetype.p/enum_as_basetype.cpp:6:
../subprojects/cppfront/include/cpp2util.h: In instantiation of ‘decltype(auto) cpp2::as_(X&&) [with C = unsigned char; X = const E&]’:
../enum_as_basetype.cpp2:10:38: required from here
../subprojects/cppfront/include/cpp2util.h:2022:13: error: static assertion failed: No safe 'as' cast available - please check your cast
2022 | program_violates_type_safety_guarantee<C, X>,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../subprojects/cppfront/include/cpp2util.h:2022:13: note: ‘cpp2::program_violates_type_safety_guarantee<unsigned char, const E&>’ evaluates to false
../enum_as_basetype.cpp2: In function ‘int main()’:
../enum_as_basetype.cpp2:10:38: error: cannot convert ‘cpp2::nonesuch_<>’ to ‘cpp2::u8’ {aka ‘unsigned char’} in initialization
10 | v3 : u8 = E::Value3 as u8; // ERROR: Uniform cast syntax doesn't work
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
| |
| cpp2::nonesuch_<>
../enum_as_basetype.cpp2:9:15: warning: unused variable ‘v2’ [-Wunused-variable]
9 | v2 : u8 = E::Value2.get_raw_value(); // Expected working behavior
| ^~
../enum_as_basetype.cpp2:10:15: warning: unused variable ‘v3’ [-Wunused-variable]
10 | v3 : u8 = E::Value3 as u8; // ERROR: Uniform cast syntax doesn't work
| ^~
ninja: build stopped: subcommand failed.