Skip to content

[BUG] Unable to compile cpp2 enum #1304

Closed
@MatthieuHernandez

Description

@MatthieuHernandez

I have a compilation error with Clang 16.0.5 when I try to declare an enum in cpp2. I use cppfront compiled from main branch (ccf7011).

compiler error

clang  -IC:/Programming/Git/gopp2/../cppfront/include -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -std=gnu++20 -MD -MT CMakeFiles/gopp2.dir/generate/main.cpp.obj -MF CMakeFiles\gopp2.dir\generate\main.cpp.obj.d -o CMakeFiles/gopp2.dir/generate/main.cpp.obj -c C:/Programming/Git/gopp2/generate/main.cpp
In file included from ../src/main.cpp2:2:
In file included from ../src/engine.h2:2:
In file included from ../src/move.h2:1:
../src/color.h2:12:18: error: constexpr constructor never produces a constant expression [-Winvalid-constexpr]
constexpr Color::Color()
                 ^
../src/color.h2:13:51: note: read of non-constexpr variable 'Black' is not allowed in a constant expression
                                        : _value{ Black._value }{}
                                                  ^
../src/color.h2:7:36: note: declared here
inline CPP2_CONSTEXPR Color Color::Black{ 0 };
                                   ^
1 error generated.

color.h2

Color: @enum<u8> type = {
    Black := 0;
    White := 1;
}

color.h

#ifndef COLOR_H_CPP2
#define COLOR_H_CPP2

#define CPP2_IMPORT_STD          Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "../src/color.h2"
class Color;
#line 2 "../src/color.h2"
    

//=== Cpp2 type definitions and function declarations ===========================

#line 1 "../src/color.h2"
class Color {
private: cpp2::u8 _value; private: constexpr Color(cpp2::impl::in<cpp2::i64> _val);

private: constexpr auto operator=(cpp2::impl::in<cpp2::i64> _val) -> Color& ;
public: static const Color Black;
public: static const Color White;
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::u8;
public: constexpr explicit Color();
public: constexpr Color(Color const& that);
public: constexpr auto operator=(Color const& that) -> Color& ;
public: constexpr Color(Color&& that) noexcept;
public: constexpr auto operator=(Color&& that) noexcept -> Color& ;
public: [[nodiscard]] auto operator<=>(Color const& that) const& -> std::strong_ordering = default;
public: [[nodiscard]] auto to_string_impl(cpp2::impl::in<std::string_view> prefix) const& -> std::string;
public: [[nodiscard]] auto to_string() const& -> std::string;
public: [[nodiscard]] auto to_code() const& -> std::string;
public: [[nodiscard]] static auto from_string(cpp2::impl::in<std::string_view> s) -> Color;
public: [[nodiscard]] static auto from_code(cpp2::impl::in<std::string_view> s) -> Color;

#line 4 "../src/color.h2"
};


//=== Cpp2 function definitions =================================================

#line 1 "../src/color.h2"

#line 1 "../src/color.h2"

constexpr Color::Color(cpp2::impl::in<cpp2::i64> _val)
                                                          : _value{ cpp2::unchecked_narrow<cpp2::u8>(_val) } {  }
constexpr auto Color::operator=(cpp2::impl::in<cpp2::i64> _val) -> Color&  { 
                                                          _value = cpp2::unchecked_narrow<cpp2::u8>(_val);
                                                          return *this; }
inline CPP2_CONSTEXPR Color Color::Black{ 0 };

inline CPP2_CONSTEXPR Color Color::White{ 1 };

[[nodiscard]] constexpr auto Color::get_raw_value() const& -> cpp2::u8 { return _value; }
constexpr Color::Color()
                                        : _value{ Black._value }{}
constexpr Color::Color(Color const& that)
                                              : _value{ that._value }{}
constexpr auto Color::operator=(Color const& that) -> Color& {
                                              _value = that._value;
                                              return *this;}
constexpr Color::Color(Color&& that) noexcept
                                              : _value{ std::move(that)._value }{}
constexpr auto Color::operator=(Color&& that) noexcept -> Color& {
                                              _value = std::move(that)._value;
                                              return *this;}
[[nodiscard]] auto Color::to_string_impl(cpp2::impl::in<std::string_view> prefix) const& -> std::string{

auto pref {cpp2::to_string(prefix)}; 
if ((*this) == Black) {return pref + "Black"; }
if ((*this) == White) {return cpp2::move(pref) + "White"; }
return "invalid Color value"; 
}

[[nodiscard]] auto Color::to_string() const& -> std::string { return to_string_impl(""); }
[[nodiscard]] auto Color::to_code() const& -> std::string { return to_string_impl("Color::"); }
[[nodiscard]] auto Color::from_string(cpp2::impl::in<std::string_view> s) -> Color{

auto x {s}; 
if ("Black" == x) {return Black; }
else {if ("White" == cpp2::move(x)) {return White; }
#line 1 "../src/color.h2"
}
CPP2_UFCS(report_violation)(cpp2::type_safety, CPP2_UFCS(c_str)(("can't convert string '" + cpp2::to_string(s) + "' to enum of type Color")));
return Black; 
}

[[nodiscard]] auto Color::from_code(cpp2::impl::in<std::string_view> s) -> Color{
std::string str {s}; return from_string(cpp2::string_util::replace_all(cpp2::move(str), "Color::", "")); }
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions