cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] "Code block with parameters" lowers arguments without semicolon

Open DyXel opened this issue 1 year ago • 4 comments

Describe the bug Title.

To Reproduce Steps to reproduce the behavior:

  1. Sample code
main: () = {
    (copy a: std::string, copy b: std::string) {
        a = "Hello, ";
        b = "World! ";
        std::cout << a << b;
    }
}
  1. Expected result - what you expected to happen I would expect one of these to happen: a. cppfront errors out and tells me that I need to initialize the parameters (if thats the intention) b. They are lowered wrapped in the deferred_init class and the usual lifetime initialization semantics apply c. they are lowered as is, just with the extra ;
  2. Actual result/error outputs the following c++ code (redundant parts omitted):
auto main() -> int{
{
std::string a // <- whoops!
std::string b // <- whoops!
    {
        a = "Hello, ";
        b = "World! ";
        std::cout << cpp2::move(a) << cpp2::move(b);
    }
}
}

Additional context I call this "Code block with parameters", but I don't know if there's a better name for them. I checked the documentation and they are referenced as "more RAII locally-scoped variables" and "local and immediate (aka 'let' in other languages)" which doesn't roll off the tongue too well 😛

DyXel avatar Jun 22 '24 22:06 DyXel

~~side note - shouldn't a and b be declared inside the block? and there also seems to be an extra } at the end in the lowered cpp1. Think there's a little more wrong here than just the missing ; 😆~~

zaucy avatar Jul 24 '24 18:07 zaucy

@zaucy It is inside the block. Does this formatting help you see what's happening?

auto main() -> int
{
  {
    std::string a
    std::string b
    {
        a = "Hello, ";
        b = "World! ";
        std::cout << cpp2::move(a) << cpp2::move(b);
    }
  }
}

Here's a version with some context.

main: () = {
    std::cout << "before";
    (copy a: std::string, copy b: std::string) {
        a = "Hello, ";
        b = "World! ";
        std::cout << a << b;
    }
    std::cout << "after";
}
auto main() -> int{
    std::cout << "before";
{
std::string a
std::string b
    {
        a = "Hello, ";
        b = "World! ";
        std::cout << cpp2::move(a) << cpp2::move(b);
    }
}
    std::cout << "after";
}

Now, the formatting isn't the greatest, but the code is correct other than the semicolons missing.

gregmarr avatar Jul 24 '24 18:07 gregmarr

Ah you're right my bad. I think the indentation is what threw me.

zaucy avatar Jul 24 '24 18:07 zaucy

Yeah, that indentation could stand to be fixed.

gregmarr avatar Jul 24 '24 19:07 gregmarr