Skip to content

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

Closed
@hsutter

Description

@hsutter

Discussed in #1048

Originally posted by ntrel April 1, 2024

main: () =
{
    i := 0;
    p: std::unique_ptr<int>;
    while i < 3 next i++ {
        if !i { break; }
        p = new<int>;
    }
    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<T>::construct(auto:61&& ...) [with auto:61 = {std::unique_ptr<int, std::default_delete<int> > (&)()}; T = std::unique_ptr<int>]’:
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<int>::unique_ptr(<brace-enclosed initializer list>)’
  774 | uct(auto&& ...args) -> void { cpp2_default.enforce(!init);  new (&data) T{CPP2_FORWARD(args)...};  init = true; }
      |                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```</div>

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions