Skip to content

Commit b9ee609

Browse files
author
Joe Shajrawi
committed
Support casting of optional to optional for opaque values
1 parent 204030a commit b9ee609

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,8 +3334,9 @@ RValue RValueEmitter::visitOptionalEvaluationExpr(OptionalEvaluationExpr *E,
33343334

33353335
// Form the optional using address operations if the type is address-only or
33363336
// if we already have an address to use.
3337-
bool isByAddress = usingProvidedContext || optTL.isAddressOnly();
3338-
3337+
bool isByAddress = ((usingProvidedContext || optTL.isAddressOnly()) &&
3338+
SGF.silConv.useLoweredAddresses());
3339+
33393340
std::unique_ptr<TemporaryInitialization> optTemp;
33403341
if (!usingProvidedContext && isByAddress) {
33413342
// Allocate the temporary for the Optional<T> if we didn't get one from the

test/SILGen/opaque_values_silgen.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ func s360________guardEnum<T>(_ e: IndirectEnum<T>) {
710710
}
711711
}
712712

713+
// Tests casting optional opaques to optional opaques
714+
// ---
715+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s370_____optToOptCastxSgSQyxGlF : $@convention(thin) <T> (@in Optional<T>) -> @out Optional<T> {
716+
// CHECK: bb0([[ARG:%.*]] : $Optional<T>):
717+
// CHECK: [[BORROWED_ARG:%.*]] = begin_borrow [[ARG]]
718+
// CHECK: [[COPY__ARG:%.*]] = copy_value [[BORROWED_ARG]]
719+
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]] : $Optional<T>, $Optional<T>
720+
// CHECK: destroy_value [[ARG]]
721+
// CHECK: return [[COPY__ARG]] : $Optional<T>
722+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s370_____optToOptCastxSgSQyxGlF'
723+
func s370_____optToOptCast<T>(_ x : T!) -> T? {
724+
return x
725+
}
726+
713727
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
714728
// ---
715729
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {

0 commit comments

Comments
 (0)