@@ -2268,15 +2268,27 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
2268
2268
Type *fty = julia_type_to_llvm (jtype);
2269
2269
const jl_cgval_t &fval_info = argv[i + 1 ];
2270
2270
emit_typecheck (ctx, fval_info, jtype, " new" );
2271
- if (!type_is_ghost (fty)) {
2272
- Value *fval = NULL , *dest = NULL ;
2273
- if (!init_as_value) {
2271
+ Value *dest = NULL ;
2272
+ if (!init_as_value) {
2273
+ bool isunion = jl_is_uniontype (jtype);
2274
+ if (!type_is_ghost (fty) || isunion) {
2274
2275
// avoid unboxing the argument explicitly
2275
2276
// and use memcpy instead
2276
2277
dest = ctx.builder .CreateConstInBoundsGEP2_32 (lt, strct, 0 , i);
2277
2278
}
2279
+ if (isunion) {
2280
+ int fsz = jl_field_size (sty, i);
2281
+ // compute tindex from rhs
2282
+ // jl_cgval_t rhs_union = convert_julia_type(ctx, rhs, jtype);
2283
+ Value *tindex = compute_tindex_unboxed (ctx, fval_info, jtype);
2284
+ tindex = ctx.builder .CreateNUWSub (tindex, ConstantInt::get (T_int8, 1 ));
2285
+ Value *ptindex = ctx.builder .CreateGEP (T_int8, emit_bitcast (ctx, dest, T_pint8), ConstantInt::get (T_size, fsz - 1 ));
2286
+ ctx.builder .CreateStore (tindex, ptindex);
2287
+ }
2288
+ }
2289
+ if (!type_is_ghost (fty)) {
2290
+ Value *fval = NULL ;
2278
2291
fval = emit_unbox (ctx, fty, fval_info, jtype, dest);
2279
-
2280
2292
if (init_as_value) {
2281
2293
if (lt->isVectorTy ())
2282
2294
strct = ctx.builder .CreateInsertElement (strct, fval, ConstantInt::get (T_int32, idx));
0 commit comments