@@ -2637,19 +2637,31 @@ RValue RValueEmitter::visitUnderlyingToOpaqueExpr(UnderlyingToOpaqueExpr *E,
26372637}
26382638
26392639RValue RValueEmitter::visitUnreachableExpr (UnreachableExpr *E, SGFContext C) {
2640- // Emit the expression, followed by an unreachable. To produce a value of
2641- // arbitrary type, we emit a temporary allocation, with the use of the
2642- // allocation in the unreachable block. The SILOptimizer will eliminate both
2643- // the unreachable block and unused allocation.
2640+ // Emit the expression, followed by an unreachable.
26442641 SGF.emitIgnoredExpr (E->getSubExpr ());
2642+ SGF.B .createUnreachable (E);
2643+
2644+ // Continue code generation in a block with no predecessors.
2645+ // Whatever code is emitted here is guaranteed to be removed by SIL passes.
2646+ SGF.B .emitBlock (SGF.createBasicBlock ());
26452647
2648+ // Since the type is uninhabited, use a SILUndef of so that we can return
2649+ // some sort of RValue from this API.
26462650 auto &lowering = SGF.getTypeLowering (E->getType ());
2647- auto resultAddr = SGF.emitTemporaryAllocation (E, lowering.getLoweredType ());
2651+ auto loweredTy = lowering.getLoweredType ();
2652+ auto undef = SILUndef::get (SGF.F , loweredTy);
26482653
2649- SGF.B .createUnreachable (E);
2650- SGF.B .emitBlock (SGF.createBasicBlock ());
2654+ // Create an alloc initialized with contents from the undefined addr type.
2655+ // It seems pack addresses do not satisfy isPlusOneOrTrivial, so we need an
2656+ // actual allocation.
2657+ if (loweredTy.isAddress ()) {
2658+ auto resultAddr = SGF.emitTemporaryAllocation (E, loweredTy);
2659+ SGF.emitSemanticStore (E, undef, resultAddr, lowering, IsInitialization);
2660+ return RValue (SGF, E, SGF.emitManagedRValueWithCleanup (resultAddr));
2661+ }
26512662
2652- return RValue (SGF, E, SGF.emitManagedRValueWithCleanup (resultAddr));
2663+ // Otherwise, if it's not an address, just emit the undef value itself.
2664+ return RValue (SGF, E, ManagedValue::forRValueWithoutOwnership (undef));
26532665}
26542666
26552667VarargsInfo Lowering::emitBeginVarargs (SILGenFunction &SGF, SILLocation loc,
0 commit comments