hsutter / cppfront

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

[BUG] read from inactive union member in cpp2::out.construct() #1012

Closed ARG-Helper closed 3 months ago

ARG-Helper commented 3 months ago

Describe the bug cpp2::out.construct() may read from the inactive member of its anonymus union when called more than once

To Reproduce int main(){ cpp2::deferred_init<int> a; cpp2::out o{&a}; o.construct(1); o.construct(2); }

Additional context the variable initialisation rules of cpp2 ensure that construct is only ever called once for each cpp2::out chain

turning cpp2::deferred_init<T>* into a T* to it's contained T is doable in a standart compliant way using reinterpret_cast<T*>() followed by std::launder()

hsutter commented 3 months ago

Thanks! Note that cpp2::in, cpp2::out, and cpp2::deferred_init are Cpp2 language support implementation details that should only be used by Cpp2 generated code, which has language guarantees that ensure out::construct is called only once.

So I'm not sure this is a problem, except possibly that the name should be changed (or put into a subnamespace?) to avoid giving the impression that users would use it directly.

gregmarr commented 3 months ago

@hsutter Maybe a detail or impl namespace would help?

hsutter commented 3 months ago

Thanks again. Yes, an impl namespace is the ticket: 7fd706de88b57857af4d5a31c8dc5429535d367f

Thanks!