hsutter / cppfront

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

Bug: Definite initialization shouldn't be in a loop #1133

Closed hsutter closed 1 week ago

hsutter commented 1 week ago

Discussed in https://github.com/hsutter/cppfront/discussions/1048

Originally posted by **ntrel** April 1, 2024 ```c++ main: () = { i := 0; p: std::unique_ptr; while i < 3 next i++ { if !i { break; } p = new; } std::cout << p* << "\n"; // no cppfront error } ``` Due to the break, p seems to be uninitialized when it is dereferenced. This would also happen when the initial loop test is false. BTW, on my system, I get a compile error from g++ and latest cppfront git. g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 ``` $ g++ --std=c++20 loopinit.cpp -I ~/git/cppfront/include/ In file included from loopinit.cpp:4: /home/nick/git/cppfront/include/cpp2util.h: In instantiation of ‘void cpp2::impl::deferred_init::construct(auto:61&& ...) [with auto:61 = {std::unique_ptr > (&)()}; T = std::unique_ptr]’: loopinit.cpp:23:20: required from here /home/nick/git/cppfront/include/cpp2util.h:774:76: error: no matching function for call to ‘std::unique_ptr::unique_ptr()’ 774 | uct(auto&& ...args) -> void { cpp2_default.enforce(!init); new (&data) T{CPP2_FORWARD(args)...}; init = true; } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
hsutter commented 1 week ago

Actually this is a duplicate of #1049, closing...