@@ -506,26 +506,16 @@ emitBuiltinCastReference(SILGenFunction &gen,
506506 auto &toTL = gen.getTypeLowering (toTy);
507507 assert (!fromTL.isTrivial () && !toTL.isTrivial () && " expected ref type" );
508508
509- if (fromTL.isLoadable () || toTL.isLoadable ()) {
510- if (UncheckedRefCastInst::canRefCastType (fromTL. getLoweredType ())
511- && UncheckedRefCastInst::canRefCastType ( toTL.getLoweredType ())) {
509+ if (fromTL.isLoadable () || toTL.isLoadable ()) {
510+ if (auto refCast = gen. B . tryCreateUncheckedRefCast (loc, args[ 0 ]. getValue (),
511+ toTL.getLoweredType ())) {
512512 // Create a reference cast, forwarding the cleanup.
513513 // The cast takes the source reference.
514- auto &in = args[0 ];
515- SILValue out = gen.B .createUncheckedRefCast (loc, in.getValue (),
516- toTL.getLoweredType ());
517- return ManagedValue (out, in.getCleanup ());
518- }
519- if (UncheckedRefBitCastInst::canRefBitCastFromType (fromTL.getLoweredType ())
520- && UncheckedRefBitCastInst::canRefBitCastToType (toTL.getLoweredType ()))
521- {
522- auto &in = args[0 ];
523- SILValue out = gen.B .createUncheckedRefBitCast (loc, in.getValue (),
524- toTL.getLoweredType ());
525- return ManagedValue (out, in.getCleanup ());
514+ return ManagedValue (refCast, args[0 ].getCleanup ());
526515 }
527516 }
528- // If casting between address-only types, cast the address.
517+ // We are either casting between address-only types, or cannot promote to a
518+ // cast of reference values.
529519 //
530520 // If the from/to types are invalid, then use a cast that will fail at
531521 // runtime. We cannot catch these errors with SIL verification because they
@@ -609,9 +599,9 @@ static ManagedValue emitBuiltinReinterpretCast(SILGenFunction &gen,
609599 SILValue out = gen.B .createUncheckedBitCast (loc, in.getValue (),
610600 toTL.getLoweredType ());
611601
612- // If the cast reduces to unchecked_ref_bit_cast , then the source and dest
602+ // If the cast reduces to unchecked_ref_cast , then the source and dest
613603 // have identical cleanup, so just forward the cleanup as an optimization.
614- if (isa<UncheckedRefBitCastInst >(out))
604+ if (isa<UncheckedRefCastInst >(out))
615605 return ManagedValue (out, in.getCleanup ());
616606
617607 // Otherwise leave the original cleanup and retain the cast value.
0 commit comments