@@ -2472,6 +2472,27 @@ static Value *compute_box_tindex(jl_codectx_t &ctx, Value *datatype, jl_value_t
24722472 return tindex;
24732473}
24742474
2475+ // Returns typeof(v), or null if v is a null pointer at run time.
2476+ // This is used when the value might have come from an undefined variable,
2477+ // yet we try to read its type to compute a union index when moving the value.
2478+ static Value *emit_typeof_or_null (jl_codectx_t &ctx, Value *v)
2479+ {
2480+ BasicBlock *nonnull = BasicBlock::Create (jl_LLVMContext, " nonnull" , ctx.f );
2481+ BasicBlock *postBB = BasicBlock::Create (jl_LLVMContext, " postnull" , ctx.f );
2482+ Value *isnull = ctx.builder .CreateICmpEQ (v, Constant::getNullValue (v->getType ()));
2483+ ctx.builder .CreateCondBr (isnull, postBB, nonnull);
2484+ BasicBlock *entry = ctx.builder .GetInsertBlock ();
2485+ ctx.builder .SetInsertPoint (nonnull);
2486+ Value *typof = emit_typeof (ctx, v);
2487+ ctx.builder .CreateBr (postBB);
2488+ nonnull = ctx.builder .GetInsertBlock (); // could have changed
2489+ ctx.builder .SetInsertPoint (postBB);
2490+ PHINode *ti = ctx.builder .CreatePHI (typof->getType (), 2 );
2491+ ti->addIncoming (Constant::getNullValue (typof->getType ()), entry);
2492+ ti->addIncoming (typof, nonnull);
2493+ return ti;
2494+ }
2495+
24752496// get the runtime tindex value, assuming val is already converted to type typ if it has a TIndex
24762497static Value *compute_tindex_unboxed (jl_codectx_t &ctx, const jl_cgval_t &val, jl_value_t *typ)
24772498{
@@ -2482,9 +2503,12 @@ static Value *compute_tindex_unboxed(jl_codectx_t &ctx, const jl_cgval_t &val, j
24822503
24832504 if (val.TIndex )
24842505 return ctx.builder .CreateAnd (val.TIndex , ConstantInt::get (T_int8, 0x7f ));
2485- if (val.isboxed )
2486- return compute_box_tindex (ctx, emit_typeof_boxed (ctx, val), val.typ , typ);
2487- return compute_box_tindex (ctx, emit_typeof_boxed (ctx, val), val.typ , typ);
2506+ Value *typof;
2507+ if (val.isboxed && !jl_is_concrete_type (val.typ ) && !jl_is_type_type (val.typ ))
2508+ typof = emit_typeof_or_null (ctx, val.V );
2509+ else
2510+ typof = emit_typeof_boxed (ctx, val);
2511+ return compute_box_tindex (ctx, typof, val.typ , typ);
24882512}
24892513
24902514static void union_alloca_type (jl_uniontype_t *ut,
0 commit comments