Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hsutter opened this issue Jun 22, 2024 Discussed in #1048 · 1 comment
Closed

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

hsutter opened this issue Jun 22, 2024 Discussed in #1048 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@hsutter
Copy link
Owner

hsutter commented Jun 22, 2024

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>
@hsutter hsutter added the bug Something isn't working label Jun 22, 2024
@hsutter hsutter self-assigned this Jun 22, 2024
@hsutter
Copy link
Owner Author

hsutter commented Jun 22, 2024

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

@hsutter hsutter closed this as completed Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant