File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3585,6 +3585,14 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
35853585
35863586 StrCat (GetUnsafeTypeAsString (qual_type));
35873587 PushBrace brace (*this );
3588+ if (record->isUnion ()) {
3589+ auto *field = expr->getInitializedFieldInUnion ();
3590+ assert (field && expr->getNumInits () == 1 );
3591+ StrCat (GetNamedDeclAsString (field), token::kColon );
3592+ ConvertFieldInit (field, expr->getInit (0 ));
3593+ SetFreshType (qual_type);
3594+ return false ;
3595+ }
35883596 int i = 0 ;
35893597 for (const auto *field : record->fields ()) {
35903598 StrCat (GetNamedDeclAsString (field), token::kColon );
Original file line number Diff line number Diff line change @@ -1916,6 +1916,21 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
19161916 return false ;
19171917 }
19181918
1919+ if (record->isUnion ()) {
1920+ auto *field = expr->getInitializedFieldInUnion ();
1921+ assert (field && expr->getNumInits () == 1 );
1922+ PushConversionKind push (*this , ConversionKind::Unboxed);
1923+ auto name = GetUnsafeTypeAsString (qual_type);
1924+ StrCat (std::format (" {{ let __u: Value<{}> = Rc::new(RefCell::new({}));" ,
1925+ name, GetDefaultAsString (qual_type)));
1926+ StrCat (std::format (" {}::{}(__u.as_pointer()).write(" , name,
1927+ GetNamedDeclAsString (field)));
1928+ ConvertVarInit (field->getType (), expr->getInit (0 ));
1929+ StrCat (" ); Rc::try_unwrap(__u).ok().unwrap().into_inner() }" );
1930+ computed_expr_type_ = ComputedExprType::FreshValue;
1931+ return false ;
1932+ }
1933+
19191934 StrCat (GetUnsafeTypeAsString (qual_type));
19201935 {
19211936 PushBrace brace (*this );
Original file line number Diff line number Diff line change @@ -93,6 +93,18 @@ fn main_0() -> i32 {
9393 assert ! ( ( ( basic:: i( ru. clone( ) ) . read( ) ) == 1065353216 ) ) ;
9494 assert ! ( ( ( pf. read( ) ) == 1_f32 ) ) ;
9595 assert ! ( ( ( ( * buf. borrow( ) ) [ ( 3 ) as usize ] as i32 ) == 63 ) ) ;
96+ let braced: Value < basic > = Rc :: new ( RefCell :: new ( {
97+ let __u: Value < basic > = Rc :: new ( RefCell :: new ( <basic >:: default ( ) ) ) ;
98+ basic:: i ( __u. as_pointer ( ) ) . write ( 5 ) ;
99+ Rc :: try_unwrap ( __u) . ok ( ) . unwrap ( ) . into_inner ( )
100+ } ) ) ;
101+ assert ! ( ( ( basic:: i( braced. as_pointer( ) ) . read( ) ) == 5 ) ) ;
102+ let copied: Value < basic > = Rc :: new ( RefCell :: new ( {
103+ let __u: Value < basic > = Rc :: new ( RefCell :: new ( <basic >:: default ( ) ) ) ;
104+ basic:: i ( __u. as_pointer ( ) ) . write ( 7 ) ;
105+ Rc :: try_unwrap ( __u) . ok ( ) . unwrap ( ) . into_inner ( )
106+ } ) ) ;
107+ assert ! ( ( ( basic:: i( copied. as_pointer( ) ) . read( ) ) == 7 ) ) ;
96108 {
97109 let anon_0: Value < anon_0 > = Rc :: new ( RefCell :: new ( <anon_0 >:: default ( ) ) ) ;
98110 anon_0:: ai ( anon_0. as_pointer ( ) ) . write ( 11 ) ;
Original file line number Diff line number Diff line change @@ -69,6 +69,10 @@ unsafe fn main_0() -> i32 {
6969 assert ! ( ( ( ( * ru) . i) == ( 1065353216 ) ) ) ;
7070 assert ! ( ( ( * pf) == ( 1_f32 ) ) ) ;
7171 assert ! ( ( ( buf[ ( 3 ) as usize ] as i32 ) == ( 63 ) ) ) ;
72+ let mut braced: basic = basic { i : 5 } ;
73+ assert ! ( ( ( braced. i) == ( 5 ) ) ) ;
74+ let mut copied: basic = basic { i : 7 } ;
75+ assert ! ( ( ( copied. i) == ( 7 ) ) ) ;
7276 {
7377 let mut anon_0: anon_0 = <anon_0 >:: default ( ) ;
7478 anon_0. ai = 11 ;
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ int main(void) {
3838 assert (*pf == 1 .0f );
3939 assert (buf[3 ] == 0x3F );
4040
41+ union basic braced{5 };
42+ assert (braced.i == 5 );
43+ union basic copied = {7 };
44+ assert (copied.i == 7 );
45+
4146 {
4247 union {
4348 int ai;
You can’t perform that action at this time.
0 commit comments