Skip to content

Commit ce6bac4

Browse files
authored
Merge pull request #74483 from xedin/fix-sendable-to-any-bridging
[SILGen] Fix `any Sendable` to `Any` bridging when result should be p…
2 parents 0246150 + 1e9e351 commit ce6bac4

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/SILGen/SILGenBridging.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,8 @@ static ManagedValue emitCBridgedToNativeValue(
11701170
.getAsSingleValue(SGF, loc);
11711171

11721172
// Convert to the marker existential if necessary.
1173-
auto anyType = SGF.getASTContext().getAnyExistentialType();
1174-
if (nativeType != anyType) {
1173+
if (!v.isInContext()) {
1174+
auto anyType = SGF.getASTContext().getAnyExistentialType();
11751175
v = SGF.emitTransformedValue(loc, v, anyType, nativeType);
11761176
}
11771177

test/SILGen/Inputs/objc_bridging_sendable.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
@interface NSBlah: NSObject
44
- (void) takeSendable: (id __attribute__((swift_attr("@Sendable")))) x;
5-
@property (readonly) id __attribute__((swift_attr("@Sendable"))) x;
5+
@property(readonly) id __attribute__((swift_attr("@Sendable"))) x;
6+
- (nullable __attribute__((swift_attr("@Sendable"))) id)test:(NSError *_Nullable __autoreleasing * _Nullable)error;
67
@end

test/SILGen/objc_bridging_sendable.swift

+21
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,25 @@ public func passSendableToObjC(_ s: Sendable) {
1212

1313
public func useSendableProperty(_ ns: NSBlah) {
1414
_ = ns.x
15+
let _: (Int, Any, String, [Any]) = (42, ns.x, "", [1, 2, 3])
16+
}
17+
18+
// CHECK-LABEL: sil private [ossa] @$s22objc_bridging_sendable23test_use_of_buffer_inityyKFypSo6NSBlahCKXEfU_ : $@convention(thin) @substituted <τ_0_0> (@guaranteed NSBlah) -> (@out τ_0_0, @error any Error) for <Any>
19+
// CHECK: bb0(%0 : $*Any, %1 : @guaranteed $NSBlah):
20+
// CHECK: [[TEST_REF:%.*]] = objc_method %1 : $NSBlah, #NSBlah.test!foreign : (NSBlah) -> () throws -> any Sendable, $@convention(objc_method) (Optional<AutoreleasingUnsafeMutablePointer<Optional<NSError>>>, NSBlah) -> @autoreleased Optional<AnyObject>
21+
// CHECK: [[RESULT:%.*]] = apply [[TEST_REF]]({{.*}}, %1) : $@convention(objc_method) (Optional<AutoreleasingUnsafeMutablePointer<Optional<NSError>>>, NSBlah) -> @autoreleased Optional<AnyObject>
22+
// CHECK: switch_enum [[RESULT]] : $Optional<AnyObject>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2
23+
// CHECK: bb1([[SUCCESS:%.*]] : @owned $AnyObject)
24+
// CHECK-NEXT: [[OPT_RESULT_VALUE:%.*]] = unchecked_ref_cast [[SUCCESS]] : $AnyObject to $Optional<AnyObject>
25+
// CHECK-NEXT: // function_ref _bridgeAnyObjectToAny(_:)
26+
// CHECK-NEXT: [[BRIDGE_INTRINSIC_REF:%.*]] = function_ref @$ss018_bridgeAnyObjectToB0yypyXlSgF : $@convention(thin) (@guaranteed Optional<AnyObject>) -> @out Any
27+
// CHECK-NEXT: apply [[BRIDGE_INTRINSIC_REF]](%0, [[OPT_RESULT_VALUE]]) : $@convention(thin) (@guaranteed Optional<AnyObject>) -> @out Any
28+
func test_use_of_buffer_init() throws {
29+
func test<T: Sendable>(_: (NSBlah) throws -> T) rethrows -> T {
30+
fatalError()
31+
}
32+
33+
let _: Any = try test {
34+
try $0.test()
35+
}
1536
}

0 commit comments

Comments
 (0)