hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 224 forks source link

[BUG] Cannot call Cpp2 constexpr function from `static_assert` at file scope #1074

Open bluetarpmedia opened 1 month ago

bluetarpmedia commented 1 month ago

Describe the bug A Cpp2 function declared as constexpr (with ==) cannot be called from a static_assert at file scope because the function definition is moved below the static_assert line.

To Reproduce Run cppfront on this code:

add: (a: int, b: int) -> int == { return a + b; }

static_assert(add(5, 6) == 11);

It lowers to (with minor edits to simplify):

[[nodiscard]] constexpr auto add(cpp2::impl::in<int> a, cpp2::impl::in<int> b) -> int;

static_assert(add(5, 6) == 11);

[[nodiscard]] constexpr auto add(cpp2::impl::in<int> a, cpp2::impl::in<int> b) -> int{return a + b; }

and the C++ compiler produces this error:

error: static assertion expression is not an integral constant expression
    3 | static_assert(add(5, 6) == 11);
      |               ^~~~~~~~~~~~~~~

Repro on Godbolt

I think a user who writes the above would expect the mixed code to work, since they've written the add definition before the static_assert line.

Additional context I was translating the std::ranges::any_of example code from cppreference.