Skip to content

Commit bb76e3a

Browse files
author
Joe Shajrawi
authored
Merge pull request #8155 from shajrawi/InjectOptional
Support Optional Injection of Opaque value types
2 parents a4c6af1 + 8a7f9ba commit bb76e3a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ SILGenFunction::emitInjectOptional(SILLocation loc,
4747

4848
// If the value is loadable, just emit and wrap.
4949
// TODO: honor +0 contexts?
50-
if (optTL.isLoadable()) {
50+
if (optTL.isLoadable() || !silConv.useLoweredAddresses()) {
5151
ManagedValue objectResult = generator(SGFContext());
5252
auto some = B.createEnum(loc, objectResult.forward(*this), someDecl, optTy);
5353
return emitManagedRValueWithCleanup(some, optTL);

test/SILGen/opaque_values_silgen.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ protocol EmptyP {}
1818

1919
struct AddressOnlyStruct : EmptyP {}
2020

21+
struct AnyStruct {
22+
let a: Any
23+
}
24+
2125
func s010_hasVarArg(_ args: Any...) {}
2226

2327
// Tests Address only enums's construction
@@ -459,6 +463,27 @@ func s260_______AOnly_enum(_ s: AddressOnlyStruct) {
459463
_ = AddressOnlyEnum.phantom(s)
460464
}
461465

466+
// Tests InjectOptional for opaque value types + conversion of opaque structs
467+
// ---
468+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s270_convOptAnyStructyAA0gH0VADSgcF : $@convention(thin) (@owned @callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct) -> () {
469+
// CHECK: bb0([[ARG:%.*]] : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct):
470+
// CHECK: [[BORROWED_ARG:%.*]] = begin_borrow [[ARG]]
471+
// CHECK: [[COPY_ARG:%.*]] = copy_value [[BORROWED_ARG]]
472+
// CHECK: [[PAPPLY:%.*]] = partial_apply %{{.*}}([[COPY_ARG]]) : $@convention(thin) (@in Optional<AnyStruct>, @owned @callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct) -> @out Optional<AnyStruct>
473+
// CHECK: destroy_value [[PAPPLY]] : $@callee_owned (@in Optional<AnyStruct>) -> @out Optional<AnyStruct>
474+
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]] : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct, $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct
475+
// CHECK: [[BORROWED_ARG2:%.*]] = begin_borrow [[ARG]]
476+
// CHECK: [[COPY_ARG2:%.*]] = copy_value [[BORROWED_ARG2]]
477+
// CHECK: [[PAPPLY2:%.*]] = partial_apply %{{.*}}([[COPY_ARG2]]) : $@convention(thin) (@in Optional<AnyStruct>, @owned @callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct) -> @out Optional<AnyStruct>
478+
// CHECK: destroy_value [[PAPPLY2]] : $@callee_owned (@in Optional<AnyStruct>) -> @out Optional<AnyStruct>
479+
// CHECK: end_borrow [[BORROWED_ARG2]] from [[ARG]] : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct, $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct
480+
// CHECK: destroy_value [[ARG]] : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct
481+
// CHECK: return %{{.*}} : $()
482+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s270_convOptAnyStructyAA0gH0VADSgcF'
483+
func s270_convOptAnyStruct(_ a1: @escaping (AnyStruct?) -> AnyStruct) {
484+
let _: (AnyStruct?) -> AnyStruct? = a1
485+
let _: (AnyStruct!) -> AnyStruct? = a1
486+
}
462487

463488
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
464489
// ---
@@ -479,14 +504,23 @@ func s999_____condTFromAny<T>(_ x: Any, _ y: T) {
479504
}
480505
}
481506

482-
// s250_________testBoxT continued Tests Implicit Value Construction under Opaque value mode
507+
// s250_________testBoxT continued Test Implicit Value Construction under Opaque value mode
483508
// ---
484509
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen3BoxVACyxGx1t_tcfC : $@convention(method) <T> (@in T, @thin Box<T>.Type) -> @out Box<T> {
485510
// CHECK: bb0([[ARG0:%.*]] : $T, [[ARG1:%.*]] : $@thin Box<T>.Type):
486511
// CHECK: [[RETVAL:%.*]] = struct $Box<T> ([[ARG0]] : $T)
487512
// CHECK: return [[RETVAL]] : $Box<T>
488513
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen3BoxVACyxGx1t_tcfC'
489514

515+
// s270_convOptAnyStruct continued Test: reabstraction thunk helper
516+
// ---
517+
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_T020opaque_values_silgen9AnyStructVSgACIxir_A2DIxir_TR : $@convention(thin) (@in Optional<AnyStruct>, @owned @callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct) -> @out Optional<AnyStruct> {
518+
// CHECK: bb0([[ARG0:%.*]] : $Optional<AnyStruct>, [[ARG1:%.*]] : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct):
519+
// CHECK: [[APPLYARG:%.*]] = apply [[ARG1]]([[ARG0]]) : $@callee_owned (@in Optional<AnyStruct>) -> @out AnyStruct
520+
// CHECK: [[RETVAL:%.*]] = enum $Optional<AnyStruct>, #Optional.some!enumelt.1, [[APPLYARG]] : $AnyStruct
521+
// CHECK: return [[RETVAL]] : $Optional<AnyStruct>
522+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen9AnyStructVSgACIxir_A2DIxir_TR'
523+
490524
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @{{.*}} : $@convention(thin) (Int, Int, Int, Int, Int, @owned @callee_owned (@in (Int, (Int, (Int, Int)), Int)) -> @out (Int, (Int, (Int, Int)), Int)) -> (Int, Int, Int, Int, Int)
491525
// CHECK: bb0([[ARG0:%.*]] : $Int, [[ARG1:%.*]] : $Int, [[ARG2:%.*]] : $Int, [[ARG3:%.*]] : $Int, [[ARG4:%.*]] : $Int, [[ARG5:%.*]] : $@callee_owned (@in (Int, (Int, (Int, Int)), Int)) -> @out (Int, (Int, (Int, Int)), Int)):
492526
// CHECK: [[TUPLE_TO_APPLY0:%.*]] = tuple ([[ARG2]] : $Int, [[ARG3]] : $Int)

0 commit comments

Comments
 (0)