Skip to content

Optional cast of opaque values #8291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3334,8 +3334,9 @@ RValue RValueEmitter::visitOptionalEvaluationExpr(OptionalEvaluationExpr *E,

// Form the optional using address operations if the type is address-only or
// if we already have an address to use.
bool isByAddress = usingProvidedContext || optTL.isAddressOnly();

bool isByAddress = ((usingProvidedContext || optTL.isAddressOnly()) &&
SGF.silConv.useLoweredAddresses());

std::unique_ptr<TemporaryInitialization> optTemp;
if (!usingProvidedContext && isByAddress) {
// Allocate the temporary for the Optional<T> if we didn't get one from the
Expand All @@ -3357,7 +3358,7 @@ RValue RValueEmitter::visitOptionalEvaluationExpr(OptionalEvaluationExpr *E,
RestoreOptionalFailureDest restoreFailureDest(SGF,
JumpDest(failureBB, SGF.Cleanups.getCleanupsDepth(), E));

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

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

Expand Down
28 changes: 28 additions & 0 deletions test/SILGen/opaque_values_silgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,34 @@ func s360________guardEnum<T>(_ e: IndirectEnum<T>) {
}
}

// Tests contextual init() of opaque value types
// ---
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s370_____optToOptCastxSgSQyxGlF : $@convention(thin) <T> (@in Optional<T>) -> @out Optional<T> {
// CHECK: bb0([[ARG:%.*]] : $Optional<T>):
// CHECK: [[BORROWED_ARG:%.*]] = begin_borrow [[ARG]]
// CHECK: [[COPY__ARG:%.*]] = copy_value [[BORROWED_ARG]]
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]] : $Optional<T>, $Optional<T>
// CHECK: destroy_value [[ARG]]
// CHECK: return [[COPY__ARG]] : $Optional<T>
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s370_____optToOptCastxSgSQyxGlF'
func s370_____optToOptCast<T>(_ x : T!) -> T? {
return x
}

// Tests casting optional opaques to optional opaques
// ---
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s380___contextualInitySiSgF : $@convention(thin) (Optional<Int>) -> () {
// CHECK: bb0([[ARG:%.*]] : $Optional<Int>):
// CHECK: [[ALLOC_OF_BOX:%.*]] = alloc_box ${ var Optional<Int> }, var
// CHECK: [[PROJ_BOX:%.*]] = project_box [[ALLOC_OF_BOX]]
// CHECK: store [[ARG]] to [trivial] [[PROJ_BOX]] : $*Optional<Int>
// CHECK: destroy_value [[ALLOC_OF_BOX]]
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s380___contextualInitySiSgF'
func s380___contextualInit(_ a : Int?) {
var x: Int! = a
}

// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
// ---
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {
Expand Down