hsutter / cppfront

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

used std::variant instead of union in cpp2::out #1008

Closed ARG-Helper closed 6 months ago

ARG-Helper commented 7 months ago

I performed a blind replacement of the union in cpp2::out with a variant in order to demonstrate a bug in the original implementation. This branch should cause some tests to fail

ARG-Helper commented 7 months ago

example code to demonstrate bug:

inline void f2(cpp2::out<int> in){
    in.construct(1);
}
inline void f3(const int& in){
   throw 1;
}
inline void f1(cpp2::out<int> in){
   try{
       f2(cpp2::out(&in));
       f3(in.value());
   }catch(...){
       in.construct(2);
   }
}
int main(){
   cpp2::deferred_init<int> x; 
   f1(cpp2::out(&x));
   return x.value();
}
hsutter commented 6 months ago

Thanks! Adding xref to this comment: https://github.com/hsutter/cppfront/issues/1012#issuecomment-1986138995

I don't think this bug can happen unless the types are used directly. Cppfront never generates those code patterns.

Does that help answer the question?

Again, thanks!

hsutter commented 6 months ago

Thanks again. I think the code is fine as long as it's used from cppfront-generated code, which we know will never call construct twice.

However, this has helped me see that I need to clarify that some things in cpputil.h) are, and others aren't, intended for users to use directly. So I've put the latter into an impl namespace in this commit: 7fd706de88b57857af4d5a31c8dc5429535d367f

Thanks!