Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] No support for enum as basetype #816

Open
dwmckee opened this issue Nov 12, 2023 · 2 comments
Open

[BUG] No support for enum as basetype #816

dwmckee opened this issue Nov 12, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@dwmckee
Copy link

dwmckee commented Nov 12, 2023

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 76c2941
  • 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 dwmckee added the bug Something isn't working label Nov 12, 2023
@gregmarr
Copy link
Contributor

@dwmckee Have you tried this branch? (I'm not sure if it adds this, but it's ongoing work.) #701

@dwmckee
Copy link
Author

dwmckee commented Nov 14, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants