hsutter / cppfront

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

[BUG] Assigning a lambda to a `constexpr` variable does not appear correctly formatted in the lowered C++ #1102

Open bluetarpmedia opened 3 weeks ago

bluetarpmedia commented 3 weeks ago

Describe the bug This is a minor formatting bug when lowering to C++. Assigning a function expression to a constexpr variable causes the lowered C++ function's compound statement / block to be formatted incorrectly. The emission of the newlines appear to be delayed.

To Reproduce Run cppfront on this code:

main: () = {
    add_v1: = :(a, b) -> _ = {
        a += 1;
        b += 2;
        return a + b;
    };

    // Note the difference here
    //      v
    add_v2: == :(a, b) -> _ = {
        a += 1;
        b += 2;
        return a + b;
    };
}

It lowers to:

auto main() -> int{
    auto add_v1 {[](auto const& a, auto const& b) mutable -> auto{
        a += 1;
        b += 2;
        return a + b; 
    }}; 

    auto constexpr add_v2 = [](auto const& a, auto const& b) mutable -> auto{a += 1;b += 2;return a + b; };

}

(EDIT - Please ignore the += for the const-ref params, I'm just keeping the syntax short.) Repro on Godbolt