Skip to content

Commit 9790333

Browse files
author
Joe Shajrawi
committed
Support contextual init of opaque optional types
1 parent b9ee609 commit 9790333

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,7 +3358,7 @@ RValue RValueEmitter::visitOptionalEvaluationExpr(OptionalEvaluationExpr *E,
33583358
RestoreOptionalFailureDest restoreFailureDest(SGF,
33593359
JumpDest(failureBB, SGF.Cleanups.getCleanupsDepth(), E));
33603360

3361-
SILValue NormalArgument;
3361+
SILValue NormalArgument = nullptr;
33623362
if (emitOptimizedOptionalEvaluation(E, NormalArgument, optInit, *this)) {
33633363
// Already emitted code for this.
33643364
} else if (isByAddress) {
@@ -3384,7 +3384,7 @@ RValue RValueEmitter::visitOptionalEvaluationExpr(OptionalEvaluationExpr *E,
33843384
failureBB->eraseFromParent();
33853385

33863386
// The value we provide is the one we've already got.
3387-
if (!isByAddress)
3387+
if (!isByAddress && NormalArgument)
33883388
return RValue(SGF, E,
33893389
SGF.emitManagedRValueWithCleanup(NormalArgument, optTL));
33903390

test/SILGen/opaque_values_silgen.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ func s360________guardEnum<T>(_ e: IndirectEnum<T>) {
710710
}
711711
}
712712

713-
// Tests casting optional opaques to optional opaques
713+
// Tests contextual init() of opaque value types
714714
// ---
715715
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s370_____optToOptCastxSgSQyxGlF : $@convention(thin) <T> (@in Optional<T>) -> @out Optional<T> {
716716
// CHECK: bb0([[ARG:%.*]] : $Optional<T>):
@@ -724,6 +724,20 @@ func s370_____optToOptCast<T>(_ x : T!) -> T? {
724724
return x
725725
}
726726

727+
// Tests casting optional opaques to optional opaques
728+
// ---
729+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s380___contextualInitySiSgF : $@convention(thin) (Optional<Int>) -> () {
730+
// CHECK: bb0([[ARG:%.*]] : $Optional<Int>):
731+
// CHECK: [[ALLOC_OF_BOX:%.*]] = alloc_box ${ var Optional<Int> }, var
732+
// CHECK: [[PROJ_BOX:%.*]] = project_box [[ALLOC_OF_BOX]]
733+
// CHECK: store [[ARG]] to [trivial] [[PROJ_BOX]] : $*Optional<Int>
734+
// CHECK: destroy_value [[ALLOC_OF_BOX]]
735+
// CHECK: return %{{.*}} : $()
736+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s380___contextualInitySiSgF'
737+
func s380___contextualInit(_ a : Int?) {
738+
var x: Int! = a
739+
}
740+
727741
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
728742
// ---
729743
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {

0 commit comments

Comments
 (0)